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. pg高可用方案repmgr带witness搭建

    一.总体架构 操作系统版本: linux redhat7.6pg版本: 12.2repmgr版本 5.2192.168.3.73 主库: repmgr+master192.168.3.74 从库1: ...

  2. 副三角形行列式转成上(下)三角形行列式为什么依次对换而不用第n行直接对换首行,第n-1行直接对换次行

    副三角形行列式转成上(下)三角形行列式为什么依次对换而不用第n行直接对换首行,第n-1行直接对换次行 前言:重在记录,可能出错. 1. 简而言之,可以用第n行直接对换首行,第n-1行直接对换次行,直到 ...

  3. Cannot invoke "java.sql.Connection.prepareStatement(String)" because "conn" is null问题解决

    HTTP状态 500 - 内部服务器错误 类型 异常报告 消息 Cannot invoke "java.sql.Connection.prepareStatement(String)&quo ...

  4. IntelliJ IDEA运行项目的时候提示 Command line is too long 错误

    这时候你需要调整运行项目的配置,将 Configuration 中的 Shorten Command Line 修改为 JAR 就可以了.

  5. marker的存储组---layerGroup

    1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="U ...

  6. Mysql去重获取最新的一条数据

    Mysql去重获取最新的一条数据 select * from yjzt_kindergartens r where id in (select max(id) from yjzt_kindergart ...

  7. go环境 依赖管理 基本命令

    Go安装 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ Windows 选择Windows版本下载安 ...

  8. git -----已经被跟踪文件如何在本地提交时忽略

    git update-index --assume-unchanged C.md 注:忽略后将不再拉取和提交c.md这个文件 git update-index --no-assume-unchange ...

  9. 对NAN的认识

    NaN是个特殊的数值,它与任何值都不相等,包括它自己,NaN==NaN和NaN===NaN都是false,如果想测试某个数字是否为NaN,可以使用内置的函数isNaN(),如果是NaN则返回true, ...

  10. cenots7 rpm 包升级ssh

    rpm下载地址 也可以自行官网下载 链接: https://pan.baidu.com/s/1S945MehpmZbIriKK6l7Sfw 提取码: y5ua centos7rpm包升级ssh 逻辑思 ...