2016-03-24 20 views
0

Ağaç, alfabetik olarak sözcüklerin ilk harfine göre sıralanır. Söyleyebileceğim bazı kelimeler, bazılarını yapamam. Silmeye çalıştığımda olduğu gibi hiçbir şey olmuyor, ancak yine de deleteWord işlevinden çıkıyor. Ayrıca ağaçta kalan iki kelimeye inecek ve bunlardan birini silmeye çalıştığımda program hiçbir şey yapmıyor. Sadece bana hiçbir şey giremediğim boş bir satır veriyor; Bir süre içinde sıkışmış ama nedenini anlayamıyorum. Bazen bir sözcüğü silmeyi ve silinmeyi değil, başka bir düğümün verileriyle değiştirilir ve yinelenen bir düğümüm vardır. Burada bir mantık hatası olduğunu biliyorum ama bulamıyorum.Düğüm işlevini silme, ikili bir arama ağacındaki bazı düğümleri silmeyecek

kelime silme kodudur:

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

ağaca kelime eklemek için

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' içinde yerel bir değişkendir, ancak' deleteWord' içinde bulamıyorum, s tanımı 'BinaryTree' üyesi olduğunu varsayabilir miyim? – user3188346

+0

Tekrar hoşgeldiniz. Bize [asgari tam bir örnek] (http://stackoverflow.com/help/mcve) verdiyseniz ve karşılaştığınız hataların daha eksiksiz bir açıklamasını ("takılıp kalıyor") net değilse, bu soruya cevap vermek daha kolay olurdu. . – Beta

+0

, "BinaryTree" in bir üyesidir. DeleteWord işlevinde bildirmedim çünkü yeni bir düğümde işaret etmedim, ancak varolan düğümler için bir işaretçi olarak kullanıyorum. düzenlenmiş açıklama. Ayrıca asgari örneğin ne olacağını bilmiyorum çünkü ne düşündüğümü en az bulduğumda, diğer işlevler için sordum. – ThePeskyWabbit

cevap

0

düzenlendi:

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

dizi görüntülemek için kod Okunması gereken son ifade:

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; 
      } 
     } 
İlgili konular