【问题标题】:B-Tree Search referencing children data type issueB-Tree Search 引用子数据类型问题
【发布时间】:2016-03-25 01:50:34
【问题描述】:

所以我试图实现一个 Order 2 B-Tree 但是我对编程很陌生,尤其是在 c++ 中我已经为每个节点创建了这个结构

    struct BTreeNode
{
    int data[2];
    BTreeNode **ChildLow;
    BTreeNode **ChildMid;
    BTreeNode **ChildHigh;

};

所以当我尝试将下一个节点设置为子节点时,我不断收到编译错误,它想要它的类型为 BTreeNode,不是吗?

bool search(BTreeNode *root, int value)
{
        BTreeNode currentNode = *root;
        bool found = false;
        bool searchComplete = false;
        while(found == false || searchComplete == false)
        {
            if (currentNode == NULL)
            {
                searchComplete == true;
                return false;
            }
            if (currentNode.data[1] == value)
            {
                found == true;
            }
            else if(value > currentNode.data[1])
            {
                if (currentNode.data[2] == value)
                {
                    found == true;
                }
                else if(value > currentNode.data[2])
                {
                    currentNode == currentNode.ChildHigh;

                }
                else
                {
                    currentNode == currentNode.ChildMid;
                }
            }
            else
            {
                currentNode == currentNode.ChildLow;
            }
        }


}

当我将它与 null 进行比较时,它也会显示错误。

以下是错误:

1   IntelliSense: no operator "==" matches these operands
        operand types are: BTreeNode == int
2   IntelliSense: no operator "==" matches these operands
        operand types are: BTreeNode == BTreeNode **    
3   IntelliSense: no operator "==" matches these operands
        operand types are: BTreeNode == BTreeNode **    
4   IntelliSense: no operator "==" matches these operands
        operand types are: BTreeNode == BTreeNode **    

1 为 Null 错误,其余为指向子级的指针

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 编译错误既不会动摇也不会抛出,它们会被打印出来,如果你需要任何帮助,你需要在这里打印它们,在你的问题中。
  • @user3105172 我已经发布了一个更新的答案,它应该可以消除你的错误,虽然我认为你应该拥有 BTreeNode *childHigh 而不是 BTreeNode **childHigh 的结构。
  • 声明中额外的间接级别的目的是什么:BTreeNode **ChildLow;?这三个名字也确实妨碍了简单的编码。使用BTreeNode *Children[3]; 会更好
  • 为什么你想要一个只返回truefalse 的搜索函数?在许多情况下,您需要知道找到它时它在哪里,如果没有找到它会在哪里。所以你应该想办法让搜索返回那种信息。
  • 在一棵普通的树中,我总是更喜欢children[2]而不是leftright。在添加重新平衡代码之前,使用children[2] 可能进行的简化可能很小而且似乎不值得。但是,如果您有任何重新平衡,那么leftright 命名迫使您编写的重新平衡代码是通常使用边 s 和边 1-s 的两倍或四倍。

标签: c++ pointers compiler-errors b-tree


【解决方案1】:

TL;DR.

试试这个,然后找出差异。

struct BTreeNode
{
    int data[2];
    BTreeNode *ChildLow;
    BTreeNode *ChildMid;
    BTreeNode *ChildHigh;

};

bool search(BTreeNode *node, int value)
{
    while(node != NULL)
    {
        if (node->data[0] == value || node->data[1] == value) {
            return true;      // found!
        }

        // search in a subtree
        if(value > node->data[1]) {
            node = node->ChildHigh;
        } else if(value > node->data[0]) {
            node = node->ChildMid;
        } else {
            node = node->ChildLow;
        }
    }

    return false;  // at the bottom of the tree - not found
}

【讨论】:

  • 这个答案没有解释任何关于为什么它不能正常工作,你只是说“这是很好的代码。使用这个代码。”跨度>
  • @PiotrPytlik 1. 固定作业,谢谢。 2.错误太多无法解释。是的,我开始写非常详细的解释——当它们比代码长时就放弃了。 OP 并没有发明 BTree,而是试图实现一些手册、书籍、纸张等中提出的想法。所以这是一个与源代码及其原始代码进行比较的实现。每个差异都表示算法或语言的一些细节,OP 需要了解这些细节。可能这不是那么简单,但恕我直言,这是一种更有成效的学习方式。
  • 我在解释时注意到了错误的数量。我不是在吹毛求疵,但如果他想要一个正确的实现来理解,我敢肯定这不是第一个,因为“手册、书籍、论文等”中通常有示例。当然,这是比较他做错了什么的好方法,但他问的是哪里错了,而不是如何正确实施这个问题。你展示了一个很好的实现,我解释了(一些)他的错误。
【解决方案2】:

你得到的第一个错误是因为 currentNode 是一个 BTreeNode 而不是一个指针,它应该与 NULL 进行比较。一个可能的解决方法是:

BTreeNode* currentNode = root;
/*...code...*/
if (currentNode == NULL)
/*...code...*/
if (currentNode->data[1] == value)
/*...code...*/
currentNode = currentNode->ChildHigh
/*...code...*/
currentNode = currentNode->ChildMid
/*...code...*/
currentNode = currentNode->ChildLow

还请注意,当您键入时:

//searchComplete == true; this compares
searchComplete = true; // this assigns

//found == true; this compares
found = true; this assigns

您没有将 true 分配给 searchComplete 或 found,您实际上是在进行比较。

编辑

另外,如果你的节点只是应该有一个指向 high、med 和 low 孩子的指针,你不应该使用 '**',你应该写

BTreeNode* childHigh;
BTreeNode* childMid;
BTreeNode* childLow;

这意味着它们指向您的孩子。

【讨论】:

  • 谢谢,这是一个问题,但不是我的意思,我试图将 currentNode 设置为下一个节点,这是通过将其指向下一个子节点来完成的?我可能做错了,但这就是我的意图
  • 您的第一个错误是因为您将 currentNode 与 NULL 进行比较,其中 currentNode 是 BTreeNode,而不是指针。我会发布一个可能的修复。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2011-08-07
相关资源
最近更新 更多