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. Angular7.1.4+Typescript3.1框架学习(二)

    接着第一部分,这篇文章就 Angular cli进行介绍总结: 1. ng g:列出当前命令 ng g 需在angular工程文件夹下执行: C:\Users\zb\angulardemo\heroe ...

  2. 笔记《JavaScript 权威指南》(第6版) 分条知识点概要1—词法结构

    [词法结构]字符集.注释.直接量.标识符和保留字.可选的分号 [字符集] JavaScript程序是用Unicode字符集编写的. Unicode是ASCII和Latin-1的超集,支持地球上几乎所有 ...

  3. java前的部分了解(计算机小白)

    一.加密 对称加密: des 3des AES rc4 (数据加密) 会话密钥 非对称加密(成对:公钥/私钥(一个加密一个解密)):RSA DSA 密钥交换 / 数字签名(用私钥加密摘要算法出的一串数 ...

  4. java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

    Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...

  5. Java面向对象编程思想

    面向对象三个特征:封装.继承.多态封装:    语法:属性私有化(private).提供相对应的get/set 的方法进行访问(public). 在set/get的方法中对属性的数据 做相对应的业务逻 ...

  6. JAVA常用异常类

    算数异常类:   ArithmeticExecption 空指针异常类:    NullPointerException 指定类不存在:    ClassNotFoundException 字符串转换 ...

  7. SecureCRT标签显示标题

  8. ES6新语法的介绍

    对于ES6新语法,阮一峰有一篇文章介绍的挺详细 http://es6.ruanyifeng.com/#docs/destructuring

  9. 简单hibernate框架测试

    -jar包 -日志配置文件: -实体类: package cn.itcast.domain; public class Customer { private Long cust_id; //客户编号 ...

  10. 详解angular2组件中的变化检测机制(对比angular1的脏检测)

    组件和变化检测器 如你所知,Angular 2 应用程序是一颗组件树,而每个组件都有自己的变化检测器,这意味着应用程序也是一颗变化检测器树.顺便说一句,你可能会想.是由谁来生成变化检测器?这是个好问题 ...