1

Ive been trying to delete specific values from my tree but instead of deleting them it sets them to 0. My implementation seems to be pretty standard compared to the other binary search trees on the web. All my other functions work fine except for my RemoveNode.

struct Node{
    int key;
    Node *right;
    Node *left;
    Node(int data){
        key=data;
        right=NULL;
        left=NULL;
    }
};
class BST{
private:
    Node *root;

    void AddLeafPrivate(int key,Node *&Ptr){
        if(Ptr==NULL){
            Ptr=createLeaf(key);
        }else if(key<Ptr->key){
            if(Ptr->left!=NULL){
                AddLeafPrivate(key,Ptr->left);
            }else
            {
                Ptr->left=createLeaf(key);
            }
        }
        else if(key>Ptr->key){
            if(Ptr->right!=NULL){
                AddLeafPrivate(key,Ptr->right);
            }else
            {
                Ptr->right=createLeaf(key);
            }
        }
    }
    void PrintInOrderPrivate(Node *Ptr){
    if(Ptr!=NULL){
        if(Ptr->left!=NULL){
            PrintInOrderPrivate(Ptr->left);
        }cout<<Ptr->key<<" ";
        if(Ptr->right!=NULL){
            PrintInOrderPrivate(Ptr->right);
        }

    }else{
        cout<<"tree is empty"<<endl;
    }
    }

    Node *findMin(Node *Ptr){
        if(Ptr->left!=NULL){
            findMin(Ptr->left);
        }else{
            return Ptr;
        }

    }

    void RemoveNodePrivate(int key,Node *parent){
        if(parent==NULL){
            cout<<"EMPTY TREE"<<endl;
            return;
    }else if(key<parent->key){
        RemoveNodePrivate(key,parent->left);
    }else if(key>parent->key){
        RemoveNodePrivate(key,parent->right);
    }else{
        if(parent->left==NULL&&parent->right==NULL){
            delete parent;
            parent=NULL;
        }else if(parent->left!=NULL&&parent->right==NULL){
            Node *temp=parent;
            parent=parent->left;
            delete temp;
            temp=NULL;

        }else if(parent->left==NULL&&parent->right!=NULL){
            Node *temp=parent;
            parent=parent->right;
            delete temp;
            temp=NULL;

        }else if(parent->left!=NULL&&parent->right!=NULL){
            Node *temp=findMin(root->right);
            root->key=temp->key;
            RemoveNodePrivate(temp->key,root->right);

        }
    }


    }

public:
    BST(){
        root=NULL;
    }


    Node *createLeaf(int key){
        Node *n=new Node(key);
        return n;
    }
    void AddLeaf(int key){
        AddLeafPrivate(key,root);
    }

    void PrintInOrder(){
        PrintInOrderPrivate(root);
    }

    void RemoveNode(int key){
        RemoveNodePrivate(key,root);
    }
};

int main()
{
BST tree;
tree.AddLeaf(5);
tree.AddLeaf(8);
tree.AddLeaf(12);
tree.AddLeaf(4);
tree.AddLeaf(3);
tree.AddLeaf(2);
tree.AddLeaf(7);
tree.RemoveNode(5);
tree.PrintInOrder();
cout<<endl;

}

I expect the output to be 2 3 4 7 8 12 but instead, it is 2 3 4 7 0 8 12

jasonv94
  • 11
  • 1
  • 1
    Take a closer look at `findMin`. Does it always return a value? – user4581301 Jul 20 '19 at 04:17
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – PaulMcKenzie Jul 20 '19 at 04:19
  • Hi should've clarified all options return 0 when using delete even if I do not use the findMin. – jasonv94 Jul 20 '19 at 04:23
  • 1
    The problem is that your code seems to correctly delete the node, but it doesn't adjust the pointers pointing at the deleted node. You would certainly do better with this `void RemoveNodePrivate(int key,Node *&parent){`. Now when you write `delete parent; parent = NULL;` it will delete the node and set the node pointing at the deleted node to NULL. Whether this is the only change I'm not sure, but you've got to realise that assigning NULL to a local variable has no effect at all on your tree. – john Jul 20 '19 at 05:50
  • 1
    In addition, a BST is a particular kind of tree that has constraints such that the keys are kept in sorted order. Removing a node will require preserving the remaining keys in sort order to preserve the tree as a BST. See, e.g. [Binary search tree - wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree) – David C. Rankin Jul 20 '19 at 06:07

0 Answers0