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. 从零开始学Python 三(网络爬虫)

    本章由网络爬虫的编写来学习python.首先写几行代码抓取百度首页,提提精神,代码如下: import urllib.request file=urllib.request.urlopen(" ...

  2. CQRS粗浅理解

    CQRS(命令查询责任分离)是一种奇特的模式,表示解耦系统的输入和输出. 通常情况下,输入端将数据写到数据库,输出端从数据库查询.与读写锁的场景类似,写的过程中不能读.正常情况下没有问题,但是在大规模 ...

  3. python数据结构与算法之单链表

    表的抽象数据类型 ADT list:                      #一个表的抽象数据类型 List(self)               #表的构造操作,创建一个空表 is_empty ...

  4. c++中各类型数据所占字节数(一)

    转自: https://blog.csdn.net/hi_baymax/article/details/82415896 和机器字长及编译器有关系: 所以,int,long int,short int ...

  5. mongodb数据库操作方法

    // Schema.Model.Entity或者Documents的关系请牢记,Schema生成Model,Model创造Entity,Model和Entity都可对数据库操作造成影响,但Model比 ...

  6. centos7上搭建http服务器以及设置目录访问

    参考文献:http://www.jb51.net/article/137596.htm,原文摘抄如下,并根据具体需要作了相应的修改. 步骤: 1. 安装httpd服务 sudo yum install ...

  7. eclipse光标变粗解决方法

    如上图,光标变成黑块好像没那么顺眼,原因是我们不小心按到了insert键造成的,再按一下insert键就OK了,如果delete和insert是一个键那么就用FN键+delete/insert键就可以 ...

  8. Python3的string库模板的应用

    模板 字符串模板将作为内置的拼接语法的替代用法.使用Template拼接时,要在名字前加前缀$来标识变量(例如,$var).或者,如果有必要区分变量和周围的文本,可以用大括号包围变量(例如,${var ...

  9. Judy Beta 第二天

    Intro 我们采取的code review方式是两人一组,每个人在自己的分支上完成工作后向master分支发起pull request,小组的另一人对pr进行review后merge进入原仓库.gi ...

  10. mysql5.7.20 windows 解压缩版安装

    1.下载 文件下载路径:https://dev.mysql.com/downloads/mysql/ 2.配置文件 在解压的文件夹内新建my.ini文件,并加入以下内容: [mysql] # 设置my ...