B-树搜索引用子数据类型问题
B-Tree Search referencing children data type issue
所以我试图实现订单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
}
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 特定数据类型的模板类
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- printf 数据类型说明符复杂问题
- 将数据类型从C#封送至C++的类型转换问题
- 在C 中使用字符串数据类型的问题
- 模板函数实例化 自定义数据类型的问题
- C++对数据类型问题
- B-树搜索引用子数据类型问题
- 复杂数据类型的模板实例化问题
- 如何正确使用模板?我在使用模板时遇到数据类型问题<>
- 尝试用c++类创建泛型数据类型的问题
- 与我定义的类的成员有关的c++向量数据类型编译器错误问题
- 有数据类型问题的c++阶乘函数