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 ...
随机推荐
- HDU 5660 jrMz and angles (暴力枚举)
jrMz and angles 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/E Description jrMz has tw ...
- c++出错记录
错误1如下: vector<vector<int>> m_vc; error: '&' cannot appear in a constant-expression ...
- Debug with jdb
原文地址: http://www.javaworld.com/article/2077445/testing-debugging/debug-with-jdb.html Q: How do you u ...
- cocos2d-x Mask的实现及优化
转自:http://blog.ch-wind.com/cocos2d-x%E4%B8%ADmask%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8F%8A%E4%BC%98%E5%8C ...
- Codeforces Gym 100203E E - bits-Equalizer 贪心
E - bits-EqualizerTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- [Angular 2] How To Debug An Angular 2 Application - Debugging via Augury or the Console
In this lesson we will learn several ways to debug an Angular 2 application, including by using Augu ...
- [MODX] 2. Chunks $
Chunk in Modx can cut your template into samll pieces to make code reuseable. [[$chunk_name]] For ex ...
- 在线服务之socket编程科普
简介 本篇文章是介绍一个典型的在线C++服务的最底层socket管理是如何实现的. 文章会从一个最简单的利用socket编程基础API的一个小程序开始,逐步引入现在典型的select,epoll机制, ...
- Tomcat以指定JDK运行
如果一台机器上有多个Tomcat,可能存在不同的Tomcat需要不同版本JDK才能运行的情况,这时候就需要指定JDK来同时运行多个Tomcat了. 在windows环境下以批处理文件方式启动tomca ...
- auto_ptr解析
auto_ptr是当前C++标准库中提供的一种智能指针,或许相对于boost库提供的一系列眼花缭乱的智能指针, 或许相对于Loki中那个无所不包的智能指针,这个不怎么智能的智能指针难免会黯然失色.诚然 ...