2016-03-24 16 views
0

Der Baum ist alphabetisch nach dem ersten Buchstaben der Wörter sortiert. Einige Wörter, die ich löschen kann, andere kann ich nicht. Wie in ich versuche, es zu löschen, und nichts passiert, aber es beendet immer noch die Funktion deleteWord. Es wird auch auf zwei Wörter im Baum verlassen und wenn ich versuche, einen von ihnen zu löschen, tut das Programm nichts. Es gibt mir einfach eine Leerzeile, in die ich nichts eintragen kann; Es steckt irgendwo in einer Zeitschleife, aber ich kann nicht herausfinden warum. Manchmal versuche ich ein Wort zu löschen und anstatt es zu löschen, wird es durch die Daten eines anderen Knotens ersetzt und ich habe einen doppelten Knoten. Ich weiß, dass hier ein logischer Fehler ist, aber ich kann es nicht finden.Löschen Knoten Funktion löscht bestimmte Knoten aus einem binären Suchbaum

der Code für das Wort Deletion ist:

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; 
    } 
} 
} 

der Code das Array anzuzeigen ist:

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

und für Wörter zu dem Baum Zugabe

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 ist eine lokale Variable in 'addWord', aber in' deleteWord' kann ich sie nicht finden; s Definition kann ich davon ausgehen, dass es ein Mitglied von 'BinaryTree' ist? – user3188346

+0

Willkommen zurück. Diese Frage wäre leichter zu beantworten, wenn Sie uns ein [minimales vollständiges Beispiel] (http://stackoverflow.com/help/mcve) geben würden, und eine ausführlichere Beschreibung der aufgetretenen Fehler ("es bleibt hängen" ist unklar) . – Beta

+0

ist ein Mitglied von 'BinaryTree'. Ich deklarierte es nicht in der Funktion deleteWord, weil ich es nicht auf einen neuen Knoten zeigte, sondern es nur als einen Zeiger für existierende Knoten verwendete. bearbeitete Beschreibung. Ich weiß auch nicht, was das minimale Beispiel wäre, denn wenn ich etwas einbeziehe, was ich für minimal halte, habe ich nach anderen Funktionen gefragt. – ThePeskyWabbit

Antwort

0

die editierte final else Anweisung zu lesen:

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; 
      } 
     } 
Verwandte Themen