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 操作的更多相关文章

  1. 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 ...

  2. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  3. 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 ...

  4. 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, ...

  5. 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 ...

  6. 平衡二叉树(AVL Tree)

    在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...

  7. 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 ...

  8. 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 ...

  9. 转载:平衡二叉树(AVL Tree)

    平衡二叉树(AVL Tree) 转载至:https://www.cnblogs.com/jielongAI/p/9565776.html 在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简 ...

随机推荐

  1. JAVA之旅(十一)——RuntimeException,异常的总结,Package,jar包,多线程概述

    JAVA之旅(十一)--RuntimeException,异常的总结,Package,jar包,多程序概述 继续JAVA之旅 一.RuntimeException 在Exception种有一个特殊的子 ...

  2. 【一天一道LeetCode】#49. Group Anagrams

    一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...

  3. markdown简易快速的编辑格式(易读易写)

    实现简单快速书写,格式指定简便.易读易写 讲解http://wowubuntu.com/markdown/ 简单使用的讲解:http://www.ituring.com.cn/article/23 代 ...

  4. [转].NET程序破解仅需三步

    近期开发公司商城,为了简化开发用了V5Shop网店程序.本来预计一个月完工,哪知道出现一堆问题大大增加了我的工作量(早知道还不如全部自己写了). 破V5Shop真不地道,说是免费的,结果程序一大堆问题 ...

  5. Leetcode_49_Anagrams

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42744709 Given an array of stri ...

  6. STL - 各个容器的使用时机

    deque的使用场景:比如排队购票系统,对排队者的存储可以采用deque,支持头端的快速移除,尾端的快速添加.如果采用vector,则头端移除时,会移动大量的数据,速度慢. vector与deque的 ...

  7. unbutu 安装java教程

    这两个讲的很好: http://www.linuxidc.com/Linux/2012-11/75001.htm http://www.cnblogs.com/fnng/archive/2013/01 ...

  8. Android NFC开发(一)——初探NFC,了解当前前沿技术

    Android NFC开发(一)--初探NFC,了解当前前沿技术 官方文档:http://developer.android.com/guide/topics/connectivity/nfc/ind ...

  9. Struts源码之ValueStack

    /** * ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated ag ...

  10. 【Android 应用开发】BluetoothServerSocket详解

    一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...