AVL树的插入操作(旋转)图解
AVL树的概念


AVL树的插入
template<class K>
struct AVLTreeNode
{
K _key;
int _bf;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent; AVLTreeNode(const K& key, const V& value)
:_key(key),
_bf(),
_left(NULL),
_right(NULL),
_parent(NULL)
{}
};












程序代码:
bool Insert(const K& key, const V& value)
{
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}
Node* pcur = _root;
Node* parent = NULL;
while (pcur)
{
if (pcur->_key == key)
return false;
else if (pcur->_key < key)
{
parent = pcur;
pcur = pcur->_right;
}
else
{
parent = pcur;
pcur = pcur->_left;
}
}
if (parent->_key < key)
{
pcur = parent->_right = new Node(key, value);
pcur->_parent = parent;
}
else
{
pcur = parent->_left = new Node(key, value);
pcur->_parent = parent;
} while (parent)
{
//修正平衡因子
if (pcur == parent->_left)
{
parent->_bf--;
}
if (pcur == parent->_right)
{
parent->_bf++;
}
//
if (parent->_bf == )
break;
else if (parent->_bf == - || parent->_bf == )
{
pcur = parent;
parent = pcur->_parent;
}
else //parent->bf -2 || 2
{ if (parent->_bf == -)
{
if (pcur->_bf == -) //右单旋
RotateR(parent);
else //左右双旋
RotateLR(parent);
}
else
{
if (pcur->_bf == ) //左单旋
RotateL(parent);
else //右左双旋
RotateRL(parent);
}
break;
}
}
return true;
}
>>旋转
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
//换指向
parent->_left = subLR;
subL->_right = parent; if (subLR)
{
subLR->_parent = parent;
} Node* PParent = parent->_parent; //判断parent是否有父节点
if (PParent)
{
if (parent == PParent->_left)
{
PParent->_left = subL;
subL->_parent = PParent;
}
else
{
PParent->_right = subL;
subL->_parent = PParent;
}
}
else
{
_root = subL;
subL->_parent = NULL;
}
parent->_parent = subL;
//修改bf
subL->_bf = ;
parent->_bf = ;
} //
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
//调整指向
subR->_left=parent;
parent->_right = subRL; if (subRL) //如果subRL非空
{
subRL->_parent = parent;
} Node* PPNode = parent->_parent;
if (PPNode)
{
if (PPNode->_left == parent)
PPNode->_left = subR;
else
PPNode->_right = subR; //subR的父节点改变
subR->_parent = PPNode;
}
else
{
_root = subR; //根节点
subR->_parent = NULL;
}
parent->_parent = subR;
/*修改bf*/
parent->_bf = subR->_bf = ;
} //双旋(左右、、右左)
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf; RotateR(parent->_right);
RotateL(parent); //调整subR和parent的平衡因子
if (bf == -)
subR->_bf = ; // subR的bf在左旋中已经置0了,这里就没有再写
else if (bf == )
parent->_bf = -; else
{
parent->_bf = ;
subRL->_bf = ;
}
} void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent); //调整parent和subL的平衡因子
if (bf == -)
parent->_bf = ; //subL的bf在左旋中已经置0了,这里就没有再写
else if (bf == )
subL->_bf = -; //parent的bf在左旋中已经置0了
else
{
subL->_bf = ;
parent = ;
}
}
AVL树的插入操作(旋转)图解的更多相关文章
- AVL树的插入和删除
一.AVL 树 在计算机科学中,AVL树是最早被发明的自平衡二叉查找树.在AVL树中,任一节点对应的两棵子树的最大高度差为 1,因此它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度 ...
- AVL 树的插入、删除、旋转归纳
参考链接: http://blog.csdn.net/gabriel1026/article/details/6311339 1126号注:先前有一个概念搞混了: 节点的深度 Depth 是指从根 ...
- AVL树的单双旋转操作
把必须重新平衡的节点称为å.对于二叉树,å的两棵子树的高度最多相差2,这种不平衡可能有四种情况: 对å的左儿子的左子树进行插入节点(左-左) 对å的左儿子的右子树进行插入节点(左-右) 对å的右儿子的 ...
- AVL树的插入删除查找算法实现和分析-1
至于什么是AVL树和AVL树的一些概念问题在这里就不多说了,下面是我写的代码,里面的注释非常详细地说明了实现的思想和方法. 因为在操作时真正需要的是子树高度的差,所以这里采用-1,0,1来表示左子树和 ...
- AVL树的插入与删除
AVL 树要在插入和删除结点后保持平衡,旋转操作必不可少.关键是理解什么时候应该左旋.右旋和双旋.在Youtube上看到一位老师的视频对这个概念讲解得非常清楚,再结合算法书和网络的博文,记录如下. 1 ...
- 第七章 二叉搜索树 (d2)AVL树:插入
- 创建AVL树,插入,删除,输出Kth Min
https://github.com/TouwaErioH/subjects/tree/master/C%2B%2B/PA2 没有考虑重复键,可以在结构体内加一个int times. 没有考虑删除不存 ...
- 数据结构--Avl树的创建,插入的递归版本和非递归版本,删除等操作
AVL树本质上还是一棵二叉搜索树,它的特点是: 1.本身首先是一棵二叉搜索树. 2.带有平衡条件:每个结点的左右子树的高度之差的绝对值最多为1(空树的高度为-1). 也就是说,AVL树,本质上 ...
- AVL树(查找、插入、删除)——C语言
AVL树 平衡二叉查找树(Self-balancing binary search tree)又被称为AVL树(AVL树是根据它的发明者G. M. Adelson-Velskii和E. M. Land ...
随机推荐
- Using FastCGI to Host PHP Applications on IIS 7 -IIS7 怎么配置 PHP5
This article describes how to configure the FastCGI module and PHP to host PHP applications on IIS 7 ...
- JavaFX 2 Dialogs
http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...
- Java IO (3) - Reader
Java IO (3) - Reader 前言 JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包.Java 流在处理上分为字符流 ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- 第三方类AFNetworking
1.AFNetworking简介 AFNetworking是一个在iOS开发中使用非常多的轻量级网络开源库适用于iOS以及Mac OS X.它构建于在(Apple iOS开发文档) NSURLConn ...
- UVa 10801 Lift Hopping / floyd
乘电梯 求到目标层的最短时间 有n个电梯 换一个电梯乘需要额外60秒 所以建图时每个电梯自己能到的层数先把时间算好 这是不需要60秒的 然后做floyd时 如果松弛 肯定是要换电梯 所以要加60秒 # ...
- Erp:原料投入产出报表
USE [ChangHongWMS612]GO /****** Object: StoredProcedure [dbo].[st_WMS_RptMaterialInOutDaily] Script ...
- Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题
前一段时间,在瞎看看 Node.js,便研究通过 Node.js 实现知乎模拟登陆.相信,有很多网站有登陆权限设置,如若用户未登陆,将会跳转至首页提醒用户登陆,无法浏览部分页面. 如若是 b/s 架构 ...
- 函数定义从零开始学C++之从C到C++(一):const与#define、结构体对齐、函数重载name mangling、new/delete 等
今天一直在学习函数定义之类的问题,下午正好有机会和大家共享一下. 一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC+ ...
- C# 添加类库依赖