69,371
社区成员
发帖
与我相关
我的任务
分享
// 节点
struct node_st
{
datatype data;
struct node_st *l;
struct node_st *r;
};
int insert(struct node_st **root, datatype *x)
{
struct node_st *node;
if(*root == NULL)
{
node = malloc(sizeof(*node));
// 如果申请内存失败...
if(node == NULL)
return -1;
node->data = *x;
node->l = node->r = NULL;
*root = node; // 为什么还要有这步操作? 只是为了将插入节点的地址带回去?
return 0;
}
if(*x <= (*root)->data)
return insert(&(*root)->l, x); // 为什么是 &(*root)->l? 等价于 (&(*root))->l?
return insert(&(*root)->r, x);
}
&(*root)->l
等价于&((*root)->l)
,二叉树的左右子树还是二叉树,如果小于根构造左子树,否则构造右子树。