//测试数据
//第一组:7个输入,测试LL型,40,36,44,32,38,28,24;
//第二组:7个输入,测试RR型,40,36,44,43,48,52,56;
//第三组:7个输入,测试LR型,40,36,44,32,37,38,39;
//第四组:7个输入,测试RL型,40,36,44,48,43,42,41;

#include<iostream>
#define Element int
using namespace std;

typedef struct AVLNode
{
Element data;
int height;
AVLNode* LeftChild,*RightChild;
AVLNode():data(0),height(0),LeftChild(NULL),RightChild(NULL){
}
AVLNode(Element e,int heig,AVLNode* L,AVLNode* R):data(e),height(heig),LeftChild(L),RightChild(R){
}
} AVLNode;

int MaxHeight(int a,int b)
{
return a>b?a:b;
}

int GetHeight(AVLNode* &Node)
{
if(Node == NULL) return -1;
else return Node->height = MaxHeight(GetHeight(Node->LeftChild),GetHeight(Node->RightChild)) + 1;
}

void InorderWalk(AVLNode* Node)
{
if(Node)
{
InorderWalk(Node->LeftChild);
cout<<Node->data<<"("<<Node->height<<") ";
InorderWalk(Node->RightChild);
}
}

AVLNode* Serach(AVLNode* root,Element key)
{
AVLNode* p = root;
while(p && p->data != key)
{
if(key < p->data)
p = p->LeftChild;
else
p = p->RightChild;
}
return p;
}

void LLRotation(AVLNode* &Node)
{
cout<<"LLRotation."<<endl;
AVLNode* p = Node->LeftChild;
Node->LeftChild = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = Node->RightChild;
Node->RightChild = p;
Element temp = p->data; p->data = Node->data; Node->data = temp;
}

void RRRotation(AVLNode* &Node)
{
cout<<"RRRotation."<<endl;
AVLNode* p = Node->RightChild;
Node->RightChild = p->RightChild;
p->RightChild = p->LeftChild;
p->LeftChild = Node->LeftChild;
Node->LeftChild = p;
Element temp = p->data; p->data = Node->data; Node->data = temp;
}

void LRRotation(AVLNode* &Node)
{
cout<<"LRRotation(先RR后LL)."<<endl;
RRRotation(Node->LeftChild);
LLRotation(Node);
}

void RLRotation(AVLNode* &node)
{
cout<<"RLRotation(先LL后RR)."<<endl;
LLRotation(node->RightChild);
RRRotation(node);
}

void Insert(AVLNode* &root,Element e)
{
AVLNode* p = root;
if(p == NULL)
{
AVLNode* pt = new AVLNode(e,0,NULL,NULL);
root = pt;
}
else
{
if(e <= p->data)
{
Insert(p->LeftChild,e);
if(GetHeight(p->LeftChild) - GetHeight(p->RightChild) == 2)
{
if(e < p->LeftChild->data)
{
LLRotation(p);
}
else
{
LRRotation(p);
}
}
p->height = MaxHeight(GetHeight(p->LeftChild),GetHeight(p->RightChild)) + 1;
}
else if(e > p->data)
{
Insert(p->RightChild,e);
if(GetHeight(p->RightChild) - GetHeight(p->LeftChild) == 2)
{
if(e < p->LeftChild->data)
{
RLRotation(p);
}
else
{
RRRotation(p);
}
}
p->height = MaxHeight(GetHeight(p->LeftChild),GetHeight(p->RightChild)) + 1;
}
}
}

void Delete(AVLNode* &root,Element key)
{
if(!root)
{
cout<<"No Record!"<<endl;
}
else
{
if(key == root->data)
{
if ( NULL==root->LeftChild && NULL==root->RightChild ) //叶节点
{

delete root;
root = NULL;
}

else if(root->LeftChild != NULL && root->RightChild == NULL)
{
AVLNode* p = root->LeftChild;
root->data = p->data;
root->LeftChild = p->LeftChild;
root->RightChild = p->RightChild;
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
delete root;
root = NULL;
}
else if(root->LeftChild = NULL && root->RightChild != NULL)
{
AVLNode* p = root->RightChild;
root->data = p->data;
root->LeftChild = p->LeftChild;
root->RightChild = p->RightChild;
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
delete root;
root = NULL;
}
else //左右两个孩子
{
AVLNode* p = root->RightChild;
if(p->LeftChild == NULL) //右孩子没有左孩子
{
root->data = p->data;
root->RightChild = p->RightChild;
delete p;
p = NULL;
if(GetHeight(p->RightChild) - GetHeight(p->LeftChild) == 2)
{
if(GetHeight(root->LeftChild->LeftChild) >= GetHeight(p->LeftChild->RightChild))
{
LLRotation(root) ;
}
else
{
LRRotation(root);
}
}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
}
else //右孩子有左孩子
{
while(p->LeftChild->LeftChild != NULL)
{
p = p->LeftChild;
}
AVLNode* pt = p->LeftChild;
root->data = pt->data;
p->LeftChild = pt->RightChild;
delete pt; pt = NULL;
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)>=2 ) // 调整结点,保持平衡。
{
if( GetHeight( root->LeftChild->LeftChild ) >= GetHeight(root->LeftChild->RightChild))
{
LLRotation(root);
}
else
{
LRRotation(root);
}

}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息

}
}
}

else if( key<root->data ) //在左子树删除数据
{
Delete(root->LeftChild,key);
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)<=-2 ) // 调整结点,保持平衡。
{
if( GetHeight( root->RightChild->LeftChild ) >= GetHeight( root->RightChild->RightChild) )
{
RLRotation(root);
}
else
{
RRRotation(root);
}

}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
}

else //在右子树删除数据
{
Delete(root->RightChild,key);
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)>=2) // 调整结点,保持平衡。
{
if( GetHeight( root->LeftChild->LeftChild ) >= GetHeight(root->LeftChild->RightChild) )
{
LLRotation(root);
}
else
{
LRRotation(root);
}
}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild)); // 重新更新该节点一下的所有结点高度信息
}

}
}

int main()
{
AVLNode *root=NULL;
int num;
Element e;
///////////创建平衡二叉树
cout<<"创建平衡二叉树,请输入节点数:"<<endl;
cin>>num;
cout<<"输入"<<num<<"个节点的数值,以空格或者回车符分开:"<<endl;
while(num--)
{

cin>>e;
Insert(root,e);
cout<<"此时根节点数据为:"<<root->data<<"("<<root->height<<")"<<endl<<"中序遍历结果为:"<<endl;
InorderWalk(root); cout<<endl<<endl;

}
cout<<"创建平衡二叉树完毕,中序遍历输出为:"<<endl;
InorderWalk(root); cout<<endl<<endl;
//////////插入数据
cout<<"请输入要插入的数"<<endl;
cin>>e;
Insert(root,e);
cout<<"此时根节点数据为:"<<root->data<<"("<<root->height<<")"<<endl;
cout<<"插入新元素后的平衡二叉树,中序遍历输出为:"<<endl;
InorderWalk(root);cout<<endl<<endl;
///////////删除结点
cout<<"输入你选择删除的数"<<endl;
cin>>e;
Delete(root,e);
cout<<"删除操作后的二叉搜索树"<<endl;
InorderWalk(root); cout<<endl<<endl;
///////////删除结点
cout<<"输入你选择删除的数"<<endl;
cin>>e;
Delete(root,e);
cout<<"删除操作后的二叉搜索树"<<endl;
InorderWalk(root); cout<<endl<<endl;
//////////查找结点
cout<<endl<<"输入要查找的数,系统找到则输出:"<<endl;
cin>>e;
AVLNode *p=Serach(root,e);
if (p) cout<<p->data<<endl;
else cout<<"没有找到该数"<<endl;
return 0;

}

AVLTree 平衡树的更多相关文章

  1. C#平衡树(AVLTree)

    参考:http://www.cnblogs.com/skywang12345/p/3577479.html using System; using System.Collections.Generic ...

  2. 平衡树(AVL)详解

    1. 为什么平衡树? 在二叉搜索树(BST,Binary Search Tree)中提到,BST树可能会退化成一个链表(整棵树中只有左子树,或者只有右子树),这将大大影响二叉树的性能. 前苏联科学家G ...

  3. 二叉排序树的创建删除中序输出&&平衡树

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  4. 实现Avl平衡树

    实现Avl平衡树   一.介绍 AVL树是一种自平衡的二叉搜索树,它由Adelson-Velskii和 Landis于1962年发表在论文<An algorithm for the organi ...

  5. 二叉树,平衡树,红黑树,B~/B+树汇总

    二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...

  6. 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

    平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...

  7. java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)

    package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...

  8. Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):   这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有.  后面两月又要忙了. ...

  9. 平衡树以及AVL树

    平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...

随机推荐

  1. Rails中的缓存

    最近学习Rails. 看到如下代码: <% if notice %> <p id="notice"><%= notice %></p> ...

  2. footer绝对定位但是不在页面最下边解决方案

    方案一 html { height: 100%; } body { position: relative; min-height: 100%; box-sizing: border-box; padd ...

  3. .net比较完美的动态注册com组件

    .net中经常需要使用com组件,怎么样注册com组件呢? 一般想到的当然是直接通过系统cmd 调用regsvr32注册程序去注册,如下: regsvr32 name.dll 在.net中可以直接执行 ...

  4. google play下载apk

    http://apps.evozi.com/apk-downloader/ 输入apk下载地址

  5. 泛型数组列表 ArrayList

    为什么使用泛型数组列表而不使用普通数组? 1.普通数组经常会发生容量太大以致浪费的情况 2.普通数组无法动态更改数组 基本概念: 1.采用[类型参数]的[类]---->[泛型类] 2.[泛型类型 ...

  6. java利用Scanner获取键盘输入

    首发地址:我的网易博客 在运行一个java程序的时候,可能我们需要在运行的时候传递一些参数进去...咋办呢... java提供了一个Scanner类,利用这个类,我们可以很方便的获取键盘输入的参数.. ...

  7. 解决jquery操作checkbox全选全不选无法勾选问题

    最近在学习中使用jquery操作checkbox,使用下面方法进行全选.反选:$("input[name='checkbox']").attr("checked" ...

  8. could not read symbols: File format not recognized

    arm-linux-gnueabi-readelf工具解决问题 编译一个32位平台的内核时,出现如下错误提示: libschw.a: could not read symbols: File form ...

  9. oracle 的分析函数

    row_number() over(partition by ... order by ...) rank() over(partition by ... order by ...) dense_ra ...

  10. el: 在jsp页面内使用函数判断子字符串

    e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn: ...