二叉搜索树的源码分析学习 笔记(基础版)
- 全文介绍了二叉搜索树的创建,插入结点,遍历结点,查找结点,最大结点,最小结点,后继结点。
- 删除结点方法,全部源码下载将在下篇博客出现,重点解释删除结点。
- 这篇博客主要看后继结点和插入结点的方法,其他难度都比较小,很容易理解。
二叉搜索树结构
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;
}
二叉搜索树创建好了后,要开始遍历,下面是三种遍历方式。函数的形参是二叉搜索树的根节点指针。
//先序遍历方式
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结点是一个右子结点不含右子结点(下图的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结点。
//查找某个结点的后继结点 比如 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: [email protected]