Binary Search Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Non-recursive Postorder Traversal

while(stack not empty && stack top right is temp)


{
pop stack into temp;
Algorithm postorder_nr() visit temp
{ }
temp=root; if stack empty
while(1) break;
{ move temp to stack top right;
while(temp is not NULL) } // end while
{
push temp onto stack; } // end algorithm
temp = temp ->left;
}
if stack top right is NULL temp=st.data[st.top]->right
{
pre-order (red): F, B, A, D, C, E, G, I, H;
pop stack into temp;
in-order (yellow): A, B, C, D, E, F, G, H, I;
visit temp;
post-order (green): A, C, E, D, B, H, I, G, F.
}

2/18/2020 DATA STRUCTURES-II 80


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) a
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer

}
}

11/17/2021 Advanced Data Structures 81


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL)
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
a
}
}

11/17/2021 Advanced Data Structures 82


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) b c
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
a
}
}

11/17/2021 Advanced Data Structures 83


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) c
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
ab
}
}

11/17/2021 Advanced Data Structures 84


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) c d e
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
ab
}
}

11/17/2021 Advanced Data Structures 85


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) d e
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
abc
}
}

11/17/2021 Advanced Data Structures 86


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) d e f
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
abc
}
}

11/17/2021 Advanced Data Structures 87


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) e f
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
abcd
}
}

11/17/2021 Advanced Data Structures 88


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL) f
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
abcde
}
}

11/17/2021 Advanced Data Structures 89


Algorithm BFS()
{
a
temp=root;
Insert temp into queue;
while Queue not empty
b c
{

f
Remove from queue into temp; d e
visit temp; Queue
if(temp->left is not NULL)
insert temp->left into queue;
if(temp->right is not NULL)
insert temp->right into queue; Answer
abcdef
}
}

11/17/2021 Advanced Data Structures 90


Assignment no 1
2. Implement binary tree and perform following operations: Creation of
binary tree and traversal recursive and non-recursive.

11/17/2021 Advanced Data Structures 91


Operations on binary tree
Copying Binary Tree (recursive)
Copy of binary tree using non recursive is done through preorder
treenode *copy(root)
{
temp=NULL
if (root!=NULL) {

Allocate memory for temp


temp->data=root->data;
temp->left=copy(root->left);
temp->right=copy(root->right);
}
return temp;
}

11/17/2021 Advanced Data Structures 92


Algorithm copy_nr(tree t2)
{ //t2 is original tree s1.push(temp1);
Allocate memory for root s2.push(temp2);
temp1=root;
Move temp1 to temp1->left
temp2=t2.root;
copy(temp1->data,temp2->data); Move temp2 to temp2->left
while(1) }
{ if stack empty break;
while(temp2!=NULL) else
{
{
if(temp2->left!=NULL)
{ Pop to temp1
Allocate memory for temp1->left; Pop to temp2
copy (temp1->left->data,temp2->left->data); temp1=temp1->right;
} temp2=temp2->right;
if(temp2->right!=NULL)
}
{
Allocate memory for temp1->right;; } //end while
copy temp1->right->data,temp2->right->data); }
}

11/17/2021 Advanced Data Structures 93


Erasing nodes in binary tree
Use postorder

11/17/2021 Advanced Data Structures 94


Algorithm depth_r()
Algorithm depth_nr()
{
{
Initialize d to 0;
temp=root; d=depth_r(root);
while(1) print d;
{ }
while(temp!=NULL) Algorithm depth_r(treenode *root)
{
{
push temp;
Initialize t1=0,t2=0;
move temp to temp->left;
if(d<st.top) if(root==NULL)
d=st.top; } return 0;
if(stack top right is NULL) else
{ {
pop to temp; } t1=depth_r(root->left);
while(stack not empty && stack top right is temp)
t2=depth_r(root->right);
{
pop to temp ; }
if(t1>t2)
if stack empty return ++t1;
break; else
move temp to stack top right; return ++t2;
} }
cout<<"\nDepth is "<<d+1; } }
11/17/2021 Advanced Data Structures 95
Algorithm mirror_r() Algorithm mirror_nr()
{ {
mirror_r(root); temp=root;
dispbfs();
q.insqueue(temp);
}
while(!q.empty())
Algorithm mirror_r(treenode *root) {
{ temp=q.delqueue();
swap left and right; swap left and right;
if(root->left!=NULL)
if(temp->left!=NULL)
mirror_r(root->left);
if(root->right!=NULL) q.insqueue(temp->left);
mirror_r(root->right); if(temp->right!=NULL)
} q.insqueue(temp->right);
}
dispbfs();
}

11/17/2021 Advanced Data Structures 96


Binary search Trees

It is a binary tree.It may be empty.If it is not empty then it satisfies the following properties

Every element has a unique key.


The keys in a nonempty left subtree are smaller than the key in the root of subtree.
The keys in a nonempty right subtree are larger than the key in the root of subtree.
The left and right subtrees are also binary search trees.

◦ Binary search trees provide an excellent structure for searching a list and at the same time for
inserting and deleting data into the list.

11/17/2021 Advanced Data Structures 97


Binary Search Tree

All < K All > K

11/17/2021 Advanced Data Structures 98


(a), (b) - complete and balanced trees;
(d) – nearly complete and balanced tree;
(c), (e) – neither complete nor balanced trees

11/17/2021 Advanced Data Structures 99


11/17/2021 Advanced Data Structures 100
Algorithm create()
{ else {
allocate memory and accept the data for root node; if(temp->right=NULL)
do {
{ temp->right=curr;
temp=root;
flag=1;
flag=0;
}
allocate memory and accept the data for curr node;
while(flag==0 && temp !=NULL) else
{ move temp to temp->right;
if(curr->data < temp->data) } //end else
{ } //end while flag
if(temp->left=NULL) Accept choice for adding more nodes;
{
}while(choice =yes); //end do
temp->left=curr;
} //end algorithm
flag=1;
}
else
move temp to temp->left
} //end if compare

11/17/2021 Advanced Data Structures 101


binary search tree creation
Jyoti,Deepa,Rekha,Amit,Gilda,Anita,Abolee,Kaustubh,Teena,Kasturi,Saurabh

11/17/2021 Advanced Data Structures 102


Algorithm search_r(temp, string)
Algorithm search () {
{ Initialize f to 0;
Initialize flag=0; if(temp!=NULL)
Accept string to be searched ; {
flag=search_r(root,str); if(string =temp->data)
if(flag=1) return 1;

print found; if(string< temp->data)


f=search_r(temp->left, str);
else
if(string >temp->data)
print not found;
f=search_r(temp->right, str);
}
}
return f;
}

11/17/2021 Advanced Data Structures 103


Algorithm search_nr()
{
Initialize flag to 0;
temp=root;
Accept string to be searched;
while(flag=0)
{
if(string=temp->data)
{
flag=1; break;
}
else if(string<temp->data)
move temp to temp->left;
else
move temp to temp->right;
} //end while
if(flag=1)
Print found;
else
Print not found;
} //end algo

11/17/2021 Advanced Data Structures 104


Function DeleteItem
First, find the item; then, delete it

Important: binary search tree property must be preserved!!

We need to consider following different cases:


(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(4) Deleting the root node

11/17/2021 Advanced Data Structures 105


(1) Deleting a leaf

11/17/2021 Advanced Data Structures 106


Deletion - Leaf Case
Algorithm sets corresponding link of the parent to NULL and disposes the node

Delete(17) 10

5 15

2 9 20

7 17 30

11/17/2021 Advanced Data Structures 107


(2) Deleting a node with only one child

It this case, node is cut from the tree and algorithm links single child (with it's subtree) directly to the parent
of the removed node.

11/17/2021 Advanced Data Structures 108


Deletion - One Child Case
Delete(15) 10

5 15

2 9 20

7 30

11/17/2021 Advanced Data Structures 109


(3) Deleting a node with two
children (contd…)

Find inorder successor


◦ Go to the right child and then move to the left till we get NULL for the leftmost node

◦ To the inorder’s successor ,attach the left of the node which we want to delete
◦ Attach right sub tree of node to the parents right.

11/17/2021 Advanced Data Structures 110


if(curr==root) //deletion of root
{
if(curr->rightc==NULL)
root=root->leftc;
else if(curr->leftc==NULL)
root=root->rightc;
else if(curr->rightc!=NULL && curr->leftc!=NULL) 10
{
temp=curr->leftc;
root=curr->rightc; 5 15
s=curr->rightc;
while(s->leftc!=NULL)
{ 2 9 20
s=s->leftc;
}
s->leftc=temp;
7 17 30
}
}

11/17/2021 Advanced Data Structures 111


else if(curr!=root) //deletion of node which is not root
{
if(curr left and right is NULL ) //deletion of a leaf
{
if(parent->leftc==curr) 10
parent->leftc=NULL;
else
parent->rightc=NULL; 5 15
}

2 9 20

7 17 30

11/17/2021 Advanced Data Structures 112


else if(curr!=root) //deletion of node which is not root
{
if(curr left and right is NULL ) //deletion of a leaf
{
if(parent->leftc==curr)
parent->leftc=NULL;
else
parent->rightc=NULL;
}
else if(curr->leftc is NULL) //deletion of a single child 10
{
if(parent->leftc==curr)
5 15
parent->leftc=curr->rightc;
else
parent->rightc=curr->rightc; 2 9 20
}

7 17 30
11/17/2021 Advanced Data Structures 113
else if(curr!=root) //deletion of node which is not root
{ else if(curr->rightc is NULL) //deletion of a
if(curr left and right is NULL ) //deletion of a leaf single child
{ {
if(parent->leftc==curr) if(parent->leftc==curr)
parent->leftc=NULL; parent->leftc=curr->leftc;
else else
parent->rightc=NULL;
parent->rightc=curr->leftc;
}
} 10
else if(curr->leftc is NULL) //deletion of a single child
{
if(parent->leftc==curr)
5 15
parent->leftc=curr->rightc;
else
parent->rightc=curr->rightc; 2 9 20
}

7 17 30
11/17/2021 Advanced Data Structures 114
else //deletion of a node having two child
{
s=curr->rightc;
temp=curr->leftc;
while(s->leftc!=NULL)
{
s=s->leftc;
}
s->leftc=temp;
if(parent->leftc==curr)
10
parent->leftc=curr->rightc;
else 5 15
parent->rightc=curr->rightc;
}
} 2 9 20
Assign curr left and right to NULL;
delete curr;
} 7 17 30
11/17/2021 Advanced Data Structures 115
Assignment no 2
Implement dictionary using binary search tree where dictionary stores keywords & its meanings.
Perform following operations:
1. Insert a keyword
2. Delete a keyword
3. Create mirror image and display level wise
4. Copy

11/17/2021 Advanced Data Structures 116

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy