why when this fun delete a node it print a random number ? insted of the deleted node ?

0 votes
asked Oct 10, 2018 by hiba

void deletenode(node * root,int data){
        
     if(root==NULL)return;
      if(data>root->data){
          deletenode(root->right,data);
      }else if(data<root->data){
          deletenode(root->left,data);
      }
        else{
        node * todelete;
        todelete=root;
        if(root->left==NULL){
             root=root->right;
        }
         
        else if(root->right==NULL){
             root=root->left;
        }
        else{
            node * parent;
            parent=root;
            todelete=root->left;
            while(todelete->right!=NULL){
                parent=todelete;
                todelete=todelete->right;
            }
            root->data=todelete->data;
            if(parent==root){
                root->left=todelete->left;
            }else{
            parent->right=todelete->left;
            }
        }
        delete todelete;
        }
          
    }

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...