C 二叉树InOrderWalk实现未按预期工作

C 二叉树InOrderWalk实现未按预期工作,c,pointers,gdb,binary-tree,inorder,C,Pointers,Gdb,Binary Tree,Inorder,我正在尝试用C实现一个二叉树数据结构,并在插入几次之后进行有序遍历。 程序只打印我插入的第一个元素,而不打印任何其他节点 #include <stdlib.h> #include <stdio.h> #include <string.h> struct tree { struct node *root; }; struct node { int val; struct node *left; struct node *righ

我正在尝试用C实现一个二叉树数据结构,并在插入几次之后进行有序遍历。 程序只打印我插入的第一个元素,而不打印任何其他节点

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct tree
{

   struct node *root;

};


struct node
{

  int val;

  struct node *left;

  struct node *right;

};


struct tree *init ()
{

  struct tree *t = malloc (sizeof (struct tree));


  t->root = NULL;

  return t;

}



void insert (struct tree *t, int val)
{

  struct node *succ;

  struct node *new = malloc (sizeof (struct node));


  new->val = val;

  new->left = NULL;

  new->right = NULL;


  succ = NULL;

  struct node *insertPlace = t->root;

  while (insertPlace != NULL)
  {

       succ = insertPlace;

       if (insertPlace->val < new->val)

       insertPlace = insertPlace->right;

       else

       insertPlace = insertPlace->left;

   }

  if (succ == NULL)
    t->root = new;

  else if (new->val < succ->val)

    insertPlace = succ->right;

  else

   insertPlace = succ->left;

}



void inorderWalk (struct node *p)
{

  if (p != NULL)
  {

    if (p->left != NULL)
      inorderWalk (p->left);

    printf ("%d ", p->val);

    if (p->right != NULL)
       inorderWalk (p->right);

  }

}



void
print (struct tree *t)
{

struct node *p;

p = t->root;


inorderWalk (p);


} 



int

main ()
{

struct tree *t = init ();

insert (t, 5);

insert (t, 15);

insert (t, 20);

insert (t, 1);

insert (t, 2);

insert (t, 4);

insert (t, 10);


print (t);


return 0;

}
#包括
#包括
#包括
结构树
{
结构节点*根;
};
结构节点
{
int-val;
结构节点*左;
结构节点*右;
};
结构树*init()
{
结构树*t=malloc(sizeof(结构树));
t->root=NULL;
返回t;
}
void insert(结构树*t,int val)
{
结构节点*成功;
结构节点*new=malloc(sizeof(结构节点));
新建->val=val;
新建->左=空;
新建->右=空;
succ=NULL;
结构节点*insertPlace=t->root;
while(insertPlace!=NULL)
{
succ=插入位置;
如果(插入位置->值<新建->值)
insertPlace=insertPlace->右侧;
其他的
insertPlace=insertPlace->left;
}
如果(成功==NULL)
t->root=new;
else if(新建->增值<成功->增值)
insertPlace=succ->右侧;
其他的
insertPlace=succ->left;
}
void inorderWalk(结构节点*p)
{
如果(p!=NULL)
{
如果(p->left!=NULL)
顺序队列(p->左);
printf(“%d”,p->val);
如果(p->right!=NULL)
顺序漫游(p->右);
}
}
无效的
打印(结构树*t)
{
结构节点*p;
p=t->root;
有序(p);
} 
int
主要()
{
结构树*t=init();
插入(t,5);
插入(t,15);
插入(t,20);
插入(t,1);
插入(t,2);
插入(t,4);
插入(t,10);
打印(t);
返回0;
}
该代码还可以通过在线gdb调试器获得

任何关于代码为什么不能按预期工作的反馈都将不胜感激


提前感谢您的帮助。

您的代码没有向树中添加除根之外的元素。设置
insertPlace=succRight
仅更新
insertPlace
变量,而不更新树中的任何内容

我建议在寻找正确节点的实际循环中设置左或右节点的值:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct tree
{
   struct node *root;
};


struct node
{
  int val;

  struct node *left;

  struct node *right;
};


struct tree *init ()
{
  struct tree *t = malloc (sizeof (struct tree));
  t->root = NULL;
  return t;
}



void insert (struct tree *t, int val)
{

  struct node *succ;

  struct node *new = malloc (sizeof (struct node));
  new->val = val;
  new->left = NULL;
  new->right = NULL;

  succ = NULL;

  struct node* insertAtNode=t->root;
  if (insertAtNode==NULL) {
    t->root=new;
    return;
  }
  do {
    if (insertAtNode->val < new->val) {
      if (insertAtNode->right==NULL) {
        insertAtNode->right=new;
        return;
      } else {
        insertAtNode=insertAtNode->right;
      }
    } else if (insertAtNode->left==NULL) {
      insertAtNode->left=new;
      return;
    }
    else {
      insertAtNode=insertAtNode->left;
    }
  }
  while(1);
}



void inorderWalk (struct node *p)
{
  if (p != NULL)
  {
    if (p->left != NULL)
      inorderWalk (p->left);
    printf ("%d ", p->val);
    if (p->right != NULL)
       inorderWalk (p->right);
  }
}



void print (struct tree *t)
{
  struct node *p;

  p = t->root;

  inorderWalk (p);
  printf("\n");
}



int main ()
{
  struct tree *t = init ();

  insert (t, 5);

  insert (t, 15);

  insert (t, 20);

  insert (t, 1);

  insert (t, 2);

  insert (t, 4);

  insert (t, 10);


  print (t);


  return 0;

}
#包括
#包括
#包括
结构树
{
结构节点*根;
};
结构节点
{
int-val;
结构节点*左;
结构节点*右;
};
结构树*init()
{
结构树*t=malloc(sizeof(结构树));
t->root=NULL;
返回t;
}
void insert(结构树*t,int val)
{
结构节点*成功;
结构节点*new=malloc(sizeof(结构节点));
新建->val=val;
新建->左=空;
新建->右=空;
succ=NULL;
结构节点*insertAtNode=t->root;
if(insertAtNode==NULL){
t->root=new;
返回;
}
做{
如果(插入节点->值<新建->值){
if(insertAtNode->right==NULL){
插入节点->右=新建;
返回;
}否则{
insertAtNode=insertAtNode->right;
}
}else if(insertAtNode->left==NULL){
插入节点->左=新建;
返回;
}
否则{
insertAtNode=insertAtNode->left;
}
}
而(1),;
}
void inorderWalk(结构节点*p)
{
如果(p!=NULL)
{
如果(p->left!=NULL)
顺序队列(p->左);
printf(“%d”,p->val);
如果(p->right!=NULL)
顺序漫游(p->右);
}
}
无效打印(结构树*t)
{
结构节点*p;
p=t->root;
有序(p);
printf(“\n”);
}
int main()
{
结构树*t=init();
插入(t,5);
插入(t,15);
插入(t,20);
插入(t,1);
插入(t,2);
插入(t,4);
插入(t,10);
打印(t);
返回0;
}

除了根之外,您的代码没有向树中添加元素。设置
insertPlace=succRight
仅更新
insertPlace
变量,而不更新树中的任何内容

我建议在寻找正确节点的实际循环中设置左或右节点的值:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct tree
{
   struct node *root;
};


struct node
{
  int val;

  struct node *left;

  struct node *right;
};


struct tree *init ()
{
  struct tree *t = malloc (sizeof (struct tree));
  t->root = NULL;
  return t;
}



void insert (struct tree *t, int val)
{

  struct node *succ;

  struct node *new = malloc (sizeof (struct node));
  new->val = val;
  new->left = NULL;
  new->right = NULL;

  succ = NULL;

  struct node* insertAtNode=t->root;
  if (insertAtNode==NULL) {
    t->root=new;
    return;
  }
  do {
    if (insertAtNode->val < new->val) {
      if (insertAtNode->right==NULL) {
        insertAtNode->right=new;
        return;
      } else {
        insertAtNode=insertAtNode->right;
      }
    } else if (insertAtNode->left==NULL) {
      insertAtNode->left=new;
      return;
    }
    else {
      insertAtNode=insertAtNode->left;
    }
  }
  while(1);
}



void inorderWalk (struct node *p)
{
  if (p != NULL)
  {
    if (p->left != NULL)
      inorderWalk (p->left);
    printf ("%d ", p->val);
    if (p->right != NULL)
       inorderWalk (p->right);
  }
}



void print (struct tree *t)
{
  struct node *p;

  p = t->root;

  inorderWalk (p);
  printf("\n");
}



int main ()
{
  struct tree *t = init ();

  insert (t, 5);

  insert (t, 15);

  insert (t, 20);

  insert (t, 1);

  insert (t, 2);

  insert (t, 4);

  insert (t, 10);


  print (t);


  return 0;

}
#包括
#包括
#包括
结构树
{
结构节点*根;
};
结构节点
{
int-val;
结构节点*左;
结构节点*右;
};
结构树*init()
{
结构树*t=malloc(sizeof(结构树));
t->root=NULL;
返回t;
}
void insert(结构树*t,int val)
{
结构节点*成功;
结构节点*new=malloc(sizeof(结构节点));
新建->val=val;
新建->左=空;
新建->右=空;
succ=NULL;
结构节点*insertAtNode=t->root;
if(insertAtNode==NULL){
t->root=new;
返回;
}
做{
如果(插入节点->值<新建->值){
if(insertAtNode->right==NULL){
插入节点->右=新建;
返回;
}否则{
insertAtNode=insertAtNode->right;
}
}else if(insertAtNode->left==NULL){
插入节点->左=新建;
返回;
}
否则{
insertAtNode=insertAtNode->left;
}
}
而(1),;
}
void inorderWalk(结构节点*p)
{
如果(p!=NULL)
{
如果(p->left!=NULL)
顺序队列(p->左);
printf(“%d”,p->val);
如果(p->right!=NULL)
顺序漫游(p->右);
}
}
无效打印(结构树*t)
{
结构节点*p;
p=t->root;
有序(p);
printf(“\n”);
}
int main()
{
结构树*t=init();
插入(t,5);
插入(t,15);
插入(t,20);
插入(t,1);
插入(t,2);
插入(t,4);
插入(t,10);
打印(t);
返回0;
}

这就像一个符咒!现在节点已正确插入并按正确顺序遍历。非常感谢,保罗!这很有魅力!现在节点已正确插入并按正确顺序遍历。非常感谢,保罗!