0% found this document useful (0 votes)
26 views48 pages

Lec11-Binary-Search-Trees-09092024-090505pm (2)

Uploaded by

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

Lec11-Binary-Search-Trees-09092024-090505pm (2)

Uploaded by

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

DATA STRUCTURES AND

ALGORITHMS

Week 11: Binary


Search Trees
Sohail Muhammad
Department of CS
Bahria University, Islamabad

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
Examples

A binary search tree Not a binary search tree

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

“Binary Search Tree” of the given


4 5 data set 19
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:
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

An inorder traversal of a BST retrieves values in ascending order.

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

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

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

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

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

Node* Inorder(Node* curr)


{
if(curr==NULL)
return;
else{
Inorder(curr->left);
cout<<curr->data;
Inorder(curr->right);}
}

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

Node* getParent(Node* curr, int num)


{
if((curr->left->data == num) ||(curr->right->data == num))
return curr;
else if(num < curr->data)
return getParent(curr->left, num);
else if(num > curr->data)
return getParent(curr->right, num);
}
31
Utility Function

Write a function that finds the node with minimum value.


BST b;
Node *min = b.FindMin(b.root)

Node* FindMin(Node* curr)


{
if(curr->left == NULL)
return curr;
else
return FindMin(curr->left);
}

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

33
Implementing a BST

Deletion
To delete a node x from a BST, we have three cases:

x is leaf

x has one child

x has two children

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

Make the left pointer of x’s parent null


Free x
75

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)

Temp could be a right son Temp could be a left son

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)

Temp could be a right son Temp could be a left son

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

You might also like