二叉搜索树的源码分析学习 笔记(基础版)

  • 全文介绍了二叉搜索树的创建,插入结点,遍历结点,查找结点,最大结点,最小结点,后继结点。
  • 删除结点方法,全部源码下载将在下篇博客出现,重点解释删除结点。
  • 这篇博客主要看后继结点和插入结点的方法,其他难度都比较小,很容易理解。
二叉搜索树结构
typedef struct bst_tree_node
{
    type key;
    struct bst_tree_node * left;
    struct bst_tree_node * right;
    struct bst_tree_node * parent;
} node, * bst_tree;
有了二叉搜索树的存储结构,那么就应该知道怎么建立

先创建一个结点

static node * create_bstree_node (type key, node * parent, node * left, node * right)
{
    node * p;
    if((p = (node * ) malloc(sizeof(node))) == NULL)
        return NULL;
    p->key = key;
    p->left = left;
    p->right = right;
    p->parent = parent;

    return p;
}

创建好结点后,再插入到树中。插入的基本要求要保持二叉搜索树的属性不能被破坏

/**
 * 算法理解方式是找准位置,然后插入。哈哈,很有意思。切记:找准位置最重要
 */
// tree是根结点 z是插入结点
static node * bstree_insert(bst_tree tree, node * z)
{
    node * y = NULL;
    node * x = tree;

    //循环找到适合插入的叶子结点位置
    while(x != NULL)
    {
        y = x;
        if(z->key < x->key)
            x = x->left;
        else if(z->key > x->key)
            x = x->right;
        else
        {
            free(z);    //如果相同key值结点,释放此结点不执行插入
            return tree;
        }
    }
    //找到合适的位置后,再插入结点
    z->parent = y;
    if(y == NULL)
        tree=z;
    else if(z->key < y->key)
        y->left = z;
    else
        y->right = z;
    return tree;
}

结论:y指向插入结点的位置,x指向的是y的子节点。因为带插入结点肯定是叶子结点,所以x也是一个NIL结点。

二叉搜索树创建好了后,要开始遍历,下面是三种遍历方式。函数的形参是二叉搜索树的根节点指针。

//先序遍历方式
void preorder_bsttree(bst_tree tree)
{
    if(tree != NULL)
    {
        printf("%d ", tree->key);
        preorder_bsttree(tree->left);
        preorder_bsttree(tree->right);
    }
}
//中序遍历方式
void inorder_bstree(bst_tree tree)
{
    if(tree != NULL)
    {
        inorder_bstree(tree->left);
        printf("%d ", tree->key);
        inorder_bstree(tree->right);
    }
}
//后序遍历方式
void postorder_bstree(bst_tree tree)
{
    if(tree != NULL)
    {
        postorder_bstree(tree->left);
        postorder_bstree(tree->right);
        printf("%d ", tree->key);
    }
}

根据一个值key查找到对应的结点方法

/**
 * 根据值 key,查找对应的结点并返回对应的地址
 * 第一步要判断这个值 key是否合法,当然了这个是很简单的
 * 下面一步就体现出二叉搜索树的特点了
 *      第一步到根节点,key如果大于根节点那么到右结点下面去比较
 *                     key如果小于根节点那么到左结点下面去比较
 *      就是这样执行下去知道结点为空为止
 */
node * bstree_search(bst_tree tree, type key)
{
    if(tree == NULL || tree->key == key)
        return tree;

    if(key < tree->key)
        return bstree_search(tree->left, key);
    else
        return bstree_search(tree->right, key);
}
取得二叉搜索树中某个结点的后继结点,存在两种情况
  • x右子树不为空,那么它的后继结点就是右子树中最小的一个
  • 如果x结点在根节点的左子树中 且x结点是一个右子结点,且x结点本身不含右子结点(下图的13结点) 。那么它的后继结点就是x的最低父亲结点(最低父亲结点的条件是:父亲结点的左结点等于x祖先)原文的解释:if the right subtree of node x is empty and x has a successor y,
    then y is the lowest ancestor of x whose left child is also an ancestor of x 得出:这里的 x结点就是13结点,y结点就是15结点。
    这种情况也就是,如果x结点在整个树的右子树中,且x是右子树的最大结点,那么他的后继结点就是这个树的根节点。
//查找某个结点的后继结点 比如 1 2 3 4 5,查找3的后继结点就是4
node * bstree_successor(node * x)
{
    //如果x存在右子树,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"
    if(x->right != NULL)
        return bstree_minimum(x->right);
    node * y = x->parent;
    while((y!=NULL) && (x == y->right))
    {
        x=y;
        y = y->parent;
    }
    return y;
}
/**
 * 二叉搜索树是一个很有意思的东西,
 *  如果要寻找是最大的结点,那么就在右子树里寻找,如果右子树为空,那么就是当前的根节点
 *  同理:如果要寻找是最小的结点,那么就在左子树里寻找,如果左子树为空,那么就是当前的根节点
 */
//左子树最左一个结点值是最小的一个结点
node * bstree_minimum(bst_tree tree)
{
    if(tree == NULL)
        return NULL;
    while(tree->left != NULL)
        tree = tree->left;
    return tree;
}
//右子树最右的一个结点值是最大的一个结点
node * bstree_maximum(bst_tree tree)
{
    if(tree == NULL)
        return NULL;
    while(tree->right != NULL)
        tree= tree->right;
    return tree;
}

删除结点的方法,在下一章,请点击

参考内容:Introduction to Algorithms [MIT press]
如果存在错误请email me: chenrui@marsdl.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值