Binary Search Tree
Binary Search Tree
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
◦ Binary search trees provide an excellent structure for searching a list and at the same time for
inserting and deleting data into the list.
else f=search_r(te
mp->left, str);
print not
} if(string >temp-
found;
>data)
} f=search_r(te
return f;
mp->right, str);
}
Delete(17) 10
5 15
2 9 20
7 17 30
5 15
2 9 20
7 30
◦ To the inorder’s successor ,attach the left of the node which we want to delete
>leftc!=NULL)
{
else if(curr!=root) //deletion of node which is not root
{ else if(curr->rightc is NULL) //deletion of
if(curr left and right is NULL ) //deletion of a
a
leaf single child
{ if(parent->leftc==curr) { if(parent->leftc==curr)
parent->leftc=NULL; parent->leftc=curr-
else >leftc; else
parent-
parent->rightc=curr-
} >rightc=NULL;
} >leftc;
else if(curr->leftc is NULL) //deletion of a single child
{
if(parent->leftc==curr)
parent->leftc=curr->rightc;
else
parent->rightc=curr-
>rightc;
}
20-MAR-19 DATA STRUCTURE-II 16
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)
parent->leftc=curr->rightc;
else
parent->rightc=curr->rightc;
}
}
Assign curr left and right to NULL;
delete curr;
}
20-MAR-19 DATA STRUCTURE-II 17