Binary Search Tree
Binary Search Tree
Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.
It is called a binary tree because each tree node has a maximum of two children.
It is called a search tree because it can be used to search for the presence of a number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary tree is
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two properties
The binary tree on the right isn't a binary search tree because the right subtree of the node "3" contains a
value smaller than it.
Example of creating a binary search tree
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
First, we have to insert 45 into the tree as the root of the tree.
Then, read the next element; if it is smaller than the root node, insert it as the root of the left
subtree, and move to the next element.
Otherwise, if the element is larger than the root node, then insert it as the root of the right subtree.
Now, let's see the process of creating the Binary search tree using the given data element. The process of
creating the BST is shown below -
As 15 is smaller than 45, so insert it as the root node of the left subtree.
As 79 is greater than 45, so insert it as the root node of the right subtree.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
Step 5 - Insert 10.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree of 10.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.
Step 9 - Insert 50.
50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of 55.
Now, the creation of binary search tree is completed. After that, let's move towards the operations that
can be performed on Binary search tree.
We can perform insert, delete and search operations on the binary search tree.
Now, let's understand the searching in binary tree using an example. We are taking the binary search tree
formed above. Suppose we have to find node 20 from the below tree.
Step1:
Step2:
Step3:
Now, let's see the algorithm to search an element in the Binary search tree.
Algorithm to search an element in Binary search tree
1. Search (root, item)
2.Step 1 - if (item = root → data) or (root = NULL)
3. return root
4.else if (item < root → data)
5. return Search(root → left, item)
6.else
7. return Search(root → right, item)
8.END if
9. Step 2 - END
Now let's understand how the deletion is performed on a binary search tree. We will also see an example
to delete an element from the given tree.
It is the simplest case to delete a node in BST. Here, we have to replace the leaf node with NULL and simply
free the allocated space.
We can see the process to delete a leaf node from BST in the below image. In below image, suppose we
have to delete node 90, as the node to be deleted is a leaf node, so it will be replaced with NULL, and the
allocated space will free.
When the node to be deleted has only one child
In this case, we have to replace the target node with its child, and then delete the child node. It means that
after replacing the target node with its child node, the child node will now contain the value to be deleted.
So, we simply have to replace the child node with NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below image. In the below image,
suppose we have to delete the node 79, as the node to be deleted has only one child, so it will be replaced
with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.
This case of deleting a node in BST is a bit complex among other two cases. In such a case, the steps to be
followed are listed as follows -
Step 1: Find the node to be deleted using search operation
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 found in above step.
Step 4: Then, check whether deleting node came to case 1 or case 2 else goto steps 2
Step 5: If it comes to case 1, then delete using case 1 logic.
Step 6: If it comes to case 2, then delete using case 2 logic.
Step 7: Repeat the same process until node is deleted from the tree.
The inorder successor is required when the right child of the node is not empty. We can obtain the inorder
successor by finding the minimum element in the right child of the node.
We can see the process of deleting a node with two children from BST in the below image. In the below
image, suppose we have to delete node 45 that is the root node, as the node to be deleted has two
children, so it will be replaced with its inorder successor. Now, node 45 will be at the leaf of the tree so
that it can be deleted easily.
Now, let's see the process of inserting a node into BST using an example.