平衡二叉树之AVL树
AVL树(命名来源于作者姓名,Adelson-Velskii和Landis),即平衡二叉树,满足以下的条件:
1)它的左子树和右子树都是AVL树
2)左子树和右子树的高度差不能超过1
从条件1可能看出是个递归定义。
AVL树中任何节点的两个儿子子树的高度最大差别为一,所以它也被称为高度平衡树。
AVL树插入节点的步骤,分为2类:
第1类:外侧插入,单旋转

第2类:内侧插入,双旋转(先旋转成外侧插入的情况,再单旋转)

由于调整以后,树高与插入前是相同的,所以无需再向上查看balance情况
代码实现:http://blog.chinaunix.net/uid-20662820-id-142440.html
struct node
{
node* parent;
node* left;
node* right;
int balance; //左右子树高度之差
int key;
}; int searchNode(int key, node* root, node** parent) //如果没找到,parent也是指向要插入位置的父位置
{
node* temp;
assert(root != NULL);
temp = root;
*parent = root->parent;
while (temp !=NULL)
{
if (temp->key == key)
return ;
else
{
*parent = temp;
if (temp->key > key)
temp = temp->left;
else
temp = temp->right;
}
}
return ;
} node* adjustAVL(node* root, node* parent, node* child)
{
node *cur;
assert((parent != NULL)&&(child != NULL));
switch (parent->balance)
{
case :
if (child->balance == -)//LR型(内侧插入):插入的节点的父节点直接升级做parent
{
cur = child->right;
cur->parent = parent->parent;
child->right = cur->left;
if (cur->left != NULL)
cur->left->parent = child;
parent->left = cur->right;
if (cur->right != NULL)
cur->right->parent = parent;
cur->left = child;
child->parent = cur;
cur->right = parent;
if (parent->parent != NULL)
if (parent->parent->left == parent)
parent->parent->left = cur;
else parent->parent->right = cur;
else
root = cur;
parent->parent = cur;
if (cur->balance == )
{
parent->balance = ;
child->balance = ;
}
else if (cur->balance == -)
{
parent->balance = ;
child->balance = ;
}
else
{
parent->balance = -;
child->balance = ;
}
cur->balance = ;
}
else //LL型(外侧插入):插入的节点的父节点升级做child,child升级做parent
child->parent = parent->parent;
parent->left = child->right;
if (child->right != NULL)
child->right->parent = parent;
child->right = parent;
if (parent->parent != NULL)
if (parent->parent->left == parent)
parent->parent->left = child;
else parent->parent->right = child;
else
root = child;
parent->parent = child;
if (child->balance == ) //插入时
{
child->balance = ;
parent->balance = ;
}
else //删除时
{
child->balance = -;
parent->balance = ;
}
}
break; case -:
if (child->balance == ) //RL型
{
cur = child->left;
cur->parent = parent->parent;
child->left = cur->right;
if (cur->right != NULL)
cur->right->parent = child;
parent->right = cur->left;
if (cur->left != NULL)
cur->left->parent = parent;
cur->left = parent;
cur->right = child;
child->parent = cur;
if (parent->parent != NULL)
if (parent->parent->left == parent)
parent->parent->left = cur;
else parent->parent->right = cur;
else
root = cur;
parent->parent = cur;
if (cur->balance == )
{
parent->balance = ;
child->balance = ;
}
else if (cur->balance == )
{
parent->balance = ;
child->balance = -;
}
else
{
parent->balance = ;
child->balance = ;
}
cur->balance = ;
}
else //RR型
{
child->parent = parent->parent;
parent->right = child->left;
if (child->left != NULL)
child->left->parent = parent;
child->left = parent;
if (parent->parent != NULL)
if (parent->parent->left == parent)
parent->parent->left = child;
else parent->parent->right = child;
else
root = child;
parent->parent = child;
if (child->balance == -) //插入时
{
child->balance = ;
parent->balance = ;
}
else //删除时
{
child->balance = ;
parent->balance = -;
}
}
break;
}
return root;
} node* insertNode(int key, node* root)
{
node *parent, *cur, *child;
assert (root != NULL);
if (searchNode(key, root, &parent)) //结点已存在
return root;
else
{
cur = (node*)malloc(sizeof(node));
cur->parent = parent;
cur->key = key;
cur->left = NULL;
cur->right = NULL;
cur->balance = ;
if (keykey)
{
parent->left = cur;
child = parent->left;
}
else
{
parent->right = cur;
child = parent->right;
} while ((parent != NULL)) //查找需要调整的最小子树
{
if (child == parent->left)
if (parent->balance == -)
{
parent->balance = ;
return root;
}
else if (parent->balance == )
{
parent->balance = ;
break;
}
else
{
parent->balance = ;
child = parent;
parent = parent->parent;
}
else if (parent->balance == ) //是右孩子,不会引起不平衡
{
parent->balance = ;
return root;
}
else if (parent->balance == -) //是右孩子,并且引起parent的不平衡
{
parent->balance = -;
break;
}
else //是右孩子,并且可能引起parent的parent的不平衡
{
parent->balance = -;
child = parent;
parent = parent->parent;
}
} if (parent == NULL)
return root;
return adjustAVL(root, parent, child);
}
}
平衡二叉树之AVL树的更多相关文章
- 【Java】 大话数据结构(12) 查找算法(3) (平衡二叉树(AVL树))
本文根据<大话数据结构>一书及网络资料,实现了Java版的平衡二叉树(AVL树). 平衡二叉树介绍 在上篇博客中所实现的二叉排序树(二叉搜索树),其查找性能取决于二叉排序树的形状,当二叉排 ...
- Java数据结构(十四)—— 平衡二叉树(AVL树)
平衡二叉树(AVL树) 二叉排序树问题分析 左子树全部为空,从形式上看更像一个单链表 插入速度没有影响 查询速度明显降低 解决方案:平衡二叉树 基本介绍 平衡二叉树也叫二叉搜索树,保证查询效率较高 它 ...
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- 数据结构之平衡二叉树(AVL树)
平衡二叉树(AVL树)定义如下:平衡二叉树或者是一棵空树,或者是具有以下性质的二叉排序树: (1)它的左子树和右子树的高度之差绝对值不超过1: (2)它的左子树和右子树都是平衡二叉树. AVL树避免了 ...
- 二叉树学习笔记之经典平衡二叉树(AVL树)
二叉查找树(BSTree)中进行查找.插入和删除操作的时间复杂度都是O(h),其中h为树的高度.BST的高度直接影响到操作实现的性能,最坏情况下,二叉查找树会退化成一个单链表,比如插入的节点序列本身就 ...
- 一步一步写平衡二叉树(AVL树)
平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...
- 平衡二叉树(AVL树)
参考资料 http://www.cnblogs.com/Cmpl/archive/2011/06/05/2073217.html http://www.cnblogs.com/yc_sunniwell ...
- 算法与数据结构(十一) 平衡二叉树(AVL树)(Swift版)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- 经典平衡二叉树(AVL树)
二叉查找树(BSTree)中进行查找.插入和删除操作的时间复杂度都是O(h),其中h为树的高度.BST的高度直接影响到操作实现的性能,最坏情况下,二叉查找树会退化成一个单链表,比如插入的节点序列本身就 ...
随机推荐
- 通过openresty && tengine && nginx 动态添加资源到 html 页面
1. 目标 不用修改后端代码,但是还需要进行js .css 等资源文件的加载 主要的场景是进行统计分析 2.技术实现 通过服务器扩展进行动态添加 iis 可以添加模块,nginx 可以使用 sub_ ...
- smarty缓存的使用
<?php require './smarty/Smarty.class.php'; $sm = new Smarty; //$sm->force_compile = true; $sm- ...
- Oracle 之 配置HugePages内存
HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...
- 13.Python接口自动化测试 -- 豆瓣
1.代码如下所示: import requests,unittest import json import HTMLTestRunner class TestDouBan(unittest.TestC ...
- RK3288 usb 摄像头旋转
系统:Android 5.1 下面实现了摄像头 180 度旋转,旋转角度只需修改 orientation. diff --git a/hardware/rockchip/camera/CameraHa ...
- Linux GNU C
Linux 系统上可用的C编译器是GNU C编译器,它建立在自由软件基金会的编程许可证的基础上,因此可以自由发布.GNU C对标准C 进行一系列扩展,以增强标准C的功能. 1.零长度数组GNU C 允 ...
- 面向对象(PHP学习)
在对超大型项目的开发过程中,如果使用面向过程地开发,代码量是非常的庞大,这将大量的用到判断和循环嵌套, 和很多很相似的代码,不仅使项目代码量更加的庞大,还不利于开发,重用及维护. 面向对象就能很好的解 ...
- Ubantu 安装fftw3
FFTW官网 FFTW ( the Faster Fourier Transform in the West) 是一个快速计算离散傅里叶变换的标准C语言程序集. ubuntu下安装指令: sudo a ...
- TCP/IP网络编程系列之四(初级)
TCP/IP网络编程系列之四-基于TCP的服务端/客户端 理解TCP和UDP 根据数据传输方式的不同,基于网络协议的套接字一般分为TCP和UDP套接字.因为TCP套接字是面向连接的,因此又称为基于流的 ...
- [Java][Web]解决 Request 的乱码
解决 get 提交的乱码 (手工处理) String username = request.getParameter("username"); username = new Str ...