BINARY Search Tree
BINARY Search Tree
1. Binary Search tree can be defined as a class of binary trees, in which the nodes are arranged
in a specific order. This is also called ordered binary tree.
2. In a binary search tree, the value of all the nodes in the left sub-tree is less than the value
of the root.
3. Similarly, value of all the nodes in the right sub-tree is greater than or equal to the value of
the root.
4. This rule will be recursively applied to all the left and right sub-trees of the root.
A Binary search tree is shown in the above figure. As the constraint applied on the BST,
we can see that the root node 30 doesn't contain any value greater than or equal to 30 in its left
sub-tree and it also doesn't contain any value less than 30 in its right sub-tree.
Q. Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
The process of creating BST by using the given elements, is shown in the image below.
-
Operations on Binary Search Tree
There are many operations which can be performed on a binary search tree.
SN Operation Description
1 Searching in Finding the location of some specific element in a binary search tree.
BST
2 Insertion in Adding a new element to the binary search tree at the appropriate location so that th
BST property of BST do not violate.
3 Deletion in Deleting some specific node from a binary search tree. However, there can be variou
BST cases in deletion depending upon the number of children, the node have.
1. #include <iostream>
2. #include <stdlib.h>
3. using namespace std;
4. struct Node {
5. int data;
6. Node *left;
7. Node *right;
8. };
9. Node* create(int item)
10. {
11. Node* node = new Node;
12. node->data = item;
13. node->left = node->right = NULL;
14. return node;
15. }
16.
17. void inorder(Node *root)
18. {
19. if (root == NULL)
20. return;
21.
22. inorder(root->left);
23. cout<< root->data << " ";
24. inorder(root->right);
25. }
26. Node* findMinimum(Node* cur)
27. {
28. while(cur->left != NULL) {
29. cur = cur->left;
30. }
31. return cur;
32. }
33. Node* insertion(Node* root, int item)
34. {
35. if (root == NULL)
36. return create(item);
37. if (item < root->data)
38. root->left = insertion(root->left, item);
39. else
40. root->right = insertion(root->right, item);
41.
42. return root;
43. }
44.
45. void search(Node* &cur, int item, Node* &parent)
46. {
47. while (cur != NULL && cur->data != item)
48. {
49. parent = cur;
50.
51. if (item < cur->data)
52. cur = cur->left;
53. else
54. cur = cur->right;
55. }
56. }
57.
58. void deletion(Node*& root, int item)
59. {
60. Node* parent = NULL;
61. Node* cur = root;
62.
63. search(cur, item, parent);
64. if (cur == NULL)
65. return;
66.
67. if (cur->left == NULL && cur->right == NULL)
68. {
69. if (cur != root)
70. {
71. if (parent->left == cur)
72. parent->left = NULL;
73. else
74. parent->right = NULL;
75. }
76. else
77. root = NULL;
78.
79. free(cur);
80. }
81. else if (cur->left && cur->right)
82. {
83. Node* succ = findMinimum(cur- >right);
84.
85. int val = succ->data;
86.
87. deletion(root, succ->data);
88.
89. cur->data = val;
90. }
91.
92. else
93. {
94. Node* child = (cur->left)? Cur- >left: cur->right;
95.
96. if (cur != root)
97. {
98. if (cur == parent->left)
99. parent->left = child;
100. else
101. parent->right = child;
102. }
103.
104. else
105. root = child;
106. free(cur);
107. }
108. }
109.
110. int main()
111. {
112. Node* root = NULL;
113. int keys[8];
114. for(int i=0;i<8;i++)
115. {
116. cout << "Enter value to be inserted";
117. cin>>keys[i];
118. root = insertion(root, keys[i]);
119. }
120.
121. inorder(root);
122. cout<<"\n";
123. deletion(root, 10);
124. inorder(root);
125. return 0;
126. }
Output:
5 5 10 15 20 25 30 40
5 5 15 20 25 30 40
Heap data structure is a complete binary tree that satisfies the heap property, where any given
node is
• always greater than its child node/s and the key of the root node is the largest among all other
nodes. This property is also called max heap property.
• always smaller than the child node/s and the key of the root node is the smallest among all other
nodes. This property is also called min heap property.
Max-heap
Min-heap
This type of data structure is also called a binary heap.
Heap Operations
Some of the important operations performed on a heap are described below along with their
algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is used to create a
Min-Heap or a Max-Heap.
1. Let the input array be
Initial Array
Complete binary
tree
3. Start from the first index of non-leaf node whose index is given by n/2 - 1.
Swap if necessary
7. Repeat steps 3-7 until the subtrees are also heapified.
Algorithm
Heapify(array, size, i)
set i as largest
leftChild = 2i + 1
rightChild = 2i + 2
To create a Max-Heap:
MaxHeap(array, size)
loop from the first index of non-leaf node down to zero
call heapify
For Min-Heap, both leftChild and rightChild must be smaller than the parent for all nodes.
If there is no node,
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)
Insert at
the end
Select the
element to be deleted
2. Swap it with the last element.
Swap
with the last element
3. Remove the last element.
Peek operation returns the maximum element from Max Heap or minimum element from Min
Heap without deleting the node.
return rootNode
Extract-Max/Min
Extract-Max returns the node with maximum value after removing it from a Max Heap whereas
Extract-Min returns the node with minimum after removing it from Min Heap.