C++ 二叉树搜索未返回预期值
如果我有一个结构:C++ 二叉树搜索未返回预期值,c++,binary-tree,binary-search-tree,binary-search,C++,Binary Tree,Binary Search Tree,Binary Search,如果我有一个结构: struct node{ int key_value; node * p_left; node * p_right; }; 和添加功能: node* add(node * p_tree, int key) { //--The base case of the recursive function will be placed in here //--since binary trees are recursive in nature and linked
struct node{
int key_value;
node * p_left;
node * p_right;
};
和添加功能:
node* add(node * p_tree, int key) {
//--The base case of the recursive function will be placed in here
//--since binary trees are recursive in nature and linked data structures
//--are as a whole in terms of space and memory, the recursive function will
//--suffice for most cases involving binary trees.
//--In this case, if the given parameter is null, we create the tree
//--by allocating the necessary memory space
if (p_tree == NULL) {
node * pnew_tree = new node;
pnew_tree->p_left = NULL;
pnew_tree->p_right = NULL;
pnew_tree->key_value = key;
cout << "Added node: " << pnew_tree->key_value << endl;
return pnew_tree;
}// end of base case
//--Depending of the value of the node, we determine if we will add to the left side or the right side of the subtree
if (key < p_tree->key_value){
// if it is less than the value, we add to the left
p_tree->p_left = add(p_tree->p_left, key);
}
else{
p_tree->p_right = add(p_tree->p_right, key);
}
return p_tree;
} // end of function
node* search(node *p_tree, int key) {
//--First:
if (p_tree != NULL) {
if(key == p_tree->key_value){
cout << "Node found" << endl;
return p_tree;
}
if(key < p_tree->key_value){
return search(p_tree->p_left, key);
}
else{
return search(p_tree->p_right, key);
}
}
else{
return NULL;
}
}//--End of recursive search function
以及搜索功能:
node* add(node * p_tree, int key) {
//--The base case of the recursive function will be placed in here
//--since binary trees are recursive in nature and linked data structures
//--are as a whole in terms of space and memory, the recursive function will
//--suffice for most cases involving binary trees.
//--In this case, if the given parameter is null, we create the tree
//--by allocating the necessary memory space
if (p_tree == NULL) {
node * pnew_tree = new node;
pnew_tree->p_left = NULL;
pnew_tree->p_right = NULL;
pnew_tree->key_value = key;
cout << "Added node: " << pnew_tree->key_value << endl;
return pnew_tree;
}// end of base case
//--Depending of the value of the node, we determine if we will add to the left side or the right side of the subtree
if (key < p_tree->key_value){
// if it is less than the value, we add to the left
p_tree->p_left = add(p_tree->p_left, key);
}
else{
p_tree->p_right = add(p_tree->p_right, key);
}
return p_tree;
} // end of function
node* search(node *p_tree, int key) {
//--First:
if (p_tree != NULL) {
if(key == p_tree->key_value){
cout << "Node found" << endl;
return p_tree;
}
if(key < p_tree->key_value){
return search(p_tree->p_left, key);
}
else{
return search(p_tree->p_right, key);
}
}
else{
return NULL;
}
}//--End of recursive search function
为什么当我跑步时:
add(myBinaryTree,1);
cout << "Testing to see if it is there" << endl;
if (search(myBinaryTree,1) == NULL {
cout << "Node not found" << endl;
}
是否未找到输出节点而不是找到节点?
据我所知,add函数不返回NULL,为什么会这样?
我曾尝试研究过类似的问题,但无法充分理解其中的代码以提出自己的解决方案,我也不太熟悉使用IDEcodeblocks进行调试,因此不知道该去哪里。
我只需要一个逻辑修复,因为我自己似乎找不到一个函数add返回一个指向二叉树根的指针。通常这与函数参数p_tree是同一个指针,因为二叉树的根永远不会改变
但是在空树p_tree==NULL的情况下,add将返回指向新创建的树根的指针。因此,必须更新变量myBinaryTree。执行后
node* myBinaryTree = NULL;
add(myBinaryTree,1);
变量myBinaryTree的值仍然为NULL。您尚未将其更新到树的根目录。以下代码起作用:
node* myBinaryTree = NULL;
myBinaryTree = add(myBinaryTree,1);
cout << "Testing to see if it is there" << endl;
if (search(myBinaryTree,1) == NULL) {
cout << "Node not found" << endl;
}
在这里发布之前,您的第一步是在调试器中运行它。为什么您的add函数返回指针?我希望add或insert函数将节点添加到列表中;不需要返回值。需要更多的代码来澄清树是如何实现的。@KevinDTimm如果不是因为我对调试一无所知,那就太好了,如果我知道如何实现,我会很高兴实现上述解决方案,也不会在这里询问,但我目前没有时间,需要想出一个快速的解决方案。@ AlxyAdL04:考虑程序是学习调试器的第一步。如果需要快速解决方案,请使用std::map;或者一个外部库,因为它们已经被调试过了,可以节省宝贵的开发时间来学习调试器。要解决这个问题,您应该发布myBinaryTree声明,包括所有分配。如果myBinaryTree的成员没有正确初始化,我不会感到惊讶@AlxyAdL04:你使用C++,而不是C,所以我强烈建议在构造函数中初始化结构的成员。我强烈建议您学习如何使用调试器!对于这样的代码,您可以在不到20秒的时间内正常解决此类问题。明白了!我非常感谢帮助我解决这个问题的努力。我的假设是,我可以调用该函数,并且不必将其作为赋值传递,因为它正在使用指针,因此认为这本身就足以在节点上工作。很容易修复,现在效果很好,谢谢。