This project implements a BinarySearchTreeWithDups class in Java, extending the standard BinarySearchTree to handle duplicate entries. The implementation includes methods for adding elements, counting occurrences, and efficiently counting elements greater than a specified value. The code is designed to be efficient, leveraging the sorted nature of the binary search tree.
This class extends BinarySearchTree<T> and provides the following key functionalities:
add(T newEntry): Overrides theaddmethod to insert elements, handling duplicates by placing them in the left subtree.addEntryHelperNonRecursive(T newEntry)(private): A non-recursive helper method for adding entries, ensuring duplicate handling.countIterative(T target): Counts the number of times a specific element appears in the tree (non-recursive).countGreaterRecursive(T target): Counts the number of elements in the tree greater than the target value (recursive).countGreaterIterative(T target): Counts the number of elements in the tree greater than the target value (non-recursive, using a stack).countUniqueValues(): Counts the number of unique values in the tree (O(n) time complexity).
BinaryNode.java: Defines theBinaryNodeclass for tree nodes.BinaryTree.java: Provides the basic binary tree implementation.BinarySearchTree.java: Implements the standard binary search tree.BinarySearchTreeWithDups.java: The core class with duplicate handling and counting methods.PoliceReport.java: Defines thePoliceReportclass used in the driver program.ProjectCPartADriver.java: Driver program for testing theBinarySearchTreeWithDupsclass.ProjectCPartBDriver.java: Driver program for processing police report data and comparing tree performance.README.md: This file, providing project documentation.image_c5a152.jpg: An image (please describe its purpose if relevant).
-
Compilation:
- Compile the Java files using a Java Development Kit (JDK). For example:
javac *.java
- Compile the Java files using a Java Development Kit (JDK). For example:
-
Running the Driver Programs:
- To test the
BinarySearchTreeWithDupsclass:java ProjectCPartADriver
- To process the police report data:
java ProjectCPartBDriver
- Note: Ensure the police report data file is in the correct location for
ProjectCPartBDriverto run successfully.
- To test the
- Duplicate Handling: The
addEntryHelperNonRecursivemethod ensures that duplicate entries are inserted in the left subtree, maintaining the specified behavior. - Efficiency: The
countGreaterIterativeandcountUniqueValuesmethods are implemented with efficiency in mind, using a stack for iterative traversal and aHashSetfor unique value tracking, respectively. - Recursion vs. Iteration: The project demonstrates both recursive (
countGreaterRecursive) and iterative (countIterative,countGreaterIterative) solutions to tree traversal and processing.
-
Clone the Repository:
git clone https://github.com/editor123/ProjectCTreesFiles/tree/master
-
Pull Latest Changes:
git pull origin main
-
Make Changes and Commit:
git add <modified_files> git commit -m "Describe your changes"
-
Push Changes:
git push origin main
- Sangharsha, Rina, Victor