0

この C++ のバイナリ ツリーの挿入関数に問題があります。右または左にノードを再度追加する必要があるまで、ノードは正しく挿入されます。関数は、私がすでにそれらの場所にノードを挿入している場合、左または右のいずれにもノードがないと考えています。

これが私のコードです:

void insert(string data)
{    
    srand(time(NULL));
    int r;
    node *aux=head;
    node *n=new node(data);
    if (head==NULL)
    {
        head =n;
        return;
    }

    while (aux!=NULL)
    { 
        r=rand()%100;
        if (r>50)
        {
            cout<<"\nRandom is "<<r<<", Therefore we have to go to the right."<<endl;
            aux=aux->right;  
        }
        else
        {   
            cout<<"\nRandom is "<<r<<", Therefore we have to go to the left."<<endl;
            aux=aux->left;
            if (aux!=NULL)
            {
                cout<<aux->getdata()<<endl;
            }
        }
    }

    aux=n;
    cout<<"\nWe insert "<<aux->getdata()<<endl;
}
4

1 に答える 1

2

コードのわずかな変更は次のとおりです。

void insert(string data)
      {    srand(time(NULL));
           int r;
           node *aux=head;
           node *n=new node(data);
           if(head==NULL){
                          head =n;
                          return;
                          }

       while(aux!=NULL) // We could put while(true) here.
       { 
                       r=rand(); // Modulo is a somehow slow operation
                       if((r  & 1 )== 0) // This is much faster. It checks if r is even
                       {  cout<<"\nRandom is "<<r<<", which is even therefore we have to go to the right."<<endl;
                          if ( aux->right == NULL) // We found an empty spot, use it and break
                          {
                              aux->right = n; break;
                          }
                          else // else move to the right child and continue
                          {
                              aux=aux->right;  
                              cout<<aux->getdata()<<endl;
                          }
                       }
                       else
                       {   
                           cout<<"\nRandom is "<<r<<", which is odd Therefore we have to go to the left."<<endl;
                          if ( aux->left == NULL) // We found an empty spot, use it and break
                          {
                              aux->left = n; break;
                          }
                          else // else move to the left child and continue
                          {
                              aux=aux->left;  
                              cout<<aux->getdata()<<endl;
                          }

                       }
       }
       cout<<"\nWe insert "<<n->getdata()<<endl;

  }

主な理由は、aux を誤用していることです。間違いを特定するのに役立つことを願っている例を次に示します。

node * aux = head; // suppose head doesn't have any child node
node * n = new node(data);

aux = aux->left; // Set aux to point on the left child of head
aux = n; // Set aux to point on n

cout << aux == NULL?"Aux is null":"Aux is not null" << endl;
cout << head->left == NULL?"Left is null":"Left is not null" << endl;

このコードは次を返す必要があります。

Aux is not null
Left is null

その理由は、nauxに割り当てたときに、左側のノードではなくnを指すように単にauxに指示したためです。nを head の左の子に割り当てませんでした。

aux をノードのポインターのポインターとして宣言することで、この問題を解決することもできます。

node * * aux = &head;
于 2012-06-24T00:22:28.100 に答える