A bug in AVL tree deletion !!! Help pls !!

Discussion in 'Meet and Greet' started by swizzy, Sep 28, 2011.

  1. swizzy

    swizzy New Member

    Joined:
    Sep 28, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Watz wrong with this avl tree deletion code ??

    A node with a single child alone gets deleted..

    But then for other nodes the program stops working and terminates abruptly as such..

    i.e. abnormal termination as error report while such nodes are entered to be deleted..

    Cud someone help me out ??

    Cudn find out wy such error reports are popped !!

    Here is my c++ code for avl deletion..


    Code:
    void remove(const comparable & x,avlnode* & r)
            {
                       if(r==NULL)
                                  cout<<"\nNo element !!!";
                       else if(x<r->element)
                       {
                                  remove(x,r->left);
                                  if(avlheight(r->right)-avlheight(r->left)>1)
                                         if(avlheight(r->right->right)>=avlheight(r->right->left))
                                                     rotatewithright(r);
                                         else
                                                     doublewithright(r);
                       }
                       else if(x>r->element)
                       {
                                  remove(x,r->right);
                                  if(avlheight(r->left)-avlheight(r->right)>1)
                                         if(avlheight(r->left->left)>=avlheight(r->left->right))
                                                     rotatewithleft(r);
                                         else
                                                     doublewithleft(r);
                       }
                       else if(r->left!=NULL&&r->right!=NULL)
                       {
                            r->element=findmin(r->right)->element;
                            remove(r->element,r->right);
                       }
                       else
                       {
                           avlnode *oldnode=r;
                           r=(r->left!=NULL)?r->left:r->right;
                           delete oldnode;
                       }
                       r->height=max(avlheight(r->left),avlheight(r->right))+1;
                  }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice