For a list of all the data structures I have implemented, click here.
This is a simple example of a binary search tree implementation in Java. We create a custom Node
class which can take values of
integer type. We also create a BinarySearchTree
class which will hold the actual binary search tree.
We provide three major traversals for our binary search tree:
- pre order traversal
- in order traversal
- post order traversal
Once you clone this repo, cd
into the project root directory and run the following command to compile and build this maven project:
mvn clean install
Once you run this command, Maven will build the project and keep it in the /target
directory in the project root directory.
You can run the program using the command below:
java -jar target/binary_search_tree_implementation_poc-1.0-SNAPSHOT.jar
For our example, we'll consider the following binary search tree, which is a very simple example:
The following traversals are for this example binary search tree:
In this type of traversal, we first traverse to the node, then the left child, then the right child. You can create a much longer tree in your example and find out how it functions when there are move levels.
10 7 5 8 15 12 18
In this type of traversal, we first traverse to the left child, then the node, then the right child.
5 7 8 10 12 15 18
In this type of traversal, we first traverse to the left child, then the right child, and finally the node, .
5 8 7 12 18 15 10