Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.tech-faq.com/binary-tree-deleting-a-node.html 1/6
4/27/2018 Binary Tree – Deleting a Node
Since the node has both, right and left child, if right sub-tree is opted find the smallest node. If left sub-tree is opted
then find the largest node.
Since node->right = node->left = NULL, delete the node and place NULL in the parent node.
http://www.tech-faq.com/binary-tree-deleting-a-node.html 2/6
4/27/2018 Binary Tree – Deleting a Node
t à right
set t = t à right
If the node to be removed has two children, the general strategy is to replace the data of this node with the smallest
data of the right sub-tree. Then the node with the smallest data is removed (this case is easy since this node cannot
have two children).
http://www.tech-faq.com/binary-tree-deleting-a-node.html 3/6
4/27/2018 Binary Tree – Deleting a Node
http://www.tech-faq.com/binary-tree-deleting-a-node.html 4/6
4/27/2018 Binary Tree – Deleting a Node
return;
}
if(location->lchild==NULL && location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/
void find(int item,node **par,node **loc)
{
node *ptr,*ptrsave;
if(root==NULL) /*tree empty*/
{
*loc=NULL;
*par=NULL;
return;
}
while(ptr!=NULL)
{
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
http://www.tech-faq.com/binary-tree-deleting-a-node.html 5/6
4/27/2018 Binary Tree – Deleting a Node
http://www.tech-faq.com/binary-tree-deleting-a-node.html 6/6