2016-03-24 23 views
0

ツリーは、単語の最初の文字のアルファベット順にソートされます。いくつかの言葉を私は削除することができます、私はすることはできません。私はそれを削除しようとすると何も起こりませんが、それはまだdeleteWord関数を終了します。それはまた、ツリーに残っている2つの単語まで下がります。そして、それらの1つを削除しようとすると、プログラムは何もしません。それは単に私に何も入力することができない空白の行を与えます。そのループはどこかでループしていますが、その理由を理解することはできません。時には単語を削除しようとすると、削除される代わりに、別のノードのデータに置き換えられ、重複するノードがあります。私はここで論理エラーを知っているが、私はそれを見つけることができない。ノードの削除機能は、特定のノードをバイナリ検索ツリーから削除しません。

語削除するためのコードである:

void BinaryTree::deleteWord(string delWord) 
{ 
current = root; 
bool found = false; 
while(!found) 
{ 
    if(current -> data.getWord() == delWord) 
    { 
     if(current -> right) 
     { 
      if(current -> right -> left) 
      { 
       temp = current -> right; 
       temp -> parent = current; 
       while(temp -> left) 
       { 
        temp -> left -> parent = temp; 
        temp = temp -> left; 
       } 

       if(temp -> right) 
       { 
        current -> data = temp -> data; 
        temp -> data = temp -> right -> data; 
        temp -> parent -> left = temp -> right; 
        temp = NULL; 
        found = true; 
       } 
       else 
       { 
        current -> data = temp -> data; 
        temp -> parent -> left = NULL; 
        temp = NULL; 
        found = true; 
       } 
      } 

      else if(current -> right -> right) 
      { 
       current -> data = current -> right -> data; 
       current -> right -> right -> parent = current; 
       current -> right = current -> right -> right; 

       found = true; 
      } 
      else 
      { 
       current -> data = current -> right -> data; 
       current -> right = NULL; 
       found = true; 
      } 

     } 
     else if(current -> left) 
     { 
      if(current -> left -> right) 
      { 
       if(current -> left -> right -> left) 
       { 
        temp = current -> left -> right; 
        temp -> parent = current -> left; 


        while (temp -> left) 
        { 
         temp -> left -> parent = temp; 
         temp = temp -> left; 
        } 

        current -> data = temp -> data; 

        if(temp -> right) 
        { 
         temp -> data = temp -> right -> data; 
         temp -> right = NULL; 
         found = true; 
        } 
        else 
        { 
         temp = NULL; 
         found = true; 
        } 

       } 

       else if(current -> left -> right -> right) 
       { 
        temp = current -> left -> right; 
        current -> data = temp -> data; 
        current -> left -> right = temp -> right; 
        temp -> right -> parent = current -> left; 
        temp = NULL; 
        found = true; 
       } 
       else 
       { 
        current -> data = current -> left -> right -> data; 
        current -> left -> right = NULL; 
        found = true; 
       } 
      } 
      else if(current -> left -> left) 
      { 
       current -> data = current -> left -> data; 
       current -> left -> left -> parent = current; 
       current -> left = current -> left -> left; 
       found = true; 
      } 
      else 
      { 
       current -> data = current -> left -> data; 
       current -> left = NULL; 
      } 
     } 
     else 
     { 
      current = NULL; 
      found = true; 
     } 
    } 
    else if(current -> data.getWord() > delWord) 
    { 
     if(current -> left) 
      current = current -> left; 
    } 

    else if(current -> data.getWord() < delWord) 
    { 
     if(current -> right) 
      current = current -> right; 
    } 
    else 
    { 
     cout << "word somehow not found, check code."; 
     found = true; 
    } 
} 
} 

アレイを表示するためのコードは次のとおり

void BinaryTree::displayAll(WordNode* n) 
{ 
if(n) 
    { 
     displayAll(n->right); 
     print(n -> data); 
     displayAll(n->left); 
    } 
} 

、編集ツリー

void BinaryTree::addWord(string newWord, string newMeaning) 
{ 
WordNode* temp = new WordNode; 
if(empty()) 
{ 
    temp -> data = Word(newWord, newMeaning); 
    temp -> left = NULL; 
    temp -> right = NULL; 
    temp -> parent = NULL; 
    root = temp; 

}//end if 
else 
{ 
    current = root; 
    while(current) 
    { 
     if(newWord > current -> data.getWord()) 
     { 
      if(current -> right == NULL) 
      { 
       temp -> data = Word(newWord, newMeaning); 
       temp -> right = NULL; 
       temp -> left = NULL; 
       temp -> parent = current; 
       current -> right = temp; 
       current = NULL; 
      } 
      else 
       current = current -> right; 
     }//if 
     else if(newWord < current -> data.getWord()) 
     { 
      if(current -> left == NULL) 
      { 
       temp -> data = Word(newWord, newMeaning); 
       temp -> right = NULL; 
       temp -> left = NULL; 
       temp -> parent = current; 
       current -> left = temp; 
       current = NULL; 
      } 
      else 
       current = current -> left; 
     }//elseif 
    }//while 
}//else 
}//funct 
+0

tempは 'addWord'のローカル変数ですが、' deleteWord'では見つけることができません; s定義は 'BinaryTree'のメンバーであると想定していますか? – user3188346

+0

ようこそ。この質問は、あなたが私たちに[最小限の完全な例](http://stackoverflow.com/help/mcve)を与え、あなたが遭遇したエラーのより完全な記述( "つまってしまった"かどうかは不明です) 。 – Beta

+0

これは 'BinaryTree'のメンバです。私は新しいノードでそれを指していなかったのでdeleteWord関数で宣言しませんでしたが、既存のノードのポインタとして使用していました。編集された説明。また、最小限の例が何であるかはわかりません。なぜなら、私が最小限だと思うものを含めるときは、いつも他の関数を求めていたからです。 – ThePeskyWabbit

答えて

0

に単語を追加するため最後にelse文を読む:

else 
     { 
      cout << "else entered.\n"; 
      if(current -> data.getWord() < current -> parent -> data.getWord()) 
      { 
       current -> parent -> left = NULL; 
       current = NULL; 
       found = true; 
      } 
      else if(current -> data.getWord() > current -> parent -> data.getWord()) 
      { 
       current -> parent -> right = NULL; 
       current = NULL; 
       found = true; 
      } 
      else 
      { 
       current = NULL; 
       found = true; 
      } 
     } 
関連する問題