Recursion 测试二叉树的成员

Recursion 测试二叉树的成员,recursion,binary-tree,member,Recursion,Binary Tree,Member,我目前正在尝试遍历二叉树,以检查某些值是否在。 for循环正在测试从1到50的所有值,并将为每个匹配的值返回true 以下是当前树: 8 / \ 4 38 / \ / \ 3 7 31 39 / / \ \ 1 16 33 45 IntegerData test(0); for (int i =

我目前正在尝试遍历二叉树,以检查某些值是否在。 for循环正在测试从1到50的所有值,并将为每个匹配的值返回true

以下是当前树:

              8
            /    \
           4      38
          / \    /  \
         3  7   31  39
        /      / \   \
       1     16  33  45

IntegerData test(0);
for (int i = 0; i < 50; i++) {
  test.value = i;
  if (bt->member(&test)) {
   cout << "member true for " << i << endl;
  }
}
前面所述的for循环将打印出来

member true for 8
member true for 4
member true for 38
但没有别的


有人会通过伪代码或脚本提供一些指导吗。你不必给我密码,因为我想自己弄清楚。谢谢。

这是一个不可靠的树遍历算法实现。但是如果你想保留你所拥有的,请考虑将返回语句的位置与下面的语句进行切换。 然而,这就是我重构代码的方式

bool member(BinaryTreeNode *node, Data *data) {
   // Create wrapper for data
   BinaryTreeNode *newNode = new BinaryTreeNode(data);

   if (data->compareTo(node->nodeData) == 0) { // Check the current node only!
       return true;
   else  // delegate the rest of the tree to recursion.
       if (member(node->left,  data)) // Check the left
       || (member(this->right, data)) // Check the right
           return true;
       else
         return false;
}
然后再次,你应该测试这个,让我知道它是否有效。我来自露比,我还没有在C++编程过一段时间。
哦,当你想在驱动程序代码中调用它时,把二叉树的根节点作为第一个节点传递。如果这是类或模块函数,请尝试传递
self
。我希望
self
指向
根节点

假设这是一个有序的BST:

bfs(root) {
    if (root is null) 
        return false // if we get to leaves without finding the target, it's not there
    if (root is target value)
        return true // if we found it, then YAY
    if (root is less than target value)
        return bfs(right child) // if target > root, target must lie in right subtree
    return bfs(left child) // otherwise target < root, check left subtree
bfs(根目录){
if(根为空)
return false//如果我们在没有找到目标的情况下到达树叶,它就不在那里
if(根是目标值)
返回true//如果我们找到它,那么
如果(根值小于目标值)
返回bfs(右子树)//如果target>root,则目标必须位于右子树中
返回bfs(左子树)//否则目标<根,检查左子树
您只想检查根目录下的值是否为目标值。如果检查子目录,您将检查其中的一些内容两次(一次是子目录,一次是根目录)。这也会使代码更加复杂,这可能是导致逻辑错误的原因


编辑:刚刚找到算法的解释

编辑:我还想强调一下你犯的一个小错误,我看到很多人都犯过这个错误——调用一个返回值的函数,但不使用返回值执行任何操作


我所说的行是
newNode->member(data);
行。您已经调用了递归中的下一步,但尚未使用结果。这意味着您的程序在树中找到较低的目标后不会回溯。若要在递归函数中使用返回值,必须具有
return next\u call()
(在本例中,
返回新节点->成员(数据);
)。

您的意思是:新节点->成员(数据);return true;?我只是尝试了一下,它没有改变任何东西。还有其他想法吗?你可能会有一些不稳定的行为,因为如果找不到元素,没有显式调用返回false。我解决了这个问题。我尝试以一种与当前代码相关的方式实现它,但它对我无效。不过谢谢你的帮助。这会吗总是像你的例子那样是一个有序的BST吗?很好,那么我就不必修改我的答案了!这很聪明。我没有考虑到我的顺序。根据你上次的编辑,他必须在返回
下一次调用()之前找到一种方法将
newNode
设置为正确的方向
。按照控制流的设置方式,代码可能最终看起来像
if(newNode=newNode->right&&newNode->member(data))
。假设第一个方面应该总是以某种
true
形式进行计算。如果这部分代码有问题,则很难调试。我不完全确定你在说什么。询问者在返回下一个调用之前,一定要将newNode设置为正确的方向,以避免搜索冗余部分另外,“newNode=newNode->right”在BST中不应该为真(节点不应该是它们自己的子节点)。我也不知道你所说的“方面”是什么意思。这是Ruby的东西吗?我已经找到了答案。事实证明,我的成员代码工作正常,但我的插入代码有错误。我将把注意力转移到这一点上。感谢你们两位花时间帮助我。我非常感谢。的确,我做了必要的更改,以便它能够正常工作正确定位
bfs(root) {
    if (root is null) 
        return false // if we get to leaves without finding the target, it's not there
    if (root is target value)
        return true // if we found it, then YAY
    if (root is less than target value)
        return bfs(right child) // if target > root, target must lie in right subtree
    return bfs(left child) // otherwise target < root, check left subtree