简体   繁体   English

二进制搜索树节点插入

[英]Binary search tree node insertion

want to insert element in binary tree. 想要在二叉树中插入元素。 what is wrong in this code. 该代码有什么问题。 I have a structure with data, left and right as self referencing structure and root as a global variable of type structure initialized to NULL. 我有一个数据结构,左和右为自引用结构,根为初始化为NULL的类型结构的全局变量。

void insert(struct tree_node *root,int data)
{
    if(root == NULL)
    {
        tree_node *new_node = new tree_node();

        new_node->left = NULL;
        new_node->right = NULL;
    }
    else if(root->data > data)
    {
        insert(root->left,data);
    }
    else
    {
    insert(root->right,data);
    }
}

There are several issues with the code you propose here: 您在此处建议的代码存在几个问题:

  • You never set your global variable "root". 您永远不会将全局变量设置为“ root”。 Also, a global variable is always a bad thing (especially if you plan to use threads). 同样,全局变量总是一件坏事(尤其是如果您打算使用线程的话)。
  • You never link the new entries to the existing structure. 您永远不会将新条目链接到现有结构。
  • You never set the new entry data 您从未设置新的输入数据

You should probably try this instead: 您可能应该尝试以下方法:

struct tree_node *insert(struct tree_node *root, int data) 
{
  if(root == NULL) {
    struct tree_node *new_node = new tree_node();

    new_node->data = data;
    new_node->left = NULL;
    new_node->right = NULL;

    return new_node;
  } else if(root->data > data) {
    root->left = insert(root->left, data);
  } else {
    root->right = insert(root->right, data);
  }

  return root;
}

which you can call using 您可以使用

root = insert(root, data);

as shown on this codepad 如此键盘上所示

**you have created new_node but doesn't assign it value .so you can't insert a value to it. **您已创建new_node,但未为其分配值。因此您无法向其中插入值。

you can also use non recursive function to insert 您还可以使用非递归函数来插入

void insert_nrec(int m)  //for insert in binary tree
{
node *tmp,*par,*ptr;
ptr=root;
par=NULL;
tmp=NULL;
    while(ptr!=NULL)
    {
        par=ptr;
        if(m<ptr->data)
        {   ptr=ptr->lchild;    }
        else if(m>ptr->data)
        {   ptr=ptr->rchild;    }
        else
            {   
                cout<<"duplicate key found::";
                return;             
            }       

    }
tmp=new node;
tmp->data=m;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(par==NULL)
    {
        root=tmp;
    }   
else if(m<par->data)
    {
        par->lchild=tmp;    
    }
else
    {
        par->rchild=tmp;
    }

}

When do you set root to anything other than NULL ? 什么时候将root设置为NULL以外的其他NULL

Once you do, how do you actually change root->left or right ? 完成后,实际上如何更改root->leftright

I'm just going to rewrite @TheRealNeo's nifty 'functional' answer so that you pass it a reference to the root pointer: 我只是要重写@TheRealNeo的漂亮的“功能性”答案,以便您将其传递给对根指针的引用:

void insert(struct tree_node * &root, int data) 
{
  if(root == NULL) {
    struct tree_node *new_node = new tree_node();

    new_node->data = data;
    new_node->left = NULL;
    new_node->right = NULL;

    root = new_node;
  } else if(root->data > data) {
    insert( root->left, data);
  } else {
    insert( root->right, data);
  }
}

which you call using 您使用的方式

insert(root, data);

An advantage of this over the functional one, is there's a good chance the compiler will actually do the recursive calls as 'tail recursions', ie when inserting left, instead of making a recursive call, the code will simply change the 'root' reference to refer to 'root->left' and jump back up to the start of the function. 与功能性代码相比,此方法的一个优势在于,编译器很有可能实际上将递归调用作为“尾递归”进行,即,当向左插入时,而不是进行递归调用,代码将简单地更改“ root”引用指向“ root-> left”,然后跳回到该功能的开头。

The explicitly non-recursive form is not as clear, since it involves some pointer-to-pointer stuff, but it's really not so bad: 明确的非递归形式不太清楚,因为它涉及到一些指针到指针的东西,但实际上还不错:

void insert(struct tree_node * &root, int data) 
{
  struct tree_node **ptrp = &root;

  while(*ptrp != NULL ){
     struct tree_node *np = *ptrp;
     if( np->data > data ){
          ptrp = &np->left;
     }else{
          ptrp = &np->right;
     }
  }
   // new node will be placed at *ptrp, a location
   // currently containing NULL.

  struct tree_node *new_node = new tree_node();

  new_node->data = data;
  new_node->left = NULL;
  new_node->right = NULL;

  *ptrp = new_node;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM