Professional Documents
Culture Documents
1
Binary Search Tree
2
Binary Search Trees
A Binary Search Tree (BST) is a binary tree in
which the value in each node is greater than all values in
its left subtree and less than all values in its right subtree.
75
60 80
58 65 92
3
BST As ADT
Collection of Data Elements:
A binary tree in which for each node x:
value in left child of x < value in x < value in right child of x
Basic Operations:
4
Examples
5
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
6
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
15
7
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
8
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
9
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
10
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
9 18
11
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
12
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
13
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
7 16
14
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
7 16
4
15
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
7 16 20
4
16
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
7 16 20
17
5
4
17
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 18
7 9 16 20
17
5
4
18
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 14 18
7 9 16 20
17
5
4
19
Example
Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 15
3 9 14 18
7 9 16 20
17
5
};
21
Implementing a BST
Node()
{
left = right = NULL;
}
Node(int item)
{
data = item;
left = right = NULL;
}
22
Implementing a BST
class BinarySearchTree
{
private:
Node* root; // pointer to root node
};
23
Implementing a BST
BinarySearchTree()
{
root = NULL;
}
Bool Empty( )
{
return root == NULL;
}
24
Implementation of a bst
bool Search(Node *, int);
void Insert(Node *, int);
void Delete(Node *, int);
25
Insertion in BST
Three steps of insertion
◦ If the root of the tree is NULL then insert the first node and root points to
that node.
◦ If the inserted number is lesser than the root node then insert the node
in the left sub-tree.
◦ If the inserted number is greater than the root node then insert the node
in the right sub-tree.
26
Modify the search algorithm so that a pointer parentPtr trails locPtr down
the tree, keeping track of the parent of each node being checked:
1. Initialize pointers locPtr = root, parentPtr = NULL
2. While locPtr ≠ null pointer:
a. parentPtr = locPtr
b. If value < locPtr->data
locPtr = locPtr->left
Else if value > locPtr->data
locPtr = locPtr->right
Else
value is already in the tree; return a found
indicator
3. Get a new node pointed to by newPtr, put the value in its
data part, and set left and right to null.
4. If parentPtr = null pointer // empty tree
Set root = newptr.
Else if value < parentPtr->data
Set parentPtr->left = newPtr.
Else
Set parentPtr->right = newPtr.
27
void Insert(Node *ptr, int value){
Node * prev = 0;
Iterative Solution
while (ptr!=0){
prev = ptr;
if (value < ptr->data)
ptr = ptr->left;
else if(value > ptr->data)
ptr = ptr->right;
else{
cout<<"Value already exist";return ;}
}
Node * temp = new Node;
temp->data=value; temp->left=0; temp->right=0;
if(prev==0)
root = temp;
else if (value < prev->data)
prev->left = temp;
else
prev->right = temp;
}
28
Insertion Function – recursive solution
Void Insert(Node* temp, int num)
{
if ( temp == NULL )
{
temp = new Node;
temp->data= num;
temp->left= NULL;
temp->right=NULL;
}
else if(num < temp->data)
Insert(temp->left, num);
else //if( num >= temp->data)
Insert(temp->right, num);
}
29
Searching in Binary Search Tree
Three steps of searching
◦ The item which is to be searched is compared with the root node. If the
item is equal to the root, then we are done.
◦ If its less than the root node then we search in the left sub-tree.
◦ If its more than the root node then we search in the right sub-tree.
The above process will continue till the item is found or you
reached end of the tree.
30
Implementing bst search
1. Set pointer locPtr = root.
2. Repeat the following:
If locPtr is null
return false
If value < locPtr->data
locPtr = locPtr->left
Else if value > locPtr->data
locPtr = locPtr->right
Else
return true
31
Iterative Solution
for(;;)
{
If(found || ptr == NULL)
break;
If(data < ptr->data)
ptr = ptr->left;
else if (data > ptr->data)
else ptr = ptr->right;
found = true;
}
return found;
32
}
Implementing bst search – recursive solution
bool Search(Node *temp, int num)
{
if(temp==NULL)
return false;
else if(temp->data == num)
return true;
else if(temp->data < num)
Search(temp->right, num);
else if(temp->data > num)
Search(temp->left, num);
}
33
Utility Function
Write a function that finds a node from the tree and returns its parent’s address
34
Utility Function
Write a function that finds the node with minimum value.
35
Deletion in BST
When we delete a node, we need to consider how we take care of the
children of the deleted node.
◦ This has to be done such that the property of the search tree is maintained.
36
Implementing a BST
Deletion
To delete a node x from a BST, we have three cases:
x is leaf
37
Implementing a BST
CASE 1:
x is a leaf
Simply make the appropriate pointer in x’s parent a null pointer
75
60 80
x 58 65 92
38
Implementing a BST
Make the left pointer of x’s parent null
Free x
75
60 80
x 58 65 92
39
Implementing a BST
CASE II:
x is has one child
Set the appropriate pointer in x’s parent to point to this child
75
60 80
58 65 x 92
62
40
Implementing a BST
75
60 80
58 65 x 92
62
41
Implementing a BST
CASE III:
x has two children
Replace the value stored in node x by its inorder successor.
Delete this successor
42
Implementing a BST
G
F J x
A
H O
I M P
xSucc K N
43
Implementing a BST
G
F K x
A
H O
I M P
xSucc K N
44
Implementing a BST
G
F K x
A
H O
I M P
xSucc K N
45
Deletion Code
void DeleteNode(Node* temp, int num){
if (temp==NULL)
cout<<"Number not Found";
else if((temp->data == num)){ //node found
Node *parent, *min ;
int number;
// if number is found at a leaf node
if((temp->left == NULL) && (temp->right == NULL)){
parent=GetParent(root, temp->data);
if(parent->left == temp)
parent->left= NULL;
else if (parent->right == temp)
parent->right = NULL;
delete temp;
}
46
SCENARIO: if(temp->left != NULL)
75 75
parent 60 80 parent 60 80
58 65 temp 92 temp 58 65 92
62 52
if (parent->right == temp) if(parent->left == temp)
parent->right = temp->left; parent->left = temp->left;
47
SCENARIO: if(temp->right != NULL)
75 75
parent 60 80 parent 60 80
58 65 temp 92 temp 58 65 92
66 59
if (parent->right == temp) if(parent->left == temp)
parent->right = temp->right; parent->left = temp->right;
48
// if node to be deleted has one child
else if(((temp->left == NULL) && (temp->right != NULL)) ||
((temp->left != NULL) && (temp->right == NULL))){
parent = GetParent(root, temp->data);
if(temp->left != NULL){
if(parent->left == temp)
parent->left = temp->left;
else if (parent->right == temp)
parent->right = temp->left;}
else if(temp->right != NULL){
if(parent->left == temp)
parent->left = temp->right;
else if (parent->right == temp)
parent->right = temp->right;}
delete temp; }
49
//if node to be deleted has two children
else if((temp->left != NULL) && (temp->right != NULL))
{
min = FindMin(temp->right); //will return the min. no. found in
Right subtree
number = min->data;
DeleteNode(temp->right, min->data); //calling to itself
recursively
temp->data= number;
}
} // end node found
} 50