Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;在2-3树搜索中,布尔值始终错误地返回true_C++_Search_Tree_Boolean - Fatal编程技术网

C++ C++;在2-3树搜索中,布尔值始终错误地返回true

C++ C++;在2-3树搜索中,布尔值始终错误地返回true,c++,search,tree,boolean,C++,Search,Tree,Boolean,我真的在这家伙身上扯头发。问题出在这里。我已经硬编码了一个2-3树,并验证了它与使用一个按序遍历函数一起工作,该函数输出它当前所在节点的值。所以我知道这棵树是正确建造的 Node *r; Node zero,one,two,three,four,five,six,seven,eight,nine,ten; r = &zero; //Root zero.small = 50; zero.large = 90; zero.left = &one;

我真的在这家伙身上扯头发。问题出在这里。我已经硬编码了一个2-3树,并验证了它与使用一个按序遍历函数一起工作,该函数输出它当前所在节点的值。所以我知道这棵树是正确建造的

 Node *r;
  Node zero,one,two,three,four,five,six,seven,eight,nine,ten;
  r = &zero;

          //Root
 zero.small = 50;
 zero.large = 90;
 zero.left = &one;       //Child node to the left
 zero.middle = &four;    //Child node in the middle
 zero.right = &seven;    //Child node to the right

  //Left Tree
 one.small = 20;
 one.large = NULL;
 one.left = &two;
 one.middle = NULL;
 one.right = &three;

 two.small = 10;
 two.large = NULL;
 two.left = NULL;
 two.middle = NULL;
 two.right = NULL;

 three.small = 30;
 three.large = 40;
 three.left = NULL;
 three.middle = NULL;
 three.right = NULL;

  //Middle Tree
 four.small = 70;
 four.large = NULL;
 four.left = &five;
 four.middle = NULL;
 four.right = &six;

 five.small = 60;
 five.large = NULL;
 five.left = NULL;
 five.middle = NULL;
 five.right = NULL;

 six.small = 80;
 six.large = NULL;
 six.left = NULL;
 six.middle = NULL;
 six.right = NULL;

  //Right Tree
 seven.small = 120;
 seven.large = 150;
 seven.left = &eight;
 seven.middle = &nine;
 seven.right = &ten;

 eight.small = 100;
 eight.large = 110;
 eight.left = NULL;
 eight.middle = NULL;
 eight.right = NULL;

 nine.small = 130;
 nine.large = 140;
 nine.left = NULL;
 nine.middle = NULL;
 nine.right = NULL;

 ten.small = 160;
 ten.large = NULL;
 ten.left = NULL;
 ten.middle = NULL;
 ten.right = NULL;

 cout<<"inorder traversal for debug"<<endl;
 inOrder(*r);
Node*r;
节点零点,一、二、三、四、五、六、七、八、九、十;
r=&0;
//根
零。小=50;
零。大=90;
零。左=&1//左侧的子节点
0.middle=&4//中间的子节点
零。右=&7//右侧的子节点
//左树
1.小=20;
1.large=NULL;
1.left=&2;
1.middle=NULL;
一。右=&3;
2.小=10;
2.large=NULL;
2.left=NULL;
2.middle=NULL;
2.right=NULL;
3.小=30;
3.大=40;
3.left=NULL;
3.middle=NULL;
3.right=NULL;
//中间树
4.小=70;
四.大=零;
4.左=&5;
4.middle=NULL;
4.右=&6;
5.小=60;
5.大=零;
5.左=空;
5.middle=NULL;
5.right=NULL;
6.小=80;
六.大=零;
6.left=NULL;
6.middle=NULL;
6.right=NULL;
//右树
7.小=120;
7.大=150;
七。左=&八;
7.中间=&9;
7.右=&10;
8.小=100;
8.大=110;
8.left=NULL;
8.middle=NULL;
8.right=NULL;
9.小=130;
9.大=140;
九。左=空;
9.middle=NULL;
9.right=NULL;
10.小=160;
十大=零;
10.left=NULL;
10.middle=NULL;
10.right=NULL;

cout除非在
if(r.small==key)
分支中,否则永远不会返回值

从中,我想说您的代码应该首先将
键进行比较,并根据比较结果从
检索(*r.left/middle/right,key)
返回结果

这方面的东西(未经测试)

if(键
您需要首先检查当前节点中的密钥是小的还是大的,如果是,则返回true。如果不是,则需要递归地调用每个包含的节点上的retrieve,如果其中任何节点返回true,则返回true。如果函数尚未返回,则需要返回false。

您需要进行初始测试,以查看递归是否应停止,因为您至少是一个节点

// precondition: current is not 0
// returns: true or false. If true, location is set to the node 
// where it was found.
bool DoSearch(Node *current, int key, Node *location)
{
 /*
  * Is key in current?
  */
if (current->smallValue == key || (current->isThreeNode() 
       && current->largeValue == key)) {

    location = current;
    return true;

} else if ((current->isLeafNode())) {

    location = current;
    return false;
 /*
  *  Does current have two keys?
  */
} else if (current->isThreeNode()){

    if (key < current->smallValue) {

        DoSearch(key, current->leftChild, location);

    }  else if (key < current->largeValue) {

        DoSearch(key, current->middleChild, location);

    } else {

        DoSearch(key, current->rightChild, location);
    }

} else { // ...or only one?

     if (key < current->smallValue) {

        DoSearch(key, current->leftChild, location);

    }  else {

        DoSearch(key, current->rightChild, location);
    }
}
}
//前提条件:当前值不是0
//返回:true或false。如果为true,则将位置设置为节点
//找到它的地方。
布尔数据搜索(节点*当前,整数键,节点*位置)
{
/*
*钥匙在电流中吗?
*/
如果(当前->小值==键| |(当前->isThreeNode()
&&当前->大值==键){
位置=电流;
返回true;
}else if((当前->isLeafNode()){
位置=电流;
返回false;
/*
*电流有两把钥匙吗?
*/
}else if(当前->isThreeNode()){
如果(键<当前->小值){
DoSearch(键,当前->左子项,位置);
}否则如果(键<当前->大值){
DoSearch(键,当前->中间子项,位置);
}否则{
DoSearch(键,当前->右子项,位置);
}
}或者只有一个?
如果(键<当前->小值){
DoSearch(键,当前->左子项,位置);
}否则{
DoSearch(键,当前->右子项,位置);
}
}
}

您永远不会从
检索
返回false。这是真的。false应该从何而来?我认为这可能是问题所在,但我不确定在不过早退出树遍历的情况下将“return false”放置在何处。嗯。。稍后我将尝试实现它,我还注意到当
返回true时
,则代码>最终在my
中被命中,而不是退出函数,它只是在函数中不断递归。return不应该返回值并退出函数吗?@TylerDean它确实返回调用方,调用方可能是原始调用方,也可能只是另一个
retrieve()
,具体取决于递归的深度。因此,可能需要一些返回,直到你击中最外面的呼叫者。
if (retrieve(*r, key))
 {
     cout<<key<<" is found!"<<endl;
 }
 else
     cout<<key<<" is not found!"<<endl;
if (key < r.small)
    return retrieve(*r.small, key);

if (key == r.small)
    return TRUE;

if (r.right == NULL)
    return retrieve(*r.middle, key);

if (key < r.large)
    return retrieve(*r.middle, key);

if (key == r.large)
    return TRUE;

return retrieve(*r.right, key);
// precondition: current is not 0
// returns: true or false. If true, location is set to the node 
// where it was found.
bool DoSearch(Node *current, int key, Node *location)
{
 /*
  * Is key in current?
  */
if (current->smallValue == key || (current->isThreeNode() 
       && current->largeValue == key)) {

    location = current;
    return true;

} else if ((current->isLeafNode())) {

    location = current;
    return false;
 /*
  *  Does current have two keys?
  */
} else if (current->isThreeNode()){

    if (key < current->smallValue) {

        DoSearch(key, current->leftChild, location);

    }  else if (key < current->largeValue) {

        DoSearch(key, current->middleChild, location);

    } else {

        DoSearch(key, current->rightChild, location);
    }

} else { // ...or only one?

     if (key < current->smallValue) {

        DoSearch(key, current->leftChild, location);

    }  else {

        DoSearch(key, current->rightChild, location);
    }
}
}