CONTENTS:
- Insertion in a BST.
- Tree traversal techniques.
- Height of Binary Search Tree.
- Validating a BST.
- Finding the Lowest Common Ancestor of key1 and key2 in BST.
- Deletion of a node 'N' from BST.
- Finding maximum (or minimum) in path from node A to node B.
Binary Search Tree, is a node-based binary tree data structure having the following properties:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
- There must be no duplicate nodes.
Most of operations on binary search trees like insertion, deletion, search etc can be done in logarithmic time O(logN).
Let's discuss each of them in detail.
First of all lets see the Structure of binary search tree :
-
Insertion : insert(root, key) : Starting with root node keep on comparing key with the value of nodes of Binary Search Tree recursively with two conditions :
- If value of key is greater than current node : move to its right subtree
- otherwise : move to its left subtree
Practice On Hackerrank
-
Four types of tree traversal techniques :
- Pre-order tree traversal : Follow this order : root --- left --- right
It means that we need to first print the data of current node, then we have to move to its left subtree and then its right subtree recursively. - In-order tree traversal : Follow this order : left --- root --- right
In inorder traversal we first need to visit its left subtree and after printing its left subtree we print the current node data and then print its right subtree recursively. This traversal will print all the nodes of tree in ascending order. - Post-order tree traversal : Follow this order : left --- right --- root
In postorder traversal we first need to visit its left subtree and after printing its left subtree we print its right subtree recursively and then the value of current node recursively. - Level Order Traversal :
It is easy to implement if you know BFS algorithm. Before learning this you must learn BFS graph traversal algorithm.
Now make a queue and insert root in it. Then print it out and pop it from queue and insert its left child first and then right child into the queue. Then do the same for each node until queue is not empty.
- Pre-order tree traversal : Follow this order : root --- left --- right
-
Height of Binary Search Tree : Height is defined as the number of nodes in the longest path from the root node to a leaf node.
StackOverflow : finding height of a BST - Check if a tree is BST or not ? : Use an awesome property of BST : Inorder traversal of BST prints data in ascending order.
-
Finding the Lowest Common Ancestor of key1 and key2 in BST :
The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in BST that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself).
Consider 3 cases :- Both key1 and key2 are on left side of a node.
- Both key1 and key2 are on right side of a node.
- One is on left side and other is on right side.
-
Deletion of a node 'N' from BST :
Lets consider some cases which are necessary to consider while deleting a node :
- if left child of N is NULL : replace node N with its right child.
- if right child of N is NULL : replace node N with its left child.
- if none of child of N is NULL : we have 2 choices :
- replace it with minimum value node in its right subtree.
- or replace it with maximum value node in its left subtree.
- UPD1 : Finding maximum (or minimum) in path from node A to node B :
Link for practice @Hackerearth
So after reading about LCA(lowest common ancestor) in point 5, we can easily find it for given 2 nodes. But why LCA ? See. we can easily reach both nodes from LCA by just moving to left or right child recursively but to move from one node to other is difficult if they are in different subtrees without knowing their LCA. So LCA helps us here !
Now find max in path from LCA to node A. Lets call it ans1
Similarly find max in path from LCA to node B. Lets call it ans2
Finally your answer must be max(ans1, ans2) .
If you have any doubt about BST you can ask freely and if you find it useful please give your feedback in the comment section.
Happy Coding !
Hi Pritish, Thank you writing this blog post. It was really helpful. Please keep writing.
ReplyDeletehow to find uncle node in any BST in c++
ReplyDeleteI have not totally clear about LCA and maximum path from node A to node B can you please explain it by taking one example of it.
ReplyDeleteit is not maximum path but it is maximum value in the path
DeleteHi, in the hackerearth question, in the condition "if(root->data > x)" why are you returning "max(maxInPath(root->left, x), root->data)" don't you think max will be always root-> data? so why not return only root-> data?
ReplyDeletenice blog!!
ReplyDeleteGreat info on binary tree. Looking for software courses?
ReplyDeleteDOT NET Training in Chennai
Hadoop Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
JAVA Training in Chennai
German Classes in chennai
Loadrunner Training in Chennai
Loadrunner Training in T Nagar
Thanks buddy, it really helped me.
ReplyDeleteNice blog brother. keep it up.
ReplyDeletethanks man :)
ReplyDeleteNice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Best Social Trading Platforms
ReplyDeleteThis blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. Price Action Strategy PDF
ReplyDeleteI am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. Quotex Best Binary Options Broker Review
ReplyDeleteNice to read your article! I am looking forward to sharing your adventures and experiences. Best Trading Signals VFX Alerts
ReplyDelete