AVL Tree 操作
1.AVL树是带有平衡条件的二叉查找树, 一棵AVL树是其每个节点的左子树和右子树的高度最多差1的二叉查找树。
2.AVL树的删除要比插入复杂。如果删除相对较少,那么用懒惰删除的方法是最好的策略。
3.AVL树的插入操作:
#ifndef _AvlTree_H
struct AvlNode;
typedef struct AvlNode *Position;
typedef struct AvlNode *AvlTree;
typedef ElementType int;
AvlTree Insert(ElmentType X,AvlTree T);
AvlTree MakeEmpty(AvlTree T);
Position Find(ElementType X,AvlTree T);
Position FindMax(AvlTree T);
Position FindMin(AvlTree T);
AvlTree Delete(ElementType X,AvlTree T);
ElementType Retrieve(Position P);
#endif;
struct AvlNode //定义一个树结构,包含了树的高度
{
ElementType Element;
AvlTree Left;
AvlTree Right;
int Height;
};
int Height(Position P) //取得树的高
{
if(P==NULL)
return -;
else
return P->Height; }
int Max(AvlTree T1, AvlTree T2) //获得大值
{
if(T1->Height>T2->Height)
return T1->Height;
else
return T2->Height;
} AvlTree Insert(ElmentType x,AvlTree T) // 树的插入
{
if(T==NULL)
{
T=new AvlTree();
T->Element=x;
T->Left=NULL;
T->Right=NULL;
T->Height=;
}
else if(x<T->Element) //插入到当前树的左子树中
{
T->Left=Insert(x,T->Left);
if(Height(T->Left)-Height(T->Right)==)//如果树不平衡
{
if(x<T->Left->Element) //左左插入只需要进行单旋转
T=SingleRotateWithLeft(T);
else //左右插入需要进行双旋转,单旋转不能改变去不平衡的状态
T=DoubleRotateWithLeft(T);
}
}
else if(x>T->Element)//插入到当前树的右子树中
{
T->Right=Insert(x,T->Right);
if(Height(T->Right)-Height(T->Left)==)
{
if(x>T->Right->Element) //右右插入只需要进行单旋转
T=SingleRotateWithRight(T);
else //右左插入需要进行双旋转,单旋转不能改变去不平衡的状态
T=DoubleRotateWithRight(T);
}
}
else{cout<<"error:不能插入相同的树结点"};
T->Height=Max(Height(T->Left),Height(T->Right))+;
return T;
} Position singleRotateWithLeft(Position K2)//左左单旋
{
Position K1;
K1=K2->Left;
K2->Left=K1->Right;
K1->Right=K2;
K2->Height=Max(Height(K2->Left),Height(K2->Right))+;
K1->Height=Max(Height(K1->Left),Height(K1->Right))+;
return K1;
}
Position singleRotateWithRight(Position K2)//右右单旋
{
Position K1;
K1=K2->Right;
K2->Right=K1->Left;
K1->Left=K2;
K2->Height=Max(Height(K2->Left),Height(K2->Right)+;
K1->Height=Max(Height(K1->Left),Height(K1->Right))+;
return K1;
}
Position DoubleRotateWithLeft(Position K3)//左右双旋
{
Position K1,K2;
K2=K3->Left;
K1=K2->Right;
k2->Right=K1->Left;
K3->Left=K1->Right;
K1->Left=K2;
K1->Right=K3; K1->Height=Max(Height(K1->Left),Height(K1->Right))+;
K2->Height=Max(Height(K2->Left),Height(K2->Right)+;
K3->Height=Max(Height(K3->Left),Height(K3->Right)+; } Position DoubleRotateWithRight(Position K3)//右左双旋
{
//Rotate between k1 and K2;
K3->Right=singleRotateWithLeft(K3->Right);
//Rotate between k2 and k3;
return singleRotateWithRight(K3); }
AVL Tree 操作的更多相关文章
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- 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 ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- 平衡二叉树(AVL Tree)
在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...
- PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- 转载:平衡二叉树(AVL Tree)
平衡二叉树(AVL Tree) 转载至:https://www.cnblogs.com/jielongAI/p/9565776.html 在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简 ...
随机推荐
- FT5X06 如何应用在10寸电容屏(linux-3.5电容屏驱动简析&移植10寸电容屏驱动到Android4.2) (by liukun321咕唧咕唧)
这是几个月以前的东西了,在彻底遗忘之前拿出来好好写写.做个笔记,也算是造福后来人了.在做这个项目之前,没有做过电容屏的驱动,印象中的电容触摸屏是不需要校正的.IC支持多大的屏就要配多大的屏.但是拿到需 ...
- 《java入门第一季》之Date类案例,算一算你的恋爱纪念日
想算你和你对象谈了多久了,还在用笔算吗,是不是很头疼?写个程序算一算吧!会变得如此简单. import java.text.ParseException; import java.text.Simpl ...
- H5 学习之旅-H5表格(7)
表格语法 table:简历表格 captian:表格标题 th:表格行表头 tr:表格行 td:单元格 thead:表格页眉 tfoot:表格页脚 tbody:表格主体 col:列属性 !!!代码实例 ...
- Cocos2D的平台检查宏
为了避免在非iOS平台包含UIKit.h文件,需要在Prefix.pch文件中添加一个条件判断: #if __CC_PLATFORM_IOS #import <UIKit/UIKit.h> ...
- [查阅]Dalvik opcodes
Dalvik opcodes Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. ...
- Leetcode_75_Sort Colors
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43302343 Given an array with n ...
- Java-Iterator总结
纸上得来终觉浅,绝知此事要躬行 --陆游 问渠那得清如许,为有源头活水来 --朱熹 迭 代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构. ...
- Android下VideoView的研究
VideoView继承自SurfaceView,实现了MediaController.MediaPlayerControl的接口.在android系统中的包名为android.widget.Video ...
- 如何取得ChipmunkConstraint实例对象的私有属性
在 如何用代码禁用SpriteBuilder中创建的关节 一篇中提到了要想禁用一个关节就需要将其无效化. 然后我们在重新创建新关节时,可以参考该关节的原始参数. 但是代码中只能直接访问到bodyA和b ...
- centos 下安装mysql-5.6.11
这次是在centos6.4下安装mysql,在安装之前,你要先确定你的linux已经安装了这些包: wget, gcc-c++, ncurses-devel ,cmake, make ,perl 如果 ...