删除二叉树中的节点我哪里出错了?

Where am i going wrong for deleting a Node in a Binary Tree?

我目前是 OOPS 的新手,正在尝试为基本的二叉树实现编写 class。当我尝试删除一个节点时,中序遍历无法正常工作。

下面是我当前的代码。我还有一些 couts 来记录一些事情。

    #include<bits/stdc++.h>
    using namespace std;

    class Node {

public :
    int data;
    Node *left, *right;

public :
    Node(int x){
        data=x;
        left=right=NULL;
    }
    // ~Node(); 


    void inorder(Node *temp){
        if(!temp)
            return ;

        // cout<<"Sent :"<<temp->data<<"\n";

        inorder(temp->left);
        cout<<temp->data<<" "<<temp<<"\n";
        inorder(temp->right);
    }


    void insert(Node *temp,int x){

        queue<Node*> q;
        q.push(temp);

        while(!q.empty()){
            Node *t=q.front();
            q.pop();

            if(!t->left){
                t->left=new Node(x);
                break;
            }
            else
                q.push(t->left);

            if(!t->right){
                t->right=new Node(x);
                break;
            }
            else
                q.push(t->right);
        }
    }


    void deleteDepest(Node *temp, Node *delNode){

        queue<Node*> q;
        q.push(temp);

        while(!q.empty()){
            Node *t=q.front();
            q.pop();

            if(t==delNode){
                t=NULL;
                delete(delNode);
                return;
            }

            if(t->left){
                if(t->left==delNode){
                    t->left=NULL;
                    delete(delNode);
                    return;
                }
                else
                q.push(t->left);
            }
            
            if(t->right){
                if(t->right==delNode){
                    t->left=NULL;
                    delete(delNode);
                    return;
                }
                else
                q.push(t->right);
            }
        }
    }


    Node* deletion(Node *temp, int x){

        if(temp==NULL)
            return NULL;

        if(!temp->left && !temp->right){
            if(temp->data==x)
                return NULL;
            return temp;
        }

        queue<Node*> q;
        q.push(temp);
        Node *tt=NULL, *t;

        while(!q.empty()){
            t= q.front();
            q.pop();

            if(t->data==x){
                tt=t;
                // break;
            }

            if(t->left)
                q.push(t->left);
            if(t->right)
                q.push(t->right);
        }

        if(tt){
            cout<<"\ndelNode "<<tt->data<<" depest "<<t->data<<"\n";
            tt->data=t->data;
            deleteDepest(temp,t);
        }

        return temp;
    }
  };


   int main()
  {

Node *root = new Node(1);

root->left = new Node(2);
root->right = new Node(3);

// cout<<"Before insertion\n";
// root->inorder(root);

root->insert(root,4);
root->insert(root,5);

// cout<<"\nAfter insertion\n";
// root->inorder(root);

int d;
cout<<"\nEnter nod to delete :";
cin>>d;
root=root->deletion(root,d);
cout<<"\nAfter deletion\n";
cout<<"root :"<<root<<" "<<root->left<<"\n";
root->inorder(root);

cout<<"Ain't working";
cout<<root;

if(root->left->left)
    cout<<"\nroot->left->left :"<<root->left->left;
else
    cout<<"NULL\n";

return 0;
}

即使我取消对 ~Node() 的注释,它也会显示错误:undefined reference to `Node::~Node()'
即使 运行 您可以看到的代码 不工作 打印语句甚至没有被执行。

if(t->right==delNode){
  t->left=NULL;
  delete(delNode);

您在 right 上找到要删除的节点,但将 left 指针设置为 NULL