简体   繁体   English

二叉树搜索没有返回结果(C ++)

[英]Binary Tree search returns no results (C++)

I am working on some binary tree algorithms and need a "find node with searchindex..." function. 我正在研究一些二叉树算法,需要一个“使用searchindex ...查找节点”功能。 The design for treenodes is basically treenodes的设计基本上是

class TreeNode {
  int index; // some identifier
  TreeNode *left;
  TreeNode *right;
}

and a tree is defined by a pointer to the root-node. 并且树由指向根节点的指针定义。

My implementation for the search function is: 我对搜索功能的实现是:

void Tree::searchNode(TreeNode * root, int nodeIndex, TreeNode *resultNode){
/* Recursive search */

  if (root->index == nodeIndex) {
            resultNode = root;
  } else {

    /*  search children if the current node is not a leaf */

    if(!root->isLeaf()) {
        this->searchNode(root->left,nodeIndex,resultNode);
        this->searchNode(root->right,nodeIndex,resultNode);
        }
  }
}

Arguments: * root is the root-node of the tree, nodeIndex is the search-index and * resultNode is the pointer to the found (or not) node in the tree. 参数: * root是树的根节点, nodeIndex是search-index,* resultNode是指向树中找到(或不是)节点的指针。

The function does not return a reference or pointer to the found node but modifies the pointer resultNode so it points to the found node. 该函数不返回指向找到的节点的引用或指针,但修改指针resultNode ,使其指向找到的节点。 The idea is to initialize resultNode with NULL, perform the search and modify it if a match occurs. 我们的想法是使用NULL初始化resultNode,执行搜索并在匹配发生时修改它。 Otherwise it remains NULL and I can easily check if there are search results or not. 否则它仍然是NULL,我可以很容易地检查是否有搜索结果。

Another class with a tree buildingTree as member utilizes the search-function in this way: 具有树构建树的另一个类作为成员以这种方式利用搜索功能:

TreeNode *resultNodePtr = NULL;
this->buildingTree->searchNode(this->buildingTree->rootPtr, 
                               currentNodeIndex, resultNodePtr);

// do sth. with resultNodePtr if != NULL

I create * resultNodePtr on the stack because I just need it temporarily inside the function. 我在堆栈上创建* resultNodePtr ,因为我只需要暂时在函数内部。 Is this done correctly? 这样做是否正确? However: The function does not work. 但是:该功能不起作用。 resultNodePtr is always NULL, even if the tree contains a node with the search-index. 即使树包含带有search-index的节点, resultNodePtr也始终为NULL。 I debugged it very carefully step by step, it detects 我一步一步地仔细调试它,它检测到

(root->index == nodeIndex)

correctly but 正确但是

  resultNode = root; 

does not work (I want resultNode to point to the same adress root points to). 不起作用(我希望resultNode指向相同的地址点)。 Debugger says resultNode before assignment is 0x0, root node is some adress, after the assignment resultNode remains 0x0. 调试器说resultNode前分配为0x0, 根节点是一些ADRESS,分配resultNode仍然为0x0后。

Do I have to overload the operator= in this case for the class TreeNode? 我是否必须重载operator =在本例中是否为TreeNode类?

I have tried it: 我试过了:

TreeNode & TreeNode::operator=(const TreeNode & oldTreeNode){
 *this = oldTreeNode;
 return *this;
 // ignore childs for now
}

I am not an expert but this operator= seems trivial. 我不是专家,但这个算子似乎微不足道。 Does it affect the assignment of two TreeNode pointers *node1 = *node2 at all? 它是否会影响两个TreeNode指针* node1 = * node2的赋值?

Maybe you can help me. 也许你可以帮助我。 Thanks for reading, appreciate your help. 感谢您的阅读,感谢您的帮助。 If I find a solution myself I will post it here. 如果我自己找到解决方案,我会在这里发布。

Regards, Mark 问候,马克

Because you pass resultNode into the function as a pointer by value , its original value never changes. 因为您将resultNode作为指针按值传递给函数,所以它的原始值永远不会改变。 Think of TreeNode* as literally nothing more than a number representing a memory address; TreeNode*视为字面上只不过是一个代表内存地址的数字; when you reassign it: 当你重新分配时:

resultNode = root;

This modifies the copy that searchNode has, but not the original pointer in the code which invokes searchNode . 这会修改searchNode具有的副本,但不会修改调用searchNode的代码中的原始指针。 Take this simpler example: 举个简单的例子:

void Foo(int x)
{
    x = 100;
}

void Bar()
{
    int x = 0;
    Foo(x);
    // at this point, x is still 0
}

resultNode 's value doesn't change from NULL for the same reason that x doesn't change from 0 when the function Bar is invoked. resultNode的值不会从NULL更改,原因与调用函数Barx不会从0更改的原因相同。 To fix this issue, pass the pointer in as a pointer to a pointer, or a pointer by reference: 要解决此问题,请将指针作为指针传递给指针,或者通过引用传递指针:

void Tree::searchNode(TreeNode* root, int nodeIndex, TreeNode*& resultNode)
{
    // same code
}

... or: ... 要么:

void Tree::searchNode(TreeNode* root, int nodeIndex, TreeNode** resultNodePtr)
{
    // assign to *resultNodePtr instead
}

Your resultNode pointer is being passed by value, not by reference. 您的resultNode指针是按值传递的,而不是通过引用传递的。 So when the function call completes the pointer on the calling side does not receive a value. 因此,当函数调用完成时,调用端的指针不会收到值。

Your algorithm looks fine :) 你的算法看起来很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM