首页 > 二叉树插入函数的一个细节,另外翻译一个词组

二叉树插入函数的一个细节,另外翻译一个词组

int bitree_ins_left(BiTree *tree, BiTreeNode *node, const void *data)
{
  BiTreeNode *new_node, **position;        //这里为什么要二重指针?   *position不行吗?
  if( node == NULL )
  {
        if( bitree_size(tree) > 0 )
        return -1;
        position = &tree->root;
  }
  else
  {
        if( bitree_left(node) != NULL )
        return -1;
        position = &node->left;
  }
  /* Allocate storage for the node */
  new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
  if( new_node == NULL )
         return -1;
  /* insert the node into the tree */
  new_node->data = (void *)data;
  new_node->left = NULL;
  new_node->right = NULL;
  *position = new_node;
    //Adjust the size of the tree to account for the inserted node.
    //上面的account for是什么意思?
  tree->size++;
  return 0;
}

http://bbs.csdn.net/topics/390828266
这里的account for是对...负责


首先你要明白c++里面的类型是什么东西,**和*是完全不同的两个类型,那什么时候决定你用哪个呢? 看需求,另外去参看参数的传递规则,看看值传递和引用传递都是什么意思

account for 按照金山词霸的解释是 解释,引起,导致 的意思


node->left是指针,&node->left是指针的指针,因此position 需要定义为二重指针。
to account for 考虑到
整句话的意思:调整树的大小考虑插入节点(因插入节点的影响)。


目测楼主看的应该是我大天朝的C语言数据结构丛书吧。再目测一下这里的struct BiTreeNode里面应该有一个叫struct BiTreeNode *next的成员吧。那为什么还要写个struct BiTreeNode **position呢?大概这就是程序员版的画蛇添足!

至于这句话Adjust the size of the tree to account for the inserted node.,我英语学的也不太好,大概to account for the inserted node这个从句是用来修饰前面那个tree的,但这句话后面有个语句tree->size++;我很清楚,这不就是插入结点后更新树的大小吗?

【热门文章】
【热门文章】