(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
Â
CS8391-Data Structures Unit 3
1. Binary Search Trees
Definition:
Binary search tree is a binary tree in
which the key values in the left node is
less than the root and the key values in
the right node is greater than the root.
3. insert
⢠To insert the element X into the tree
â Check with the root node T
â If it is less than the root
⢠Traverse the left sub tree recursively until it reaches
The Tď Left ==NULL.
⢠Then X is placed in Tď Left
â If X is greater than the root
⢠Traverse the right sub tree recursively until it reaches
The Tď rRght ==NULL.
⢠Then X is placed in Tď Right
4.
5. Void insert(int X, Tree T)
{
If(T==NULL)
{
T=malloc(sizeof(struct TreeNode));
if(T!=NULL)
{
Tď Element= X
Tď Left = NULL
Tď Right =NULL
}
}
else if (X<Tď Element)
Tď left=insert(X,Tď left)
Else if(X>Tď Element)
Tď right=insert(X,Tď right)
}
6. Find
â Check the value X with the root node value
⢠If X is equal to Tď data, return T
⢠If X is less than Tď data, traverse the left of T
recursively
⢠If X is greater than Tď data, traverse the right of T
recursively
7. Find
void find(int x, tree t)
{
if(T==NULL)
return NULL
if (X<Tď element)
return find(X,Tď left)
else if(X>Tď Element)
return find(X,Tď right)
else
return T
}
8. Findmin
â start from root go left as long as there is left child.
â Returns the position of the smallest element in
the tree
12. CASE 1 â node to be
deleted is a leaf node /
No children
⢠If the node is a leaf node,
it can be deleted
immediately
13. CASE 2 â Node with one
child
⢠If the node has one
child
â It can be deleted by
adjusting its parent
pointer that points to its
child node.
14. CASE 3: node with two
children
Replace the data of the node
to be deleted with its
smallest data of the right
sub tree and recursively
delete that node
15. void delete(int X, Tree T)
{
if(T==NULL)
Error âNo tree valueâ;
else if (X<T-->Element)
T--> Left = Delete(X,T--> Left)
else
if(X> T> T--> Element)
T--> Right = Delete(X, T--> Right)
else
if(T--> Left && T--> Right)
{
Tmpcell = FindMin (T-->Right)
T-->Element = Tmpcell-->Element
T-->Right = Delete(T-->Element; T-->Right)
}