2016-12-30 6 views
0

私のツリーの削除機能がノードを「幾分」削除している理由を知りません。結果を印刷すると、「削除された」ノードはゼロとして表示されます。例えばバイナリ検索ツリー削除する代わりに機能設定ノードを削除します。

: ノード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;}     
    } 
} 
+0

*自分のツリーの削除機能がノードを削除している理由を理解できない* - コンパイラツールセットに付属のデバッガを使用して、あなたが期待していたものから。 – PaulMcKenzie

+0

葉を削除するときは、その親の子ポインタがnullptrに設定されていることを確認してください。それは、あなたが正しい実装を達成することからまだ遠いと思います。 –

+0

私は現在のノードとその子の両方を追跡する方法に問題があります – user6313136

答えて

0

C++でdeleteを呼び出すと、指定されたオブジェクトのメモリの割り当てが解除されます。しかし、実際には宣言していません。たとえば、rootを削除すると、rootがノードであることが分かります。一時ファイルを削除する場合と同じです。だから、それを正常に削除して、その価値を取り除き、そのメモリを解放しているようです。しかし、オブジェクトはまだそこにありますが、nullまたはバイナリ0( '\ 0')のバイト形式でインスタンス化されます。 \ 0を出力すると、結果は通常の0として表示されます。

あなたの回答を7,9として印刷したいと思っていますか?あなたの質問を間違って解釈したら、私に知らせてください。ベスト

0

3ノードが、ある検討

[0] [7] [ノード3] - > < - [node7] [3] [node9] - > < - [ノード3] [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, 
関連する問題