Now let’s see an example of a Binary Search Tree node:
As in the above code snippet we define a node class having three property data, left and right, Left and right are pointers to the left and right node in a Binary Search Tree. Data is initialized with data which is passed when object for this node is created and left and right is set to null.
Now let’s see an example of a Binary Search Tree class.
The above example shows a framework of a Binary Search tree class, which contains a private variable root which holds the root of a tree, it is initialized to null.
Now lets implement each of this function:
1. insert(data) – It inserts a new node in a tree with a value data
In the above code we have two methods insert(data) and insertNode(node, newNode). Let’s understand them one by one:-
- insert(data) – It creates a new node with a value data, if the tree is empty it add this node to a tree and make it a root, otherwise it calls insert(node, data).
- insert(node, data) – It compares the given data with the data of the current node and moves left or right accordingly and recur until it finds a correct node with a null value where new node can be added.
2.remove(data) – This function removes a node with a given data.
In the above code we have two methods remove(data) and removeNode(node, data), let understand them one by one:
- remove(data) – It is helper methods which call removeNode by passing root node and given data and updates the root of the tree with the value returned by the function
- removeNode(node, data) – It searches for a node with a given data and then perform certain steps to delete it.
- Deleting the leaf node – As leaf node does not have any children, hence they can be easily removed and null is returned to the parent node
- Deleting a node with one child – If a node has a left child, then we update the pointer of the parent node to the left child of the node to be deleted and similarly, if a node have a right child then we update the pointer of the parent node to the right child of the node to be deleted
- Deleting a node with two children – In order to delete a node with two children we find the node with minimum value in its right subtree and replace this node with the minimum valued node and remove the minimum valued node from the tree
Now Lets understand different ways of traversing a Binary Search Tree.
inorder(node) – It performs inorder traversal of a tree starting from a given node
Algorithm for inorder:
Traverse the left subtree i.e perform inorder on left subtreeVisit the rootTraverse the right subtree i.e perform inorder on right subtree
1. preorder(node) – It performs preorder traversal of a tree starting from a given node.
Algorithm for preorder:
Visit the rootTraverse the left subtree i.e perform preorder on left subtreeTraverse the right subtree i.e perform preorder on right subtree
2. postorder(node) – It performs postorder traversal of a tree starting from a given node.
Algorithm for postorder:
Traverse the left subtree i.e perform postorder on left subtreeTraverse the right subtree i.e perform postorder on right subtreeVisit the root
Let’s declare some helper method which is useful while working with Binary Search Tree.
1. findMinNode(node) – It searches for a node with a minimum value starting from node.
As seen in the above method we start from a node and keep moving to the left subtree until we find a node whose left child is null, once we find such node we return it.
2. getRootNode() – It returns the root node of a tree.
3. search(data) – It searches the node with a value data in the entire tree.
Note : Different helper method can be declared in the BinarySearchTree class as per the requirement.
Now lets use the BinarySearchTree class and its different methods described above.
For more on binary trees, please refer to the following article: Binary tree Data Structure
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.