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 ...
随机推荐
- PHP的json_encode()函数与JSON对象
一.问题描述 这周搬砖的时候,前端通过ajax获取后端的数据后,照例用 对象.属性 的方式取值,然而结果总是总是不能如预期般展示在页面上. 先写个 demo 还原下场景:选中一个下拉框列表选项后,会在 ...
- 为何存在requests库,pycharm依然报错解决方法 --转载
原文地址:https://www.jianshu.com/p/e28a72ba7809 今天在使用pycharm的时候,用到了第三档库requests,提示有错误,报错显示 No module nam ...
- hive的排序,分組练习
hive的排序,分組练习 数据: 添加表和插入数据(数据在Linux本地中) create table if not exists tab1( IP string, SOURCE string, TY ...
- angular实操
一.创建angular工程(一定要在管理员权限下运行,Windows可忽略) 1.安装angular CLi 在终端窗口打开想要创建的工程所在文件夹,如:Cd Desktop\Angular-proj ...
- eclipse常用工具
Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclipse里 ...
- RK3288 GPIO
简介GPIO, 全称 General-Purpose Input/Output(通用输入输出),是一种软件运行期间能够动态配置和控制的通用引脚.RK3288有9组 GPIO bank: GPIO0,G ...
- 2ci
- HDU2138(Miller-Rabin素数检测)
最近在看RSA,找到一个一个大素数是好多加密算法的关键一步,而大素数无法直接构造,一般情况下都是生成一个随机数然后判断是不是素数.判断是否是素数的方法有好多,有的能够准确判断,比如可以直接因式分解(R ...
- 增强for、iterator迭代器
因为初学java,对部分语法还模棱两可, 在做练习的时候,用增强for遍历字符串编译报错 所以来复习下增强for原理和适用范围 一.增强for概念 增强for(也成为for each循环)是JDK 1 ...
- Git设置旧邮箱与现邮箱不一致问题
设置名字和邮箱git config user.name 'lhr' git config user.email 'lhr@qq.com' 工程根目录创建email.sh文件粘贴以下代码 #!/bin/ ...