del(int item) { struct node *parent,*location; if(root==NULL) { printf("\ntree empty\n"); return; } find(item,&parent,&location); if(location==NULL) { printf("\nitem is not present in tree\n"); 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); printf("\ndeleted\n"); }
void BinTree::del(int key) { tree_node *parent, *location; if (root == NULL) { std::cout << "Tree empty" << std::endl; return; } find(key, &parent, &location); if (location == NULL) { std::cout << "Item not present in tree" << std::endl; return; } if (location->left == NULL && location->right == NULL) { case_a(parent, location); } if (location->left != NULL && location->right == NULL) { case_b(parent, location); } if (location->left == NULL && location->right != NULL) { case_b(parent, location); } if (location->left != NULL && location->right != NULL) { case_c(parent, location); } delete location; }
del(int item) { struct node *parent,*location; if(root==NULL) { printf("Tree empty"); return; } find(item,&parent,&location); if(location==NULL) { printf("Item not present in tree"); 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()*/
case_c(struct node *par,struct node *loc) { struct node *parsuc,*suc; parsuc=loc; suc=loc->rchild; while(suc->lchild!=NULL) { parsuc=suc; suc=suc->lchild; } if((suc->lchild==NULL)&&(suc->rchild==NULL)) case_a(parsuc,suc); else case_b(parsuc,suc); if(par==NULL) root=suc; else { if(loc==par->lchild) par->lchild=suc; else par->rchild=suc; } suc->lchild=loc->lchild; suc->rchild=loc->rchild; }
void BinTree::case_c(tree_node *par, tree_node *loc) { tree_node *ptr, *ptrsave, *suc, *parsuc; ptrsave = loc; ptr = loc->right; while (ptr->left != NULL) { ptrsave = ptr; ptr = ptr->left; } suc = ptr; parsuc = ptrsave; if (suc->left == NULL && suc->right == NULL) { case_a(parsuc, suc); } else { case_b(parsuc, suc); } if (par == NULL) { root = suc; } else { if (loc == par->left) { par->left = suc; } else { par->right = suc; } } suc->left = loc->left; suc->right = loc->right; }
case_c(struct node *par,struct node *loc) { struct node *ptr,*ptrsave,*suc,*parsuc; /*Find inorder successor and its parent*/ ptrsave=loc; ptr=loc->rchild; while(ptr->lchild!=NULL) { ptrsave=ptr; ptr=ptr->lchild; } suc=ptr; parsuc=ptrsave; if(suc->lchild==NULL && suc->rchild==NULL) case_a(parsuc,suc); else case_b(parsuc,suc); if(par==NULL) /*if item to be deleted is root node */ root=suc; else if(loc==par->lchild) par->lchild=suc; else par->rchild=suc; suc->lchild=loc->lchild; suc->rchild=loc->rchild; }/*End of case_c()*/
void del(int item) { if(head==NULL) { printf("Tree empty"); return; } find(item); if(Loc==NULL) { printf("Item not present in tree"); return; } if(Loc->left==thread && Loc->right==thread) // for zero child case_a(head,Par,Loc); if(Loc->left==link && Loc->right==thread) // for one left child case_b(head,Par,Loc); if(Loc->left==thread && Loc->right==link) // for one right child case_b(head,Par,Loc); if(Loc->left==link && Loc->right==link) // for two child left and right case_c(head,Par,Loc); }
case_c( node *head,node *Par,node *Loc) { node *ptr,*ptrsave,*suc,*parsuc; /*Find inorder successor and its parent*/ ptrsave=Loc; ptr=Loc->right_ptr; while(ptr->left==link) { ptrsave=ptr; ptr=ptr->left_ptr; } suc=ptr; parsuc=ptrsave; Loc->data= suc->data; if(suc->left==thread && suc->right==thread) case_a(head,parsuc,suc); else case_b(head,parsuc,suc); }