//测试数据
//第一组: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. 使用reflux进行react组件之间的通信

    前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...

  2. 通过nginx代理之后,获取客户端ip

    1.相关nginx配置(通过header将客户端ip,host等信息传入) location ~ .*.do$ { proxy_set_header X-Real-IP $remote_addr; p ...

  3. Execl 使用技巧

    1. =COUNTIF(C:C;"*OS7*")   某一列中包含OS7的数量总数138

  4. springmvc 解决跨域CORS

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  5. [阅读笔记]Zhang Y. 3D Information Extraction Based on GPU.2010.

    1.立体视觉基础 深度定义为物体间的距离 视差定义为同一点在左图(reference image) 和右图( target image) 中的x坐标差. 根据左图中每个点的视差得到的灰度图称为视差图. ...

  6. js页面跳转(含框架跳转)整理

    js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/java ...

  7. ARM+LINUX 项目学习总结

    一.确定功能 二.系统移植 1. 根据具体板子修改u-boot (三星的开发板资料) 2. 根据具体板子和功能修改内核 (基本的驱动) 3. 移植busybox 三.驱动修改编写 四.应用编程 附1 ...

  8. Wind7外接显示器选择拓展模式后,鼠标只能往右移动才能切换到外接显示器上,不能修改切换方向

    win7外接显示器选择拓展模式后,为什么鼠标只能往右移动才能切换到外接显示器上,不能修改切换方向 打开控制面板-->显示 其他不变的情况下,鼠标拖动上面的两个显示器图标,拉出你希望的方向即可.

  9. Spring Bean

    一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...

  10. 神奇的margin之豆瓣豆瓣么么哒

    在经过周末的豆瓣主页和这周的豆瓣电影,表示网页什么的已经被我玩坏了. 老师在周末布置豆瓣主页,对于只学了四天的css和html的我,表示鸭梨山大. 最开始的两个小时只能做出一个连自己都看不下去的导航栏 ...