In the above example, we have created an auxiliary Node class that stores the int value as the key and the reference or address of the left and the right child. The above code gives us the following output: After it is found null, the traversal is stopped as it is the extreme right node. Since 3 does not have any left child, 3 is printed and then the right subtree of 3 is traversed. It traverses the right subtree of 1 and gets 3 as the root node. It again goes back to 1 as the root node and prints 1. It moves to the right subtree of 2 and encounters null. It again backtracks itself to 2 as the root node and prints 2. It prints 4 and then moves to its right which is null. For every subtree, it follows the same method of traversal.įor the tree formed above, it first moves to node 1, then to its left until it encounters a null node. It first traverses the left subtree of the root, then the root and the right subtree of the root. The inorder uses the left root right policy. Calling the function traverseTree with the root as the parameter traverses the tree in an inorder manner starting from the root. We then create the nodes of the tree by putting the values in them one by one and placing them at the required positions. In the main function, we have created an object of BinaryTree class named tree. We have created a method named traverseTree which traverses the binary tree in an inorder manner and prints the key value of all nodes. Since Java doesn't provide an inbuilt function for Trees, we have created a class named Binary Tree. The above example is an implementation of a Binary Tree in Java. The size of the array needs to be defined at the time of creation which leads to space issues. The array representation is sequential and static. The node representation is dynamic and the number of nodes can be increased as per requirement. The data inside every node can be a string, char, integer, double, or float data type.Ī binary tree can be implemented in two ways: A node representation and an array representation. In Java, a tree node is implemented using a class. Example: Java Program to Implement Binary Tree Let's learn how to implement binary tree data structure in Java. Operations like Insertion, Deletion, and Traversal are applied to them. They are used for sorting, searching, grouping, and storing data hierarchically. Most of the applications use variations of binary trees that are Tries, Binary Search Tree, and B-Trees. Therefore, the Number of nodes at nth level = Sum of nodes at all n-1 levels + 1īinary trees have many applications in the real world. Number of nodes at 3rd level = 8 = 1 + 2 + 4 + 1 = Number of nodes at 0th level + Number of nodes at 1st level + Number of nodes at 2nd level + 1 Number of nodes at 2nd level = 4 = 1 + 2 + 1 = Number of nodes at 0th level + Number of nodes at 1st level + 1 Number of nodes at 1st level = 2 = Number of nodes at 0th level + 1
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |