Deletion in binary search tree

Saqib_Awan 391 views 10 slides Jan 28, 2017
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

A simple way of delete a node from binary search tree
with the help of an example
Three possible cases also described in these slides
If any question about these slides then please ask in comments


Slide Content

Deletion

* Deletion is simple if we are deleting a leaf
node

+ Three cases

pd

LeopardROtk

Deletion

+ If the element to be deletedis a leaf node

yo
7
q 1

LeopärdRrök

Deletion

+ If the element to be deleted has one child

\

Deletion

+ If the element to be deleted has one child

@ (3) de
>
14]

ff

LeopärdRöck

Deletion

+ If the element to be deleted has both children

y

)

x
Le

Deletion

+ If the element to be deleted has both children

EN
»
e O \ á

4

LeoparoReck

Code

struct BSTNode* Delete(struct BSTNode *root, int data)

{

struct BSTNode *temp;

if(root==NULL)
printf(“No such element exists”);

else if(data<root->data)
root->left=Delete(root->left,data);

else if(data> root->data)
root->right=Delete(root->right,data);

Clipboard Image Tools Brushes) Shapes Size

struct BSTNode* Delete(stri

{

|

struct BSTNode *temp; 0

if(root==NULL) | | ©)
printf(“No such elem |

else if(data<root->data) |
root->left=Delete(roo||

else if(data> root->data)

root->right=Delete(r

Code

else { //element found

if{root->left && root->right) { //both children
temp=FindMax(root->left);
root->data=temp->data;

root->left=Delete(root->left,root->data);

}
else{ //one or none child
temp=root;
if(root->left==NULL)
root=root->right;
if(root->right==NULL)
root=root->left;
free(temp);
}
} //end of else
return(root);

} //end of function



== an M