ds notes
ds notes
In a
Binary search tree, the value of left node must be smaller than the parent
node, and the value of right node must be greater than the parent node.
This rule is applied recursively to the left and right subtrees of the root.
In the above figure, we can observe that the root node is 40, and all the
nodes of the left subtree are smaller than the root node, and all the nodes
of the right subtree are greater than the root node.
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.
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.
Advertisement
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.
ADVERTISEMENT
10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.
ADVERTISEMENT
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree
of 79.
ADVERTISEMENT
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.
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.
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:
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 -
o After that, replace that node with the inorder successor until the target
node is placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated
space.
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 understand how insertion is performed on a binary search tree.
Now, let's see the process of inserting a node into BST using an example.
The complexity of the Binary Search tree
Let's see the time and space complexity of the Binary search tree. We will see
the time complexity for insertion, deletion, and searching operations in best
case, average case, and worst case.
1. Time Complexity
2. Space Complexity
Operations Space
complexity
Insertion O(n)
Deletion O(n)
Search O(n)
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 h
as 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 giv
en binary tree is - \n");
109. inorder(root);
110. insertion(root, 2);
111. printf("\nAfter inserting node 2, the inorder traversal of the give
n binary tree is - \n");
112. inorder(root);
113. return 0;
114. }