c - 插入二叉树

标签 c binary-tree

如何递归地在二叉树中执行插入,使其始终从左侧填充???以下是我编写的代码,这当然是错误的...我在递归方面有点弱...请提供您的建议...

void insert(node **root,int n)
{
     node *temp;
     temp=(node *)malloc(sizeof(node));
     temp->data=n;
     temp->lchild=NULL;
     temp->rchild=NULL;
     if(*root==NULL)
     {
         *root=temp;
         return;
     }
     if((*root)->lchild==NULL)
     {
          (*root)->lchild=temp;
          return;
     }
     if((*root)->rchild==NULL)
     {
          (*root)->rchild=temp;
          return;
     }
     insert(&((*root)->lchild),n);
     insert(&((*root)->rchild),n);
     return;
}

最佳答案

  1. 不要在 C 程序中转换 malloc 的返回值。
  2. 您的程序可能会将 n 插入到两个子树中。

关于c - 插入二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669372/

相关文章:

c - 读取由同一驱动程序的其他字符设备写入一个字符设备的数据

data-structures - Rope 数据结构解释?

PHP二叉树递归算法

c - iconv 库不正确地将 UTF-8 转换为 KOI8-R

c - C语言编程中的二分法

CUDA:使用网格跨步循环减少共享内存

java - Java中如何向完全二叉树插入节点?

java - 如何将字符串转换为泛型类型<E>?

c - 在二叉树中搜索元素,函数总是返回0

C程序内存映射