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-794 有效的井字游戏

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/valid-tic-tac-toe-state 题目描述 用字符串数组作为井字游戏的游戏板 boa ...

  2. Spring的注入方式

    Spring的注入方式 目录 Spring的注入方式 一.前言 二.常见的三种注入方式 2.1.Field注入 2.2 构造器注入 2.3 setter注入 三.构造器注入的好处 四.答疑 一.前言 ...

  3. 2、flex最后不对齐问题

    https://www.zhangxinxu.com/wordpress/2019/08/css-flex-last-align/

  4. Educational Codeforces Round 137 (Rated for Div. 2) - D. Problem with Random Tests

    期望 + 暴力 [Problem - D - Codeforces](https://codeforces.com/contest/1743/problem/E) 题意 给出一个长度为 \(n\;(1 ...

  5. python 获取近几周日期

    import datetimedef get_Next_day(count): today = datetime.datetime.today().date() for i in range(coun ...

  6. 如何将PDF文件中的部分信息隐藏或遮盖呢?

      由于工作需要,总是需要对PDF文件中的内容进行部分隐藏.之前,作为VIP,可以使用某软件对PDF中的信息进行部分遮盖,现在,VIP到期了,我也不想继续花钱了(哭穷,嘻嘻)   在信息时代,只要会百 ...

  7. EF Core级联保存时DbUpdateConcurrencyException报错异常

    出现改报错异常的原因是,EF Core不支持级联更新时添加新的子项!!! 如果主体子项添加一个新内容,EF Core则认为这个内容原本已经存在了(实际是你新增的),只不过并发冲突中被其他进程删除掉了, ...

  8. rocketmq集群配置

    rocketmq 2m-2s-sync部署 1.下载 jdk-8u361-linux-x64.tar.gz rocketmq-all-5.1.0-bin-release.zip #/etc/profi ...

  9. mysql 当年所有月份列表

    -- 不依赖任何表,只是用mysql自带函数方法select concat((select year(now())),'-01') as `date`union select concat((sele ...

  10. vue3 reactive值不更新

    即上一个随笔里面的form表单数据定义的问题之后,又出现了另一个问题. 页面里面有一个数组: let ruleForm = reactive([     { name:'123456' } ]) 我要 ...