Operations On BST
Operations On BST
In this article, we will discuss the Binary search tree. This article will be very helpful
and informative to the students with technical background as it is an important topic
of their course.
Before moving directly to the binary search tree, let's first see a brief description of
the tree.
What is a tree?
A tree is a kind of data structure that is used to represent the data in hierarchical
form. It can be defined as a collection of objects or entities called as nodes that are
linked together to simulate a hierarchy. Tree is a non-linear data structure as the data
in a tree is not stored linearly or sequentially.
64M
1.2K
Exception Handling in Java - Javatpoint
Similarly, we can see the left child of root node is greater than its left child and
smaller than its right child. So, it also satisfies the property of binary search tree.
Therefore, we can say that the tree in the above image is a binary search tree.
Suppose if we change the value of node 35 to 55 in the above tree, check whether
the tree will be binary search tree or not.
In the above tree, the value of root node is 40, which is greater than its left child 30
but smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the
property of Binary search tree. Therefore, the above tree is not a binary search tree.
o Searching an element in the Binary search tree is easy as we always have a hint
that which subtree has the desired element.
o As compared to array and linked lists, insertion and deletion operations are
faster in BST.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
o First, we have to insert 45 into the tree as the root of the tree.
o 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.
o 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.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
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.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of
15.
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.
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element,
if it is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.
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.
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.
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.
When the node to be deleted has two children
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 -
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.
1. Time Complexity
Operations Best case time Average case time Worst case time
complexity complexity complexity
2. Space Complexity
Insertion O(n)
Deletion O(n)
Search O(n)
In this program, we will see the implementation of the operations of binary search
tree. Here, we will see the creation, inorder traversal, insertion, and deletion
operations of tree.
Here, we will see the inorder traversal of the tree to check whether the nodes of the
tree are in their proper location or not. We know that the inorder traversal always
gives us the data in ascending order. So, after performing the insertion and deletion
operations, we perform the inorder traversal, and after traversing, if we get data in
ascending order, then it is clear that the nodes are in their proper location.
1. #include <iostream>
2. using namespace std;
3. struct Node {
4. int data;
5. Node *left;
6. Node *right;
7. };
8. Node* create(int item)
9. {
10. Node* node = new Node;
11. node->data = item;
12. node->left = node->right = NULL;
13. return node;
14. }
15. /*Inorder traversal of the tree formed*/
16. void inorder(Node *root)
17. {
18. if (root == NULL)
19. return;
20. inorder(root->left); //traverse left subtree
21. cout<< root->data << " "; //traverse root node
22. inorder(root->right); //traverse right subtree
23. }
24. Node* findMinimum(Node* cur) /*To find the inorder successor*/
25. {
26. while(cur->left != NULL) {
27. cur = cur->left;
28. }
29. return cur;
30. }
31. Node* insertion(Node* root, int item) /*Insert a node*/
32. {
33. if (root == NULL)
34. return create(item); /*return new node if tree is empty*/
35. if (item < root->data)
36. root->left = insertion(root->left, item);
37. else
38. root->right = insertion(root->right, item);
39. return root;
40. }
41. void search(Node* &cur, int item, Node* &parent)
42. {
43. while (cur != NULL && cur->data != item)
44. {
45. parent = cur;
46. if (item < cur->data)
47. cur = cur->left;
48. else
49. cur = cur->right;
50. }
51. }
52. void deletion(Node*& root, int item) /*function to delete a node*/
53. {
54. Node* parent = NULL;
55. Node* cur = root;
56. search(cur, item, parent); /*find the node to be deleted*/
57. if (cur == NULL)
58. return;
59. if (cur->left == NULL && cur->right == NULL) /*When node has no children*/
60. {
61. if (cur != root)
62. {
63. if (parent->left == cur)
64. parent->left = NULL;
65. else
66. parent->right = NULL;
67. }
68. else
69. root = NULL;
70. free(cur);
71. }
72. else if (cur->left && cur->right)
73. {
74. Node* succ = findMinimum(cur->right);
75. int val = succ->data;
76. deletion(root, succ->data);
77. cur->data = val;
78. }
79. else
80. {
81. Node* child = (cur->left)? cur->left: cur->right;
82. if (cur != root)
83. {
84. if (cur == parent->left)
85. parent->left = child;
86. else
87. parent->right = child;
88. }
89. else
90. root = child;
91. free(cur);
92. }
93. }
94. int main()
95. {
96. Node* root = NULL;
97. root = insertion(root, 45);
98. root = insertion(root, 30);
99. root = insertion(root, 50);
100. root = insertion(root, 25);
101. root = insertion(root, 35);
102. root = insertion(root, 45);
103. root = insertion(root, 60);
104. root = insertion(root, 4);
105. printf("The inorder traversal of the given binary tree is - \n");
106. inorder(root);
107. deletion(root, 25);
108. printf("\nAfter deleting node 25, the inorder traversal of the given binary tree
is - \n");
109. inorder(root);
110. insertion(root, 2);
111. printf("\nAfter inserting node 2, the inorder traversal of the given binary tree
is - \n");
112. inorder(root);
113. return 0;
114. }
Output