在二叉树中插入4或5个数字,但输出中只有3个数字
Inserting 4 or 5 numbers in binary tree but getting just 3 numbers in the output
这是学校处理递归和二叉树的实验的一部分。如果我插入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;
}
}
}
感谢所有帮助过我的人……
我也回答了我自己的问题。这是我应该做的还是我应该编辑我的帖子?我认为人们更愿意看到整个过程,而不是让我删除旧的不工作的代码…)
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何将输出数字划分为奇数和偶数
- 输出错误,问题是找到总和5000位数字cpp
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 以C++输出一个数字三角形
- C++输出奇怪的字符而不是数字 (Windows)
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- 反向输出数字 - c++
- 为什么当我输入一个大数字时,输出会一遍又一遍地重复?
- 未来值公式显示疯狂巨大数字的输出
- 输出一个数字,该数字可能是三种类型之一
- 预期输出之后的所有这些数字是多少
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 素数检查 C++ 函数输出非素数的数字
- 如何将数字输出到文件中,所有数字都具有相同的精度
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- C++ 如何从输入字母或数字输出字母或数字
- gcc 预处理的表单哈希符号 + 数字"# 1"输出行是什么意思?
- C++ 意外的数字输出
- 数据将奇怪的数字输出到文件,MAP C++