【发布时间】: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];会更好 -
为什么你想要一个只返回
true或false的搜索函数?在许多情况下,您需要知道找到它时它在哪里,如果没有找到它会在哪里。所以你应该想办法让搜索返回那种信息。 -
在一棵普通的树中,我总是更喜欢
children[2]而不是left和right。在添加重新平衡代码之前,使用children[2]可能进行的简化可能很小而且似乎不值得。但是,如果您有任何重新平衡,那么left、right命名迫使您编写的重新平衡代码是通常使用边s和边1-s的两倍或四倍。
标签: c++ pointers compiler-errors b-tree