AVL tree rotate
AVL tree
single rotate
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then set new root.
*/
void rotateWithLeftChild( AvlNode * & k2 )
{
AvlNode *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max( height( k2->left ), height( k2->right ) ) + 1;
k1->height = max( height( k1->left ), k2->height ) + 1;
k2 = k1;
}
k2
/ \
k1 z
/ \
x y
|
k1
/ \
x k2
| / \
y z
/**
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
* Update heights, then set new root.
*/
void rotateWithRightChild( AvlNode * & k1 )
{
AvlNode *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max( height( k1->left ), height( k1->right ) ) + 1;
k2->height = max( height( k2->right ), k1->height ) + 1;
k1 = k2;
}
k1
/ \
X k2
/ \
y z
|
-->
k2
/ \
k1 z
/ \ |
x y
doublerotate
/**
* Double rotate binary tree node: first left child.
* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then set new root.
*/
void doubleWithLeftChild( AvlNode * & k3 )
{
rotateWithRightChild( k3->left );
rotateWithLeftChild( k3 );
}
k3
/ \
k1 d
/ \
a k2
/ \
b c
-->
k2
/ \
k1 k3
/ \ / \
a b c d
/**
* Double rotate binary tree node: first right child.
* with its left child; then node k1 with new right child.
* For AVL trees, this is a double rotation for case 3.
* Update heights, then set new root.
*/
void doubleWithRightChild( AvlNode * & k1 )
{
rotateWithLeftChild( k1->right );
rotateWithRightChild( k1 );
}
k1
/ \
a k3
/ \
k2 d
/ \
b c
-->
k2
/ \
k1 k3
/ \ / \
a b c d
AVL tree rotate的更多相关文章
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- 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 ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- 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 ...
- A1066. Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
随机推荐
- 【2】APP自动化-脚本研发2.0-3.0需要重复看视频
calculatorV1.0.py #V1.0 实现手机端计算器自动化测试:使用常量进行参数传递#导入appium类库from appium.webdriver.webdriver import W ...
- Deer_GF之框架介绍
介绍一下Deer_GameFramework_Wolong,这个框架是我自己这几年经验及自己摸索出来缝合出来一套包含优秀库及开发工具可以直接上手快速开发游戏的框架. 缝合东西包括游戏框架G ...
- Bug Bash测试
愿望 养成参加 Bug Bash 的习惯,就像养成到点就吃饭一样的习惯. 一.Bug Bash 名词解释 A Bug Bash is a collaborative effort across o ...
- Java面向对象之封装详解
封装详解 封装 该露的露,该藏的藏 1.我们程序设计要追求"高内聚.低耦合".高内聚:类的内部数据操作细节自己完成,不允许外部干涉:低耦合:仅暴露少量的方法给外部使用. 封装(数据 ...
- HUAWEI--配置单臂路由
HUAWEI--配置单臂路由 1.在LSW1中创建vlan10和vlan20 [LSW1]vlan batch 10 20 2.接口Eth0/0/2配置trunk,放行vlan10和20,接口Eth0 ...
- 查看git的用户名和密码
转载自:https://www.cnblogs.com/xihailong/p/13354628.html 一.查看查看用户名 :git config user.name查看密码: git confi ...
- NVIDIA显卡安装
查看是否有gcc命令. 执行gcc -v 查看相关版本信息.一般完整安装是有此命令的.没有就要自行安装. yum -y install gcc gcc-c++ 修改文件vim /lib/mod ...
- 计算机视觉——SSD和YOLO简介
前言 本文记录用,防止遗忘 计算机视觉--SSD和YOLO简介 课件(单发多框检测SSD) 生成锚框 对每个像素,生成多个以它为中心的锚框 给定n个大小 s1, ...,s2,和m个高宽比,那么生成 ...
- git介绍和常用操作
- spring的aop的粗浅理解
aop有什么用? 假设你写了一本书,写的是一个平民的日常聊天.现在突然你想让这个平民变成一个书生的口气.于是你想在这个平民的每句话之前加上"之乎",后面加上"者也&quo ...