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 ...
随机推荐
- 简单的jQuery hide()方法演示
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>jQu ...
- es6,es7,es8
概述 ES全称ECMAScript,ECMAScript是ECMA制定的标准化脚本语言.目前JavaScript使用的ECMAScript版本为ECMAScript-262. ECMAScript 标 ...
- argument 1 must be 2-item sequence, not int
看了 https://blog.csdn.net/qq_18250439/article/details/80872425 的说明,才明白为什么错了. 当使用Python3-pygame时出现Typ ...
- null 和System.DBNull.Value
row[column]的值为DBNull.Value的话,说明它是从数据库中取到值了,对应了数据库中的空值:但如果row[column]的值为null的话,说明没有从数据库中取到值. DBNull.V ...
- 关于python中的GIL
什么是GIL锁? GIL是Global Interpreter Lock的缩写,GIL中文可以称为全局解释器锁.提及到GIL,我们要知道它是在实现Python解析器(CPython)时所引入的一个概念 ...
- multiprocessing
multiprocessing multiprocessing模块是跨平台版本的多进程模块. multiprocessing模块提供了一个Process类来代表一个进程对象. multiprocess ...
- java -jar启动java项目时,引用不同配置文件命令
java -jar eureka-server.jar --spring.profiles.active=peer1
- oracle新建表空间的四条语句
1.create user platform identified by intest; 2.create tablespace PLATFORM_DATA datafile 'c:\PLATFORM ...
- python操作samba
最近在部署完xxl-job后,陆续将一些日常性执行的python脚本迁移到上面去:其中部分脚本涉及到对samaba的操作,先后尝试了pysmb.fs.smbfs.pysmbclient pysmb 安 ...
- linux(ubuntu)GCC编译包含库函数的问题
GCC 编译命令通常为:gcc hello.c -o hello.out 注意:若hello.c中引用有库函数(比如math.h),直接编译会出错 "/tmp/ccalvMPY.o: In ...