【问题标题】:Insertion function in a random binary tree随机二叉树中的插入函数
【发布时间】:2012-06-24 08:22:28
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow!让陌生人通过检查来发现代码中的错误是没有效率的。您应该使用调试器来识别(或至少隔离)问题,然后返回一个更具体的问题(一旦您将其缩小到 10 行 test-case)。
  • 问题不在代码中,它可以编译,问题在于插入我不知道如何让它检测为什么当我需要添加第四个节点时它没有'不知道第一个右节点和左节点已经在使用中。

标签: c++ binary-tree


【解决方案1】:

这里对您的代码稍作修改:

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

原因是当我们将 n 分配给 aux 时,我们只是告诉 aux 指向 n而不是指向左节点。我们没有将 n 指定为 head 的左孩子。

您也可以通过将 aux 声明为节点指针的指针来解决此问题。

node * * aux = &head;

【讨论】:

  • 谢谢,我刚刚使用了您发布的内容,它完全解决了我的问题!是的,你是对的,我错过了辅助,但我现在已经吸取了教训。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
相关资源
最近更新 更多