0
我有一個二叉搜索樹,當我嘗試執行刪除帶有單個子節點的情況時,您將刪除該節點並將其移動到位。我有它的代碼,但是每當我這樣做的時候它就會給我一個糟糕的指針。帶有一個孩子的二元搜索樹刪除節點
這是
else if((root->Left != NULL) != (root->Right != NULL)){ //Checks if it's a on child node
if(root->Left != NULL){ //If it has a left child, attempts to move the left child to existing node
delete root;
root = root->Left;
}
else{ //If it is right child, attempts to move right child to existing node
delete root;
root = root->Right;
}
}
的結構有值
DATA_TYPE Value;
TreeNode* Left;
TreeNode* Right;
我知道我分配錯了來自調試器的代碼段,有啥移動節點的正確方法?
不,我需要它是XOR – wzsun
@wzsun編輯我的回答 – James
有人告訴我,你實際上必須先刪除它,讓我去與它一起,但如果你只是使用=重新分配那麼該節點會發生什麼?因爲它只是永遠存儲這個空間,你現在無法刪除它 – wzsun