我正在做一個乙 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]
我猜這是簡單的東西,我忽略了,但我找不到它,一直試圖相當一段時間。
編輯:決定現在簡單地傳遞到key
和minNode()
忽略oldKey和auxKey,修改minNode返回指針。
新的錯誤,同樣的地方
lvalue required as left operand
如果您將代碼段精簡爲單個文件(可以從頭文件複製並粘貼),那麼編譯它時會出現相同的錯誤,這將有助於人們。這樣,(1)您將在錯誤消息中獲得與您所顯示的代碼相對應的行號,以及(2)人們可以非常輕鬆地將代碼拿走並在其最喜歡的編譯器上進行編譯,該編譯器的錯誤消息他們最習慣於解釋。 –
請注意,使用templatetypedef的時間少於找到該錯誤的行,而不是我花時間寫出該建議。所以在這種情況下,我錯了,它沒有幫助! –
那麼我應該給所有的代碼?我很好,但它有點大。我不確定非常大的代碼「片段」的禮節。 – Kalec