簡體   English   中英

二進制搜索樹刪除功能將節點設置為零而不是刪除

[英]Binary Search Tree Remove Function setting node to zero instead of deleting

我在弄清楚為什么我的樹的刪除功能為什么“有點”刪除節點時遇到了麻煩。 當我打印出結果時,“已刪除”節點顯示為零。

例如:添加節點7,3,9,然后刪除節點9

輸入:添加7,3,9刪除3

輸出:0,7,9

void Tree::remove(int val) {
Node* temp;
if (root == nullptr)
    {return;}
if (val == root->val)
{
   if (root->left == nullptr && root->right == nullptr){ //leaf node 
       delete root;
      }


   else{
        temp = root;
        if (root->left != nullptr && root->right != nullptr){ //left and right children exist

            if (root->left->val - val <= root->right->val - val){//left child is closer to value than right
                int val_to_save = root->left->val;
                root = root->left;
                remove(val_to_save);
                temp->val = val_to_save;//replace value with deleted node
                root = temp;}

            else{
                int val_to_save = root->right->val;
                root = root->right;
                std::cout << val_to_save << std::endl;
                remove(val_to_save);
                temp->val = val_to_save;//replace value with deleted node
                root = temp;}
        }   

       else{ // only one child, either left or right
           if(root->left != nullptr){ //left child
               temp->left = root->left;
               delete temp;}

           else{ //right child
               temp->right = root->right;
               delete temp;}

       }
   }        
}

else{ //value does not match
    temp = root;
    if (val < root->val)
        {
         temp = temp->left;
         remove(val);
         root = temp;
         }

    else{
         root = root->right; 
         remove(val);
         root = temp;}                
    }
}

在c ++中調用delete時,您正在為該給定對象分配內存。 但是,您實際上並沒有“取消聲明”它。 例如,如果刪除根,它仍然知道根是一個節點。 與刪除溫度相同。 因此,似乎您已成功刪除它,刪除了它的值並重新分配了它的內存。 但是,該對象仍然存在,但實例化為null或以字節形式實例為二進制0('\\ 0')。 當它打印\\ 0時,結果顯示為常規0。

我假設您希望答案打印為7、9正確? 如果您的問題解釋錯誤,請告訴我。

最好

考慮有3個節點,

[0] [7] [node3]-> <-[node7] [3] [node9]-> <-[node3] [9] [0]

我們要刪除節點3,

void Tree::remove(int val) 
{
    Node* root = head; // head is head node 
    if (root)
    { 
        if (val == root->val)
        {
            if(root->left)
                root->left->right = root->right; // making the right of 7 node as the right of 3 node,
            if(root->right)
                root->right->left = root->left; //making the left of 9 node as the left of 3 node,
            delete root;
            root = 0;
        }
    }
}
 when we are deleting 7, if(root->right) will execute only, so the 0 (value of 7node->left) is assigned to 3node->left, when we are deleting 3, if(root->left) will execute , so the node9 (value of 3node->left) is assigned to 3node->left, when we are deleting 3, if(root->right) will execute , so the node7 (value of 3node->left) is assigned to 9node->left, when we are deleting 7, if(root->left) will execute only, so the 0 (value of 9node->right) is assigned to 3node->right, 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM