Binary Search Tree
Binary Search Tree
In a binary tree, every node can have a maximum of two children but there is no need to maintain
the order of nodes basing on their values. In a binary tree, the elements are arranged in the order
they arrive at the tree from top to bottom and left to right.
To enhance the performance of binary tree, we use a special type of binary tree known as Binary
Search Tree. Binary search tree mainly focuses on the search operation in a binary tree. Binary
search tree can be defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values in its left
subtree and only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values and
all the nodes in the right subtree of any node contains larger values as shown in the following
figure...
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes
with smaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary search tree.
● Search
● Insertion
● Deletion
In a binary search tree, the search operation is performed with O(log n) time complexity. The
search operation is performed as follows...
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
Step 4 - If both are not matched, then check whether search element is smaller or larger than that
node value.
Step 5 - If search element is smaller, then continue the search process in left subtree.
Step 6- If search element is larger, then continue the search process in right subtree.
Step 7 - Repeat the same until we find the exact element or until the search element is compared
with the leaf node
Step 8 - If we reach to the node having the value equal to the search value then display "Element
is found" and terminate the function.
Step 9 - If we reach to the leaf node and if it is also not matched with the search element, then
display "Element is not found" and terminate the function.
In a binary search tree, the insertion operation is performed with O(log n) time complexity. In
binary search tree, new node is always inserted as a leaf node. The insertion operation is
performed as follows...
Step 1 - Create a newNode with given value and set its left and right to NULL.
Step 4 - If the tree is Not Empty, then check whether the value of newNode is smaller or larger
than the node (here it is root node).
Step 5 - If newNode is smaller than or equal to the node then move to its left child. If newNode
is larger than the node then move to its right child.
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL).
Step 7 - After reaching the leaf node, insert the newNode as left child if the newNode is smaller
or equal to that leaf node or else insert it as right child.
In a binary search tree, the deletion operation is performed with O(log n) time complexity.
Deleting a node from Binary search tree includes following three cases...
Step 2 - Delete the node using free function (If it is a leaf) and terminate the function.
Case 2: Deleting a node with one child
We use the following steps to delete a node with one child from BST...
Step 2 - If it has only one child then create a link between its parent node and child node.
Step 3 - Delete the node using free function and terminate the function.
We use the following steps to delete a node with two children from BST...
Step 2 - If it has two children, then find the largest node in its left subtree (OR) the smallest node
in its right subtree.
Step 3 - Swap both deleting node and node which is found in the above step.
Step 4 - Then check whether deleting node came to case 1 or case 2 or else goto step 2
Step 7 - Repeat the same process until the node is deleted from the tree.
Example
10,12,5,4,20,8,7,15 and 13
50,15,62,5,20,58,91,3,8,37,60,24
A Leaf node can be directly deleted because, it will not have any
children.
Deleting a node with one child
Method 1:
1. visit the right sub tree of the deleting node
2. pluck the least value element called its inorder
successor
3. replace the deleting element with its inorder
successor
Method 2:
1. visit the left sub tree of the deleting node
2. pluck the greatest value element called its inorder
predecessor
3. replace the deleting element with its inorder
predecessor
13,3,4,12,14,10,5,1,8,2,7,9,11,6,18