0% found this document useful (0 votes)
52 views22 pages

BINARY Search Tree

A binary search tree is a binary tree where the value of each node is greater than or equal to all values in its left subtree and less than or equal to all values in its right subtree. Key operations on a binary search tree include searching for a node, inserting a new node, and deleting an existing node while maintaining the binary search tree properties. The operations make use of recursively traversing the tree and comparing node values to determine the appropriate subtree for insertion or location of a searched value.

Uploaded by

Mallinath Khonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views22 pages

BINARY Search Tree

A binary search tree is a binary tree where the value of each node is greater than or equal to all values in its left subtree and less than or equal to all values in its right subtree. Key operations on a binary search tree include searching for a node, inserting a new node, and deleting an existing node while maintaining the binary search tree properties. The operations make use of recursively traversing the tree and comparing node values to determine the appropriate subtree for insertion or location of a searched value.

Uploaded by

Mallinath Khonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

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.

Advantages of using binary search tree


1. Searching become very efficient in a binary search tree since, we get a hint at each step,
about which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays and
linked lists. In searching process, it removes half sub-tree at every step. Searching for an
element in a binary search tree takes o(log2n) time. In worst case, the time it takes to search
an element is 0(n).
3. It also speed up the insertion and deletion operations as compare to that in array and linked
list.

Q. Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50

1. Insert 43 into the tree as the root of the tree.


2. Read the next element, if it is lesser than the root node element, insert it as the root of the
left sub-tree.
3. Otherwise, insert it as the root of the right of the right sub-tree.

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.

Program to implement BST operations

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:

Enter value to be inserted? 10


Enter value to be inserted? 20
Enter value to be inserted? 30
Enter value to be inserted? 40
Enter value to be inserted? 5
Enter value to be inserted? 25
Enter value to be inserted? 15
Enter value to be inserted? 5

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

2. Create a complete binary tree from the array

Complete binary
tree
3. Start from the first index of non-leaf node whose index is given by n/2 - 1.

Start from the first


on leaf node
4. Set current element i as largest.
5. The index of left child is given by 2i + 1 and the right child is given by 2i + 2.
If leftChild is greater than currentElement (i.e. element at ith index), set leftChildIndex as
largest.
If rightChild is greater than element in largest, set rightChildIndex as largest.
6. Swap largest with currentElement

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

if leftChild > array[largest]


set leftChildIndex as largest
if rightChild > array[largest]
set rightChildIndex as largest

swap array[i] and array[largest]

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.

Insert Element into Heap

Algorithm for insertion in Max Heap

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.)

heapify the array


1. Insert the new element at the end of the tree.

Insert at
the end

2. Heapify the tree.


Heapify
the array
For Min Heap, the above algorithm is modified so that parentNode is always smaller
than newNode.

Delete Element from Heap

Algorithm for deletion in Max Heap

If nodeToBeDeleted is the leafNode


remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted
heapify the array

1. Select the element to be deleted.

Select the
element to be deleted
2. Swap it with the last element.

Swap
with the last element
3. Remove the last element.

Remove the last element

4. Heapify the tree.


Heapify the array
For Min Heap, above algorithm is modified so that both childNodes are greater smaller
than currentNode.

Peek (Find max/min)

Peek operation returns the maximum element from Max Heap or minimum element from Min
Heap without deleting the node.

For both Max heap and Min Heap

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.

You might also like