设为首页 加入收藏

TOP

二叉搜索树的详解(算法导论读书笔记) (三)
2014-11-23 20:10:28 来源: 作者: 【 】 浏览:15
Tags:搜索 详解 算法 导论 读书 笔记
个节点且该节点是左孩子,那么用其左孩子来替换node
3. 否则node既有一个左孩子又有一个右孩子,我们要查找node的后继y,这个后继位于node的右子树中并且没有左孩子,现在需要将y移除原来的位置进行拼接,并替换树中的node
3.1. 如果y是z的node的右孩子,那么用y替换node
3.2 否则,y位于z的右子树中但并不是z的右孩子,这种情况下,先用y的右孩子替换y,然后用y替换node(最后步骤同3.1)
为了在二叉搜索树内移动子树,定义一个函数bst_transplant,它用凉意可子树替换一棵字数并成为双亲的孩子节点。以下实现为用一棵以v为根的子树来替换一棵以u为根的子树,节点u的双亲就变为节点v的双亲,并且最后v成为u的双亲的相应孩子


[cpp]

//replace subtree of u with subtree of v  
BST *bst_transplant(BST *root, BST *u, BST *v)
{
if(u->parent==NULL) //u is root
root = v;
else if(u->parent->right == u) // u is his parent's right subtree
u->parent->right = v;
else u->parent->left = v; //u is his parent's left subtree
if(v!=NULL) //set v's parent
v->parent = u->parent;
return root;
}

 
 
 

[cpp]
//replace subtree of u with subtree of v BST *bst_transplant(BST *root, BST *u, BST *v) { if(u->parent==NULL) //u is root root = v; else if(u->parent->right == u) // u is his parent's right subtree u->parent->right = v; else u->parent->left = v; //u is his parent's left subtree if(v!=NULL) //set v's parent v->parent = u->parent; return root; } //replace subtree of u with subtree of v
BST *bst_transplant(BST *root, BST *u, BST *v)
{
if(u->parent==NULL) //u is root
root = v;
else if(u->parent->right == u) // u is his parent's right subtree
u->parent->right = v;
else u->parent->left = v; //u is his parent's left subtree
if(v!=NULL) //set v's parent
v->parent = u->parent;
return root;
}

树的删除代码如下
[cpp]
BST *bst_delete(BST *root, data_t data)
{
BST *node = NULL, *y = NULL;
node = bst_search(root, data); //find the deleted node
if(node==NULL)
return NULL;
if(node->left == NULL) //case 1
root = bst_transplant(root, node, node->right);
else if(node->right == NULL) //case 2
root = bst_transplant(root, node, node->left);
else
{
y = bst_mininum(node->right); //find node's successor
if(y->parent!=node) //case 3 follow make y's parent to node
{
root = bst_transplant(root, y, y->right);
y->right = node->right;
node->right->parent = y;
}
//case 3 y->parent == node
root = bst_transplant(root, node, y);
y->left = node->left;
y->left->parent = y;
}
return root;
}

BST *bst_delete(BST *root, data_t data)
{
BST *node = NULL, *y = NULL;
node = bst_search(root, data); //find the deleted node
if(node==NULL)
return NULL;
if(node->left == NULL) //case 1
root = bst_transplant(root, node, node->right);
else if(node->right == NULL) //case 2
root = bst_transplant(root, node, node->left);
else
{
y = bst_mininum(node->right); //find node's successor
if(y->parent!=node) //case 3 follow make y's parent to node
{
root = bst_transplant(root, y, y->right);
y->right = node->right;
node->right->parent = y;
}
//case 3 y->parent == node
root = bst_transplant(root, node, y);
y->left = node->left;
y->left->parent = y;
}
return root;
}


首页 上一页 1 2 3 下一页 尾页 3/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)