Overview

AVL tree is a special binary search tree, by definition, any node, its left tree height and right tree height difference is not more than 1. The purpose of AVL tree is to try best to reduce the search time complexity. if the binary search tree is too deep, that will increase the search cost. By converting it to AVL, the search time can be stably controlled within O(LogN).

Data Structure and High Level API

 public class AVLTreeNode
{
public int data
{
get;
set;
} public AVLTreeNode leftChild
{
get;
set;
} public AVLTreeNode rightChild
{
get;
set;
} public int height
{
get;
set;
} public AVLTreeNode(int data)
{
this.data = data;
this.height = ;
}
}

AVLTreeNode Insert(AVLTreeNode node, int key)

Recursion 

INParam: current root node, incoming key
OUT: the new root node
AVLNode Insert(AVLTreeNode node, int key)
=
) if node is null, return new AVLNode(key)
) if key is greater than current node, node.rightChild = Insert(node.rightChild, key)
) if key is smaller than current node, node.leftChild = Insert(node.leftChild, key)

Source Code

   public class AVLTree
{
public AVLTreeNode root
{
get;
set;
} private int Height(AVLTreeNode node)
{
if (node == null)
{
return ;
} if (node.leftChild == null && node.rightChild == null)
{
return ;
}
else if (node.leftChild == null)
{
return + node.rightChild.height;
}
else if (node.rightChild == null)
{
return + node.leftChild.height;
} return + Math.Max(node.leftChild.height, node.rightChild.height);
} private int GetBalance(AVLTreeNode node)
{
if (node == null)
{
return ;
} return Height(node.leftChild) - Height(node.rightChild);
}
      // 右旋,左孩子和右孙子
private AVLTreeNode RightRotation(AVLTreeNode node)
{
AVLTreeNode childNode = node.leftChild;
AVLTreeNode grandChildNode = childNode.rightChild;
childNode.rightChild = node;
node.leftChild = grandChildNode; // for affected nodes, update their height
node.height = Math.Max(Height(node.leftChild), Height(node.rightChild)) + ;
childNode.height = Math.Max(Height(childNode.rightChild), Height(childNode.leftChild)) + ; return childNode;
}

    // 左旋
    // 右孩子和左孙子
private AVLTreeNode LeftRotation(AVLTreeNode node)
{
AVLTreeNode childNode = node.rightChild;
AVLTreeNode grandChildNode = childNode.leftChild;
childNode.leftChild = node;
node.rightChild = grandChildNode; node.height = Math.Max(Height(node.leftChild), Height(node.rightChild)) + ;
childNode.height = Math.Max(Height(childNode.rightChild), Height(childNode.leftChild)) + ; return childNode;
} public AVLTreeNode Insert(AVLTreeNode node, int key)
{
if (node == null)
{
return new AVLTreeNode(key);
} if (key < node.data)
{
node.leftChild = Insert(node.leftChild, key);
}
else
{
node.rightChild = Insert(node.rightChild, key);
} node.height = + Math.Max(Height(node.leftChild), Height(node.rightChild)); // after insertion, calculate the balance
int balance = GetBalance(node); // left left case
if (balance > && node.leftChild.data > key) // balance is greater than 1, which means node must have left child.
{
// right rotation
return RightRotation(node); // 向右转, 左左组合,入参是当前根节点, 要动谁,谁就是参数
} // left right case
        6
/
4
\
        5
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); // 右旋,传入参数是node rightChild
// left rotation
return LeftRotation(node);
} return node;
} public void InOrder(AVLTreeNode node)
{
if (node == null)
{
return;
} InOrder(node.leftChild);
Console.WriteLine(node.data);
InOrder(node.rightChild);
}
}

左旋和右旋分别cover两种情况,举个例子,右旋既可以解决左左的旋转,也可以解决右侧子树, 右左的情况。所以旋转函数只有两个API,左旋和右旋,no more choice!

AVL Tree Insertion的更多相关文章

  1. 树的平衡 AVL Tree

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

  2. 平衡二叉树(AVL Tree)

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

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

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

  4. AVL Tree (1) - Definition, find and Rotation

    1. 定义 (15-1) [AVL tree]: 一棵空二叉树是 AVL tree; 若 T 是一棵非空二叉树, 则 T 满足以下两个条件时, T 是一棵 AVL tree: T_LeftSubtre ...

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

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

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

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

  9. A1123. Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. mysql8.0修改密码无效的问题

    今天安装了mysql8,但是在修改默认密码的时候发现一直无法成功,下面给出解决的办法. 一直报ERROR 1064 (42000): You have an error in your SQL syn ...

  2. react - next.js 设置body style

    因为next.js可以用pages文件夹中的js文件进行route,所以不需要public文件夹和html,因此没有body tag. body自带8px的maigin,我想要给整个页面设置背景颜色, ...

  3. js中Array数组基本方法

    总结:push() 添加元素到数组未尾, 返回数组长度 unshift() 添加元素到数组头部, 返回数组长度 pop() 删除数组未尾元素, 返回删除元素 shift() 删除数组头部元素, 返回删 ...

  4. VM Linux版本安装

    安装方法 https://www.jb51.net/softs/619194.html 1. 增加执行权限: sudo chmod +x VMware-Workstation-Full-14.1.2- ...

  5. STL 小白学习(9) 对组

    void test01() { //构造方法 pair<, ); cout << p1.first << p1.second << endl; pair< ...

  6. 常用算法的python实现

    1.乘法表 #! -*- coding:utf-8 -*- for i in range(1,10): for j in range(1,i+1): print('%d*%d=%d\t' % (i, ...

  7. 利用this属性实现点击按钮变色.选中效果

    浏览器宿主的全局环境中,this指的是window对象. <script type="text/javascript"> console.log(this === wi ...

  8. JQuery 方法合集(懒人备记)

    原创文章,转载请私信.谢谢~ PS:请将jquery的引用文件放在head的标签内 语法:$(selector).action() $(document).ready(function(){ // 开 ...

  9. UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  10. Django--Managers

    Django--Managers Manager 概念: 1.Manager是Django中的数据模型,可以通过manager进行对数据库的查询操作.可以看其结构它本身是一个空的类,其主要的功能来自于 ...