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的更多相关文章

  1. HDU 2193 AVL Tree

    AVL Tree An AVL tree is a kind of balanced binary search tree. Named after their inventors, Adelson- ...

  2. 平衡二叉树(AVL Tree)

    在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...

  3. 转载:平衡二叉树(AVL Tree)

    平衡二叉树(AVL Tree) 转载至:https://www.cnblogs.com/jielongAI/p/9565776.html 在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  8. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  9. 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 ...

随机推荐

  1. java Comparable and Comparator

    1.Comparable简介 此接口对实现它的每个类的对象强加一个总排序.这种排序被称为类的自然排序,类的compareTo方法被称为其自然比较方法.可以通过 Collections.sort(和Ar ...

  2. .NET controller传给view的bool类型

    问题: 在view中访问该变量是False,不是字符串,也不是bool类型的false. 解决方法: controller: ViewBag.Test = false; view中得到false值: ...

  3. yml多环境配置

    配置独立各自的环境 注:如果需要修改环境测试,只需要修改spring: profiles: active: “环境名” spring: profiles: active: prd --- #开发环境配 ...

  4. vim 多标签和多窗口

    多文件编辑时的利器啊.谁用谁知道. 多标签 一次性以标签的形式打开多个文件: vim -p *.cpp :tabnew 增加一个标签 :tabc       关闭当前的tab :tabo       ...

  5. 关于Servlet的一些归纳(2)

    1.web项目结构 根路径: 文件夹 文件 WEB-INF: lib(存放一些jar文件) classes(存放class文件) web.xml 2.GenericServlet类 实现了Servle ...

  6. sed命令总结-Linux

    sed命令总结-Linux linuxsed 2018年02月08日 19时27分57秒 命令语法经常忘记,每次总是看笔记不切实际,记不起来的要多查manual,本次总结按照manual总结,希望下次 ...

  7. 异常java.lang.NumberFormatException解决

    原因一:超出了int类型的取值范围 项目中要把十六进制字符串转化为十进制, 用到了到了Integer.parseInt(str1.trim(), 16):这个是不是后抛出java.lang.Numbe ...

  8. Django知识总结(二)

    拾 ● 模型(M) 一 ● ORM(对象关系映射, Object Relational Mapping) 类----一张表 类的实例对象----表中的一条记录 映射关系 ①python的类名对应的SQ ...

  9. [Oracle][DATAGUARD]关于REDO_TRANSPORT_USER参数

    大家可能已经知道,在Oracle的DATAGUARD(这里指的是PHYSICAL STANDBY)环境中,Primary端会把生成的REDO传到Standby端,然后由Standby端的MRP进程应用 ...

  10. MarkdownPan2 简单使用指南

    markdown 简单使用指南 一级标题 二级标题 三级标题加代码 四级标题 这里是加粗 这里是正文and English 888 这里有正文嵌入代码这种样式 这里是代码块 这种使用的代码块 还有引用 ...