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

标签 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 *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;

}

代码也可用here使用在线 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;

}

关于c - 二叉树 InOrderWalk 实现未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43821424/

相关文章:

c- 分配和释放二维数组

c - 为什么当我输入指向字符串文字的指针时,程序会返回段错误,但当我输入指向数组的指针时,程序不会返回段错误?

C 字符数组在传递给函数后损坏

gdb - 我可以打印带有前导零的 rax 寄存器的内容吗?

c - 在 C 中向字符串添加子字符串时重新分配内存的最佳方法

c - c 中多线程的错误输出?

c - 如何从带有指针的函数返回数组

c++ - 当设置 SDL_WINDOW_VULKAN 标志时,SDL_CreateWindow 失败

c - gdb:仅当调用函数不等于某个值时才会有条件地中断函数

c - 在 python 中调试 c 扩展