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 ...
随机推荐
- js控制关闭layui的switch开关
<input class="switch" type="checkbox" lay-skin="switch" lay-filter= ...
- CMT: Convolutional Neural Networks Meet Vision Transformers概述
0.前言 相关资料: arxiv github 论文解读(CSDN,CSDN) 论文基本信息: 作者单位:华为诺亚, 悉尼大学 发表时间:CVPR2022(2021.7.13) 1.针对的问题 当前将 ...
- C# DevExpress GridControl中BandedGridView表格使用
1.设计器方式创建 1.点击"Change view">>然后在选中"Convert to"选项>>最后选中点击"Banded ...
- python ddt file_data
# -*- coding: utf-8 -*-"""------------------------------------------------- File Name ...
- grafana嵌入iframe,websoket连接报错400或403(nginx代理)
1.custom.ini配置文件修改allowed_origins=* 2. nginx中增加配置,如下:
- vue中input触发方法中调用ajax,导致input失去焦点问题
发现在vue中监控input绑定的值,调用方法时,如果方法中有调用后端接口(使用Ajax),会导致input输入框失去焦点,这样导致的问题就是每输入一个字符,就要重新聚焦一次,可通过以下方式在方法中重 ...
- 2022-05-13内部群每日三题-清辉PMP
1.一个运营团队认为他们的技能在项目上是不必要的,团队士气低落,且团队成员试图阻止项目实现目标.项目经理应该怎么做? A.建议公司改变战略,并立即停止项目 B.要求工会的支持来激励团队 C.根据项目成 ...
- git基础代码获取
1.新建文件夹 2.使用命令git init 初始化 3.见你与远程厂库的连接,git remote add origin 远程仓库地址 4.将远程仓库代码拉去到本地,git fetch origin ...
- Software_programming_tools_IDEA
13:45:55 key promtoer 快捷键提示插件
- 32位机转化11位手机号以及BLE与USB的切换
目录 用现有的资源,去实现本应该用更多资源来实现的需求,是一件很有意思的事情.不是说提倡这样使用,而是换一种思路解决问题比较新奇,或是在很多限制既定的情况下可以应急. 比如说,582m芯片,默认用32 ...