2015-12-18 46 views
0

所以我試着去實現一個令2 B樹但是我很新的節目,特別是在C++中,我創建了這個結構的每個節點B樹搜索引用兒童的數據類型問題

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爲空的錯誤,其餘爲指針,以兒童

任何幫助,將不勝感激

謝謝

+0

編譯錯誤既不會動搖也不會拋出,它們會被打印出來,如果您需要任何幫助,您需要在這裏打印它們,在您的問題中。 – EJP

+0

@ user3105172我已經發布了一個更新的答案,它應該擺脫你的錯誤,雖然我認爲你應該有BTreeNode * childHigh而不是BTreeNode ** childHigh的結構。 –

+0

你的聲明中額外的間接級別的目的是什麼:'BTreeNode ** ChildLow;'?而且這三個名字真的妨礙了簡單的編碼。用'BTreeNode * Children'[3];' – JSF

回答

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; 

這意味着他們指向你的孩子。

+0

謝謝,這是一個問題,但不是我的意思,我試圖設置currentNode到下一個節點,這是通過指向下一個孩子完成的?我可能會錯過這一切,但那是我的意圖無論如何 – meltonCG

+0

你的第一個錯誤是因爲你正在比較currentNode爲NULL,其中currentNode是一個BTreeNode,而不是指針。我會發佈一個可能的修復。 –

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 
} 
+2

這個答案沒有解釋任何**爲什麼**它不能正常工作,你只是說「這是很好的代碼,使用這個代碼。」 –

+0

@PiotrPytlik 1.固定作業,謝謝。 2.解釋錯誤太多。是的,我開始寫相當詳細的解釋 - 當它們比代碼長時,就放棄了。 OP沒有發明BTree,而是試圖實現一些手冊,書籍,論文等等。所以這裏有一個實現來與源代碼和它們的原代碼進行比較。每個差異都表示OP需要了解的算法或語言的一些細節。可能這不是那麼簡單,但恕我直言,更有成效的學習方式。 – CiaPan

+0

我注意到解釋時的錯誤數量。我不挑剔,但如果他想要一個適當的實現來理解,我相信這不是第一個,因爲通常在「手冊,書籍,論文,任何」中都有例子。當然,這是一個很好的比較他做錯了什麼的方法,但是他問什麼是錯誤的,而不是如何正確地實現這個問題。你展示了一個很好的實現,我解釋了(一些)他的錯誤。 –