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

最新文章

  1. JQuery 方法简写
  2. php 无限循环
  3. 云主机上搭建squid3代理服务器
  4. MVC 5 的 EF6 Code First 入门
  5. WebService学习整理(一)——客户端三种调用方式整理
  6. 翻译-你必须知道的28个HTML5特征、窍门和技术
  7. Android软件盘InputMethodManager
  8. [ML]机器学习书单
  9. Java面试2018常考题目汇总
  10. 华莱士的 第一个python程序之(用户登录)
  11. java添加菜单项目
  12. listView item分割线不显示
  13. python set和get实现
  14. 关于百度world 编辑器改变上传图片的保存路径图片不显示的问题
  15. js回车、ESC、F2按钮事件
  16. ==和Equals与值类型和引用类型
  17. KEUC首次落地中国,网易云深度剖析Kubernetes优化与实践
  18. Spring Cloud和Dubbo整合开发笔记(1)
  19. JFinal record类型数据在前台获取
  20. CentOS 6 系统启动流程

热门文章

  1. c# 使用 Redis
  2. Java语言输出菱形图型
  3. 结合代码和内存变化图一步步弄懂JVM的FullGC
  4. ChatGPT Java客户端,OpenAi的Java版本SDK已完成,请火速接入。
  5. Vitis-AI之docker指南
  6. Office2021简体中文离线安装包下载地址合集,目前最全
  7. 一个方便IO单元测试的C#扩展库
  8. 04-python垃圾回收机制
  9. 2022-05-20内部群每日三题-清辉PMP
  10. 解决windows环境TIM无法修改个人文件夹位置