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. JavaWeb中的Servlet

    Servlet 目录 Servlet 一.互联网中的资源 二.Servlet 2.1.Servlet的作用 2.2.Servlet执行流程 2.3.Servlet生命周期 2.4.Servlet的继承 ...

  2. .Net简单使用了一下Redis

    书接上回!!! 创建控制台应用 管理NuGet程序包 下载ServiceStack.Redis 最后写代码 1 class Program 2 { 3 static RedisClient redis ...

  3. C# Post调用接口并传递json参数

    1 public string Post(string Url, string jsonParas) 2 { 3 string strURL = Url; 4 //创建一个HTTP请求 5 HttpW ...

  4. 【Direct3D 12】配置编译环境

    创建桌面应用程序 使用Visual Studio Community 2019创建一个桌面应用程序. 配置SDK版本.头文件.依赖库 右键单击创建的项目名称,选择Properties. 在Config ...

  5. 下载nodejs和vue

    下载nodejs https://nodejs.org/en 下载或更新npm npm install cnpm -g npm install -g vue 全局安装 创建一个基于 "web ...

  6. Vue2 常见问题汇总及解决方案

    参考:https://mp.weixin.qq.com/s/6Wapb1bZLQaYv0zlip6ygg 1.安装超时(install timeout) 方案: cnpm : 国内对npm的镜像版本 ...

  7. redisTemplate实现分布式锁(释放锁用lua脚本)

    package com.xxx.platform.util; import org.springframework.beans.factory.annotation.Autowired; import ...

  8. go-fastdfs断点续传功能

    1)安装go-fastdfs: 可以从GitHub上获取go-fastdfs的源码,然后使用go get命令安装: go get github.com/sjqzhang/go-fastdfs 2)安装 ...

  9. 安装 Harbor

    安装Harbor先决条件 https://goharbor.io/docs/2.6.0/install-config/installation-prereqs/ 1.安装docker 参考docker ...

  10. 如何使用命令行直接运行PHP脚本程序

    在我的博客里有一些文章是和解码或者处理文件有关,其中有些自动化工作比较简单,使用了一些PHP程序来编写处理,这样写起程序来也比较快,因为是纯过程的任务,不想用C#来处理. 写完PHP的脚本程序后,保存 ...