2012-06-13 69 views
0

我正在做一個 inary 小號目錄操作搜索牛逼 REE(BST的簡稱)和我碰到,我想不通一個問題。遇到問題返回類成員

我會盡量減少代碼量,但恐怕還是需要一點點。

節點:

template <typename Type> 
class BSTNode {   // Binary Search Tree nodes 
    private: 
     int key;  // we search by key, no matter what type of data we have 
     Type data; 
     BSTNode *left; 
     BSTNode *right; 

    public: 

     BSTNode (int, Type); 
     bool add (int, Type); 
     Type search (int); 
     BSTNode<Type> *remove (int, BSTNode*); 
     BSTNode<Type> *minNode (int);             
}; 

根:

template <typename Type> 
class BST {     // The binary search tree containing nodes 
    private: 
     BSTNode<Type> *root; // Has reference to root node 

    public: 

     BST(); 
     bool add (int, Type); 
     Type search (int); 
     bool remove (int); 

};

我不知道要給多少代碼,因爲我不想誇大,如果您需要更多,請說。

我都做遞歸搜索並刪除

template<typename Type> 
BSTNode<Type> *BSTNode<Type>::remove(int removeKey, BSTNode *parent) { 

    // Here I try to remove nodes 
    // Depending on the number of children a node has, I remove in different ways 
    // The error occurs at removing a node with 2 children 
    // here I look for smallest node greater than current node, replace current node, delete node I replaced WITH 

    if (this->left != NULL && this->right != NULL){ 

     int *auxKey = &key; 

     this = this->right->minNode(auxKey); // replace 

     return this->right->remove(this->key, this); // remove old node 
    } 
} 

這裏是minNode:

template<typename Type> 
Type *BSTNode<Type>::minNode (int oldKey) { 
    if (this->left == NULL) { 
     //oldKey = this->key; 
     return this->data; 
    } else 
     return left->minNode(); 
} 

這是發生錯誤:

this = right->minNode(auxKey); 

這導致鏈的錯誤,但我認爲主要的錯誤是:

error: invalid conversion from 'int*' to 'int' [-fpermissive] 

我猜這是簡單的東西,我忽略了,但我找不到它,一直試圖相當一段時間。

編輯:決定現在簡單地傳遞到keyminNode()忽略oldKey和auxKey,修改minNode返回指針。

新的錯誤,同樣的地方

lvalue required as left operand 
+2

如果您將代碼段精簡爲單個文件(可以從頭文件複製並粘貼),那麼編譯它時會出現相同的錯誤,這將有助於人們。這樣,(1)您將在錯誤消息中獲得與您所顯示的代碼相對應的行號,以及(2)人們可以非常輕鬆地將代碼拿走並在其最喜歡的編譯器上進行編譯,該編譯器的錯誤消息他們最習慣於解釋。 –

+0

請注意,使用templatetypedef的時間少於找到該錯誤的行,而不是我花時間寫出該建議。所以在這種情況下,我錯了,它沒有幫助! –

+0

那麼我應該給所有的代碼?我很好,但它有點大。我不確定非常大的代碼「片段」的禮節。 – Kalec

回答

0

你minNode函數採用代表舊密鑰的一個int值,但你傳遞一個int *到它在刪除功能(具體而言,auxKey)。嘗試傳入舊密鑰的值,而不是指向它的指針。或者,如果要更新in參數以保存正確的值(您似乎正在嘗試這樣做),請將該參數更改爲參考參數。

希望這會有所幫助!

+0

我試圖只返回'數據'但由於數據和密鑰是分開的,我想修改密鑰。除非這段代碼不起作用,否則它實際上會損害功能性,所以我可以這樣做 – Kalec