For a list of all the data structures I have implemented, click here.
This is a simple example of a binary tree implementation in Java. We create a custom Node class which can take values of
generic type T, so that we can create trees of different types using the same implementation class. We also create a
BinaryTree class which will hold the actual binary tree.
We provide three major traversals for our binary tree:
- pre order traversal
- in order traversal
- post order traversal
If you check the Node class, I'm taking the generic class T as the type of the value field. This is so that during
runtime, we can have a string binary tree or integer binary tree, or a binary tree of a custom class as well. T is a generic class in Java,
the type of which can be decided in runtime.
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 installOnce 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/BinaryTree_Implementation_Java_POC-1.0-SNAPSHOT.jarFor our example, we'll consider the following binary tree, which is a very simple example:
The following traversals are for this example binary 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.
1 2 4 5 3 6 7In this type of traversal, we first traverse to the left child, then the node, then the right child.
4 2 5 1 6 3 7In this type of traversal, we first traverse to the left child, then the right child, and finally the node, .
4 5 2 6 7 3 1