AVL Tree Insertion
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的更多相关文章
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- 平衡二叉树(AVL Tree)
在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...
- 转载:平衡二叉树(AVL Tree)
平衡二叉树(AVL Tree) 转载至:https://www.cnblogs.com/jielongAI/p/9565776.html 在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简 ...
- AVL Tree (1) - Definition, find and Rotation
1. 定义 (15-1) [AVL tree]: 一棵空二叉树是 AVL tree; 若 T 是一棵非空二叉树, 则 T 满足以下两个条件时, T 是一棵 AVL tree: T_LeftSubtre ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- webpack基本配置文件(含解释)
const path = require('path'); // 以下文件需要npm i 文件名 --save-dev const uglify = require('uglifyjs-webpack ...
- ZedBoard前期准备工作
1. 资源下载 内核:https://github.com/Xilinx/linux-xlnx/releases uboot:https://github.com/Xilinx/u-boot-xlnx ...
- 八大排序算法详解(动图演示 思路分析 实例代码java 复杂度分析 适用场景)
一.分类 1.内部排序和外部排序 内部排序:待排序记录存放在计算机随机存储器中(说简单点,就是内存)进行的排序过程. 外部排序:待排序记录的数量很大,以致于内存不能一次容纳全部记录,所以在排序过程中需 ...
- PCA算法数学原理及实现
数学原理参考:https://blog.csdn.net/aiaiai010101/article/details/72744713 实现过程参考:https://www.cnblogs.com/ec ...
- xcode打包苹果应用遇到的问题及解决方法
1.手机升级到iOS 10之后,运行真机出现了Development cannot be enabled while your device is locked. 原因分析: 这里是你对这台电脑设置了 ...
- java课堂笔记4
- winfrom程序Datagridview列名问题
之前在做程序的时候,有遇到过这个问题: 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColume"解决方法 解决办法 ...
- Data assimilation
REF: https://en.wikipedia.org/wiki/Data_assimilation Data assimilation is the process by which obser ...
- IIS10安装IIS URL重写模块2伪静态组件提示需要IIS7.0以上版本。
解决方法: 打开注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp 双击右边MajorVersion,默认的数值是a,修改成9,然后再安装 ,安装完成后 ...
- JSP学习(2)---四种基本语法与三种编译指令
JSP的异常可以不处理,即使是checked异常. 四种基本语法: jsp声明,jsp注释,jsp表达式,jsp脚本 三种编译指令: page,include,taglib 下面是具体的练习. sho ...