设为首页 加入收藏

TOP

二叉搜索树的详解(算法导论读书笔记) (二)
2014-11-23 20:10:28 来源: 作者: 【 】 浏览:13
Tags:搜索 详解 算法 导论 读书 笔记
f tree
BST *bst_mininum(BST *root)
{
if(root == NULL)
return NULL;
while(root->left!=NULL)
root = root->left;
return root;
}

//return the maxest node of tree
BST *bst_maxinum(BST *root)
{
if(root == NULL)
return NULL;
while(root->right!=NULL)
root = root->right;
return root;
}
注,以上两个算法的时间复杂度都是O(logn) (n为输得节点的个数)


节点的前驱和后继
给定一棵二叉搜索树的一个节点,有时候需要按中序遍历的次序查找它的后继。如果所有的关键字互不相同,则一个节点x的后继是大于x.key的最小关键字的节点。
查找前驱过程分为两部分:1. 如果x的右子树非空,那么x的后继则是x右子树的最左节点;2. 如果x的右子树为空且有一个后继节点y,那么y就是最底层祖先并且其左孩子也是一个祖先。为了找到y,只需从x开始沿树而上知道遇到一个其双亲有左孩子的节点
查找节点的前驱则先判断x节点的左子树是否为空,非空则前驱就是左子树的最右节点,空则从x沿树而上直到遇到一个其双亲都有右孩子的节点,那么该节点就是x的前驱结点
下面为实现代码
[cpp]
BST *bst_successor(BST *node)
{
if(node == NULL)
return NULL;
if(node->right!=NULL) //find successor in the leftest of the right subtree
return bst_mininum(node->right);
//else find it a node leftSubTree from his parent
BST *y = node->parent;
while(y!=NULL && node==y->right)
{
node = y;
y = y->parent;
}
return y;
}

BST *bst_predecessor(BST *node)
{
if(node == NULL)
return NULL;
if(node->left!=NULL)
return bst_maxinum(node->left);
BST *y = node->parent;
while(y!=NULL && node==y->left)
{
node = y;
y = y->parent;
}
return y;
}

BST *bst_successor(BST *node)
{
if(node == NULL)
return NULL;
if(node->right!=NULL) //find successor in the leftest of the right subtree
return bst_mininum(node->right);
//else find it a node leftSubTree from his parent
BST *y = node->parent;
while(y!=NULL && node==y->right)
{
node = y;
y = y->parent;
}
return y;
}

BST *bst_predecessor(BST *node)
{
if(node == NULL)
return NULL;
if(node->left!=NULL)
return bst_maxinum(node->left);
BST *y = node->parent;
while(y!=NULL && node==y->left)
{
node = y;
y = y->parent;
}
return y;
}
树的插入与删除
树的插入与删除操作会引起二叉搜索树表示的动态集合的变化,一定要修改数据结构来反映这个变化,但修改要保持二叉树搜索树的性质的成立
插入
要将一个新值插入到一棵二叉搜索树T中,需要调用bst_insert。该函数以关键字v作为输入,在函数构造节点node,并将node->left=NULL,node->right=NULL
插入过程为:从树根开始,指针node记录一条向下的简单路锦,并查找要替换新节点insert的NULL,该遍历保持tmo作为node的双亲
[cpp]
//insert a node into a binary-search-tree
BST *bst_insert(BST *root, data_t data)
{
BST *insert = NULL, *node = NULL, *tmp = NULL;
insert = bst_newnode(data); //make a node to insert

node = root;
while(node) //find pos to insert
{
tmp = node;
if(data < node->data)
node = node->left;
else
node = node->right;
}

insert->parent = tmp;
if(tmp==NULL) //tree root is empty
root = insert;
else
{
if(data < tmp->data)
tmp->left = insert;
else
tmp->right = insert;
}

return root;
}

//insert a node into a binary-search-tree
BST *bst_insert(BST *root, data_t data)
{
BST *insert = NULL, *node = NULL, *tmp = NULL;
insert = bst_newnode(data); //make a node to insert

node = root;
while(node) //find pos to insert
{
tmp = node;
if(data < node->data)
node = node->left;
else
node = node->right;
}

insert->parent = tmp;
if(tmp==NULL) //tree root is empty
root = insert;
else
{
if(data < tmp->data)
tmp->left = insert;
else
tmp->right = insert;
}

return root;
}


树的删除
从一个二叉搜索树T中删除一个节点z,这个过程取决与指向root和待删除节点node,以下为该过程的四种情况
1. 如果node没有左孩子,那么有其右孩子来替换node,这个右孩子可以是NULL,也可以不是
2. 如果node仅有一

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇矩阵快速幂模版 下一篇提取用','分割的单词

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C 内存管理 | 菜鸟教 (2025-12-26 20:20:37)
·如何在 C 语言函数中 (2025-12-26 20:20:34)
·国际音标 [ç] (2025-12-26 20:20:31)
·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)