在二叉树中插入4或5个数字,但输出中只有3个数字

Inserting 4 or 5 numbers in binary tree but getting just 3 numbers in the output

本文关键字:数字 输出 3个 5个 二叉树 插入      更新时间:2023-10-16

这是学校处理递归和二叉树的实验的一部分。如果我插入4或5个数字并输出结果,我只得到3个数字。下面是insert的代码:

Node *insert(Node *t, int key) {
    Node *insertParent;
    Node *result=NULL;
    if (t!=NULL) {
        result=search(t,key,insertParent);
    } else {
        t=new Node;
        t->data=key;
        t->leftchild=NULL;
        t->rightchild=NULL;
        return t;
    }
    if (result==NULL) {
        if (insertParent->data>key) {
            insertParent->leftchild=new Node;
            insertParent->leftchild->data=key;
            insertParent->leftchild->leftchild=NULL;
            insertParent->leftchild->rightchild=NULL;
            return insertParent->leftchild;
        } else if (insertParent->data<key) {
            insertParent->rightchild=new Node;
            insertParent->rightchild->data=key;
            insertParent->rightchild->leftchild=NULL;
            insertParent->rightchild->rightchild=NULL;
            return insertParent->rightchild;
        }
    } else
        return NULL;
}

但是我认为问题是在搜索函数中,特别是通过引用父节点指针:

Node* search(Node *t, int key, Node *&parent) {
    if (t!=NULL) {
        parent=t;
        if (t->data==key)
            return t;
        else if (t->data>key)
            return search(t->leftchild,key,t);
        else 
            return search(t->rightchild,key,t);
    } else
        return NULL;
}

我有一个输出树的函数,并根据我手动构建的树检查了它,它工作得很好:

void inorder(Node *t)
{
    if (t!=NULL) {
        if (t->leftchild!=NULL)
            inorder(t->leftchild);
        cout << t->data << ", ";
        if (t->rightchild!=NULL)
            inorder(t->rightchild);                     
    }  
}

不是在寻找答案,只是在寻找我应该关注的领域。

你的怀疑是对的。跟踪在深度搜索多个节点时,顶级'parent'参数是如何更新的。

Node* search(Node *t, int key, Node *&parent)
{
    if(t!=NULL)
    {
    parent=t;
    if (t->data==key)
        return t;
    else if (t->data>key)
        return search(t->leftchild, key, parent); // use “parent” because you want to assign parent to this variable
    else 
        return search(t->rightchild,key, parent);
    }
    else     return NULL;
}

所以我发现我的问题是与搜索功能。它确实与引用父节点变量有关。我必须使用四个else/ifelse语句来决定走哪条路,递归还是不递归。

Node* search(Node *t, int key, Node *&parent) {
    if (t!=NULL) {
        if (t->data==key) {
            parent=NULL;
            return t;
        } else if (t->data>key && t->leftchild!=NULL) {
            return search(t->leftchild,key,parent); // think this needs returned
        } else if (t->data>key && t->leftchild==NULL) {
            parent=t;
            return NULL;
        } else if (t->data<key && t->rightchild!=NULL) {
            return search(t->rightchild,key,parent); //think this needs returned
        } else if (t->data<key && t->rightchild==NULL) {
            parent=t;
            return NULL;
        }
    } else {
        parent=NULL;
        return NULL;
    }
}

搜索函数的改变需要对插入函数进行改变:

Node *insert(Node *t, int key) {
    Node *insertParent=NULL;
    Node *result=NULL;
    if (t!=NULL) {
        result=search(t,key,insertParent);
    } else {
        t=new Node;
        t->data=key;
        t->leftchild=NULL;
        t->rightchild=NULL;
        return t;  
    }
    if (insertParent==NULL && result!=NULL) {
        return NULL;
    } else if (insertParent!=NULL && result==NULL) {
        //key not found need to add
        if (insertParent->data>key) {
            insertParent->leftchild=new Node;
            insertParent->leftchild->data=key;
            insertParent->leftchild->leftchild=NULL;
            insertParent->leftchild->rightchild=NULL;
            return insertParent->leftchild;
        } else {
            insertParent->rightchild=new Node;
            insertParent->rightchild->data=key;
            insertParent->rightchild->leftchild=NULL;
            insertParent->rightchild->rightchild=NULL;
            return insertParent->rightchild;
        }
    }
}

感谢所有帮助过我的人……

我也回答了我自己的问题。这是我应该做的还是我应该编辑我的帖子?我认为人们更愿意看到整个过程,而不是让我删除旧的不工作的代码…)