【问题标题】:Where am i going wrong for deleting a Node in a Binary Tree?删除二叉树中的节点我哪里出错了?
【发布时间】:2020-12-24 14:05:58
【问题描述】:

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

以下是我当前的代码。我也有一些 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() 它也会显示错误:未定义对 `Node::~Node()' 的引用
即使在运行代码时你也可以看到Ain't working print 语句甚至没有被执行。

【问题讨论】:

  • 无论哪本 C++ 教科书指示您在代码中编写 #include &lt;bits/stdc++.h&gt;,您都应该把它扔掉,给自己找一本更好的教科书。附言插入和删除逻辑似乎很糟糕。事实上,如果它最初是NULL,它似乎总是插入到节点的左侧。这就是逻辑所显示的。但是如果要插入的值大于节点的值,但是left指针为空,需要在右边插入值。那应该如何工作?为什么插入需要队列?

标签: c++ oop binary-tree destructor delete-operator


【解决方案1】:
if(t->right==delNode){
  t->left=NULL;
  delete(delNode);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多