AVL Tree Deletion
Overview
知识点:
1. delete函数的signature
public AVLTreeNode Delete(AVLTreeNode node, int key)
2. 算法,如何删除节点:
// 如果左右节点都为空,node = null
// 如果一个为空,另一个字节点不为空,把node节点替换成不为空的字节点
// 如果两个节点都不为空,则要找到中序后继节点,然后去其值,再递归删掉右侧子树的后继节点
3. 旋转:
左旋和右旋逻辑和插入是一致的。
Source Code
private int GetMinValue(AVLTreeNode node)
{
if (node == null)
{
throw new Exception("node is null.");
} if (node.rightChild != null)
{
AVLTreeNode temp = node.rightChild;
while (temp.leftChild != null)
{
temp = temp.leftChild;
}
// don't write it like temp.leftChild.data
return temp.data;
}
else
{
throw new Exception("successor node is not found");
}
} public AVLTreeNode Delete(AVLTreeNode node, int key)
{
// STEP 1: standard BST deletion
if (node == null)
{
return node;
} if (key < node.data)
{
node.leftChild = Delete(node.leftChild, key);
}
else if (key > node.data)
{
node.rightChild = Delete(node.rightChild, key);
}
else
{
// 如果左右节点都为空,node = null
// 如果一个为空,另一个字节点不为空,把node节点替换成不为空的字节点
// 如果两个节点都不为空,则要找到中序后继节点,然后去其值,再递归删掉右侧子树的后继节点
if (node.leftChild == null || node.rightChild == null)
{
AVLTreeNode temp = null;
if (node.leftChild == null)
{
temp = node.rightChild;
}
else
{
temp = node.leftChild;
} if (temp == null)
{
// no child at all
node = null;
}
// has one child
else
{
node = temp;
}
}
else
{
// has two children
node.data = GetMinValue(node);
node.rightChild = Delete(node.rightChild, node.data);
}
}
// 下面这个逻辑很重要,如果node是叶子节点,直接返回,没有必要继续下去
if (node == null)
{
return node;
} // STEP 2: update height, 下面逻辑和插入是一样的
node.height = + Math.Max(Height(node.leftChild), Height(node.rightChild)); // STEP 3: calculate balance factor
// after insertion, calculate the balance
int balance = GetBalance(node); // left left case
if (balance > && node.leftChild.data > key)
{
// right rotation
return RightRotation(node);
} // left right case
if (balance > && node.leftChild.data <= key)
{
// left rotation first
node.leftChild = LeftRotation(node.leftChild);
// then do right rotation
return RightRotation(node);
} // right right case
if (balance < - && node.rightChild.data <= key)
{
// left rotation
return LeftRotation(node);
} // right left case
if (balance < - && node.rightChild.data > key)
{
// right rotation
node.rightChild = RightRotation(node.rightChild);
// left rotation
return LeftRotation(node);
} return node;
}
AVL Tree Deletion的更多相关文章
- HDU 2193 AVL Tree
AVL Tree An AVL tree is a kind of balanced binary search tree. Named after their inventors, Adelson- ...
- 平衡二叉树(AVL Tree)
在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...
- 转载:平衡二叉树(AVL Tree)
平衡二叉树(AVL Tree) 转载至:https://www.cnblogs.com/jielongAI/p/9565776.html 在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简 ...
- 04-树5 Root of AVL Tree
平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...
- 1066. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 1066. Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child su ...
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- 1123. Is It a Complete AVL Tree (30)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
随机推荐
- url路由配置以及渲染方式
路由分配及模板渲染 路由系统 urlpatterns = [ path('admin/', admin.site.urls), path('teacher/',include('teacher.url ...
- Anaconda与Spyder升级命令
step1:首先以管理员的身份启动cmd.exe: step2:升级conda(升级Anaconda前需要先升级conda)命令为:conda update conda step3:升级anacond ...
- 7、TypeScript数据类型
1.变量声明 var 不要使用 建议使用: let 变量 const 常量,一旦申明不能修改 2.数据类型 2.1布尔值:boolean 2.2数字类型 :number 2.3字符串类型:strin ...
- Netty(一)——Netty入门程序
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7447618.html 有兴趣的可先了解下:4种I/O的对比与选型 主要内容包括: Netty开发环境的搭建 ...
- lr介绍
---恢复内容开始--- loadrunner是通过agent进程来监控各种协议的客户端和服务端的通信: init和end不能进行迭代,action才能迭代(参数化才有作用) init(比如说有50个 ...
- js的事件流事件机制
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...
- 为虚机Linux系统设置静态IP,ping通外网并解决相关问题
在虚机中安装完Linux系统后,虚机是ping不通外网的,而默认的动态IP会为之后的Hadoop应用造成不少麻烦,为了减少这些不必要的麻烦,我们把系统的IP设置为静态. 步骤: 修改系统配置文件 命令 ...
- bootstrap modal 监听滚动条事件
bootstrap modal 里面使用datetimepicker时间控件,滚动时,时间控件并不会隐藏,这是一个小bug,在组里改下,当滚动条滚动时,直接隐藏这个时间控件,$("#alar ...
- Java并发编程阅读笔记-锁和活跃性问题
- 代码规范mark一下
转自于:https://github.com/zh-google-styleguide/zh-google-styleguide/blob/master/google-python-styleguid ...