以下是之前我们老师讲的二叉树插入数据时的操作, 数据小则插入左边, 大则右边. 疑问我写到注释里, 求解惑.
// 节点
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);
}