Поэтому я пытаюсь реализовать B-Tree 2-го порядка, однако я довольно новичок в программировании, особенно в 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;
}
}}
Это также показывает ошибку, когда я сравниваю его с нулем.
Вот ошибки:
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 — нулевая ошибка, остальные — указатели на детей
Любая помощь будет оценена
Благодарю вас
Первая ошибка, которую вы получаете, заключается в том, что 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 или найдено, вы фактически делаете сравнение.
РЕДАКТИРОВАТЬ
Кроме того, если ваш узел должен иметь указатель на дочерние элементы high, med и low, вы не должны использовать ‘**’, вы должны написать
BTreeNode* childHigh;
BTreeNode* childMid;
BTreeNode* childLow;
что означает, что они указывают на ваших детей.
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
}