B-树搜索引用子数据类型问题

B-Tree Search referencing children data type issue

本文关键字:数据类型 问题 引用 搜索      更新时间:2023-10-16

所以我试图实现订单2 B树,但我对编程很陌生,尤其是在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错误,其余为指向子的指针

如有任何帮助,将不胜感激

谢谢

您得到的第一个错误是因为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或find,实际上是在进行比较。

编辑

此外,如果你的节点应该有一个指向high、med和low子节点的指针,你不应该使用"**",你应该写

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

这意味着他们指向你的孩子。

TL;博士

试试这个,然后找出不同之处。

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
}