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 ...
随机推荐
- Jenkins的安装配置和使用
Jenkins的安装配置和使用 1 Jenkins介绍 w3cschool中这样介绍:Jenkins是一个独立的开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个 ...
- MongoDB之 分组查询
分组查询 可视化工具 https://robomongo.org pymongo from pymongo import MongoClient # 方式一: c = MongoClient(host ...
- GNOME 系统设置
详细的GNOME系统设置全文,参见这里. 以下摘录使用到的部分. 1. 在任务栏上显示日期或周几(二选一).秒数 $ gsettings set org.gnome.desktop.interface ...
- java 编译
package javacodeforstudy.testcode; public class Helloworld{ public static void main(String[] args) { ...
- VC调用外部程序
#include <windows.h> int main() { STARTUPINFO mStatusInfo; memset(&mStatusInfo, 0, sizeof( ...
- 关于input
form表单的使用: 1.iuput:type:类型有很多常用的: text:输入的内容为文本格式(内容可见) password:输入的内容为......(内容不可见) radio:显示为单选框(框为 ...
- 【转载】Excel 三维地图入门
三维地图入门(office 2016) https://support.office.com/zh-cn/article/%E4%B8%89%E7%BB%B4%E5%9C%B0%E5%9B%BE%E5 ...
- python学习------面向对象的程序设计
一 面向对象的程序设计的由来 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数 据.简单来说,就是直接编写 和 的序列来代表程序语言 ...
- python之路-python2.x与python3.x区别
Python崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. Python2.x 与 Python3.x的区别: python2.x:源码混乱,重复代码较多,冗余. python3.x:源码规范,崇 ...
- JAVAEE 第八周
equals():反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值. hashCode():计算出对象实例的哈希码,并返回哈希码,又称为散列函数.根类Obje ...