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-23 合并K个升序链表

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/merge-k-sorted-lists 题目描述 给你一个链表数组,每个链表都已经按升序排列. ...

  2. LeetCode-838 推多米诺

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/push-dominoes 题目描述 n 张多米诺骨牌排成一行,将每张多米诺骨牌垂直竖立.在开始时 ...

  3. fiddler的界面详细讲解

    一.fiddler首页概述

  4. Codeforces 1492D、Genius's Gambit

    原题网址 https://codeforces.com/contest/1492/problem/D 题目大意 给定a,b,k,求x,y使得x和y的二进制表示都恰有a个0和b个1,且不能使用开头的0. ...

  5. java正则解析ip

    public class test { public static void main(String[] args) { // TODO Auto-generated method stub Stri ...

  6. Mongo 常用命令

    1.登入 docker exec -it  mongo mongo  数据库 db.auth("账号","密码") docker exec -it  mongo ...

  7. 2022-05-26内部群每日三题-清辉PMP

    1.在执行关键路径上的一项活动时,职能主管将涉及这个活动的两个项目资源调去支持解决某个应急情况,项目经理应该怎么做? A.实施应急计划 B.快速跟进关键路径 C.与职能经理协商分配替代资源 D.将该问 ...

  8. 函数:3ds max 给选择对象设置轴心点

    ------轴心点函数大全------函数名称中的字母含义:------w:西 e:东 n:北 s:南 b:底 c:中心 t:顶 m:间 如:wnb表示西北下 smt表示南中上 fn pivot_wn ...

  9. window.location.href 用法总结

    想做好前端,这几种用法一定要了解: 在做网页前端的时候会经常用到JavaScript其中window.location.href用得非常多: 其中最常用的有windows.location.href= ...

  10. ts面试题

    1.ts的内置数据类型2.ts中any和unknown3.如何将unknown指定为更具体的类型4.说说对ts中命名空间与模块的理解?区别?5.对ts的理解,和js的区别6.tsconfig.json ...