Lec11-Binary-Search-Trees-09092024-090505pm (2)
Lec11-Binary-Search-Trees-09092024-090505pm (2)
ALGORITHMS
1
Binary Search Tree
2
Binary Search Trees
60 80
58 65 92
3
Examples
4
Example
• Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
5
Example
• Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
15
6
Example
• Input list of numbers:
14 15 4 9 7 18 3 5 16 4 20 17 9 14 5
14
4 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
9 18
10
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
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
7 16
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
4
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 20
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
17
5
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 9 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 14 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
Basic Operations:
Construct an empty BST
Determine if the BST is empty
Search the BST for a given item
Insert an item in the BST maintaining the BST property
Delete an item from the BST maintaining the BST property
Traverse the BST
20
Implementing a BST
class Node
{
public:
int data;
Node * left,
* right;
};
21
Implementing a BST
Node()
{
left = right = NULL;
}
Node(int item)
{
data = item;
left = right = NULL;
}
22
Implementing a BST
class BinarySearchTree
{
public:
Node* root; // pointer to root node
BinarySearchTree();
bool isPresent(Node *, int);
bool isEmpty();
void Insert(Node *, int);
void Delete(Node *, int);
void InorderTraversal(Node *);
void PreorderTraversal(Node *);
void PostorderTraversal(Node *);
};
23
Implementing a BST
BinarySearchTree()
{
root = NULL;
}
Bool isEmpty( )
{
return root == NULL;
}
24
Insertion in BST
25
void Insert(Node *ptr, int value){
Node * prev = NULL;
while (ptr!=NULL){
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 if (value > prev->data)
prev->right = temp;
}
26
Searching in Binary Search
Tree
• The above process will continue till the item is found or you
reached end of the tree.
27
Implementing bst search
bool isPresent(Node *ptr, int data)
{
bool found = false;
for(;;)
{
If(ptr == NULL)
break;
If(data < ptr->data)
ptr = ptr->left;
else if (data > ptr->data)
ptr = ptr->right;
Else if(value==ptr->data)
{found = true; break;}
}
return found;
}
28
Implementing bst search –
recursive solution
Node* Search(Node *temp, int num)
{
if(temp==NULL)
return;
else if(temp->data == num)
return temp;
else if(temp->data < num)
return Search(temp->right, num);
else if(temp->data > num)
return Search(temp->left, num);
}
29
Traversals in BST
30
Utility Function
Node* Parent = b.getParent(ptr, 1)
Write a function that finds a node from the tree and returns its parent’s
address
32
Deletion in BST
33
Implementing a BST
Deletion
To delete a node x from a BST, we have three cases:
x is leaf
34
Implementing a BST
CASE 1:
x is a leaf
Simply make the appropriate pointer in x’s parent a null pointer
75 delete(b.root, 58);
60 80
x
58 65 92
35
Implementing a BST
60 80
65 92
x 58
36
Implementing a BST
CASE II:
x is has one child
Set the appropriate pointer in x’s parent to point to this child
parent 75
60 80
58 65 x 92
62
37
Implementing a BST
75
60 80
58 62 92
38
Implementing a BST
CASE III:
x has two children
Replace the value stored in node x by its inorder successor.
Delete this successor
39
Implementing a BST
F J x
A
H O
I M P
Inorder successor
AFGHIJKLMNOP
xSucc K N
40
Implementing a BST
F K x
A
H parent O
I M P
xSucc K N
41
Implementing a BST
F K x
A
H parent O
1: find that node
2: find its parent
3: does node have children I M P
3.a: no child
3.b: 1 child
3.c: 2 children xSucc K N
4: determine what child is node of parent (a,b)
5: make arrangement for children with parent
6: delete node L
42
Implementing a BST
F K x
A
H O
I M P
L N
43
Deletion Code
Node* DeleteNode(Node* temp, int num){
While(temp!=NULL)
{
75
if (num < temp->data)
temp = temp->left; 60 80
else if (num > temp->data)
temp = temp->right; 58 65 92
else if((temp->data == num)) //node found
{
Node *parent; //for all
Node *min , int number; //only for third case
//Case 1: 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; }
44
// 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){ 75
if(parent->left == temp)
parent->left = temp->left; 60 80
else if (parent->right == temp)
parent->right = temp->left;}
58 65 92
else if(temp->right != NULL){
62
if(parent->left == temp)
parent->left = temp->right; 75
else if (parent->right == temp)
parent->right = temp->right;} 60 80
delete temp; }
58 65 92
62
45
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;
46
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;
47
//Case 2: if node to be deleted has two children
J
else if((temp->left != NULL) && (temp->right != NULL))
{
K
min O
= FindMin(temp->right);
//will return the min. no. found in Right subtree
num = min->data; temp
K
DeleteNode(temp->right, min->data); G
O K
//calling to itself recursively F KJ
temp->data= num;
A H
} O
}
I M P
if(temp==NULL) min
Left->Root->right
{cout<<“Node not found”; K N
return temp;}
L
} A, F, G ,H ,I, J, K, L, M, N, O,P
In order successor of J
48