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的更多相关文章

  1. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  2. AVL Tree (1) - Definition, find and Rotation

    1. 定义 (15-1) [AVL tree]: 一棵空二叉树是 AVL tree; 若 T 是一棵非空二叉树, 则 T 满足以下两个条件时, T 是一棵 AVL tree: T_LeftSubtre ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. LeetCode-1610 可见点的最大数目

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/maximum-number-of-visible-points 题目描述 给你一个点数组 poi ...

  2. 记录篇-浪潮服务器raid卡

    在说raid卡之前,首先,我们要明确,不同的raid卡型号,会有不同的配置方式,但是总体来说是大同小异的 这里举例浪潮机架式服务器经常用到的raid卡型号:  PM8204      (其实像3108 ...

  3. Python中文官方文档

    Python 2.7.18 的中文文档:  https://docs.python.org/zh-cn/2.7/ Python 3.10.6 的官方文档:https://docs.python.org ...

  4. RESTful风格与Spring注解

    RESTfulL是一种网络应用程序的设计风格和开发方式,即接口请求方式和路径的一种风格. 普通风格: localhost:8080/add?a=1&b=2 RestFul风格: localho ...

  5. lg7863

    傻题. 对于相邻的每个点,从高度高的点向高度低的点连边. 依靠差分的思想,设边权是高度差. 考虑第一问,答案显然是这个图dag路径覆盖,可以运行最大流. 考虑第二问.如果每连一条链,那么答案会加上这条 ...

  6. Google Earth Engine——基于改进的RSEI评估生态环境(水体掩膜后)

    未经允许,禁止随意转载,尊重他人版权,仅供学习参考,欢迎交流. 背景介绍 遥感生态指数(Remote Sensing Ecological Index)的获得,是使用主成分分析法耦合了绿度.湿度.干度 ...

  7. 如何将多个TXT合并成一个TXT,文件名称提取

    方法1:1.将所有需要合并的TXT整理到一个文件夹中,切记,TXT合并最好每个TXT内容头或尾留一行间距,因为合并是直接合并,不会保留间距. 2.使用Windows命令cmd,切换到文件所在文件夹 3 ...

  8. UIAutomator API定位元素

    一.根据class name和text属性  包名不可省略 code= new UiSelector().text("我的").classname("android.wi ...

  9. VOLO论文笔记

    Outlook Attention 设给定输入为 \(X \in R^{H \times W \times C}\), 首先经过两个线性映射得到两个输出A 和 V,A叫做outlook weight ...

  10. shell脚本自动过滤尝试多次连接ip并添加到系统黑名单

    #!/bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c | awk '{{ print $2 " ...