所以即时尝试实现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错误,其余是指向孩子的指针
任何帮助将不胜感激
谢谢
答案 0 :(得分:0)
你得到的第一个错误是因为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
您没有为searchComplete指定true或找不到,您实际上正在进行比较。
修改强>
此外,如果您的节点应该指向高,中,低的孩子,那么您不应该使用' **',您应该写
BTreeNode* childHigh;
BTreeNode* childMid;
BTreeNode* childLow;
这意味着他们指向你的孩子。
答案 1 :(得分:0)
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
}