AVLTree 平衡树
//测试数据
//第一组: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 平衡树的更多相关文章
- C#平衡树(AVLTree)
参考:http://www.cnblogs.com/skywang12345/p/3577479.html using System; using System.Collections.Generic ...
- 平衡树(AVL)详解
1. 为什么平衡树? 在二叉搜索树(BST,Binary Search Tree)中提到,BST树可能会退化成一个链表(整棵树中只有左子树,或者只有右子树),这将大大影响二叉树的性能. 前苏联科学家G ...
- 二叉排序树的创建删除中序输出&&平衡树
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- 实现Avl平衡树
实现Avl平衡树 一.介绍 AVL树是一种自平衡的二叉搜索树,它由Adelson-Velskii和 Landis于1962年发表在论文<An algorithm for the organi ...
- 二叉树,平衡树,红黑树,B~/B+树汇总
二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...
- 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】
平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...
- java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)
package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...
- Algorithms: 二叉平衡树(AVL)
二叉平衡树(AVL): 这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有. 后面两月又要忙了. ...
- 平衡树以及AVL树
平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...
随机推荐
- Thread
问题:编写一个能提现多线程的例子?假设有t1,t2两个线程,如何保证t2线程在t1线程执行完后再执行? package cn.changb.thread; public class MyThread ...
- Log4j源代码学习
了解log4j的源代码来源于项目中一次需求,我们想将系统所有的warn日志统一收集到common-warn.log的日志中去,以便于系统对其进行监控处理.于是模拟自动生成的error配置完成了warn ...
- sql server 查询表结构
--查询表结构start SELECT 序号 = a.colorder,字段名称 = a.name,字段描述 = f.value, 标识 then '√' else '' end, 主键 FROM s ...
- WinForm中MouseEnter和MouseLeave混乱的问题
MouseEnter+MouseLeave不行,我用了MouseMove+MouseLeave,效果一样 最近做个聊天的系统,仿照qq的界面设计,像qq聊天界面中字体.表情.截图等图片,鼠标放上去显示 ...
- 利用eclips创建一个maven项目
最近在写新项目,由于以前没有用过Maven,所以在创建Maven 项目的过程中遇到了很多问题...现在将整个创建过程给记录下来,方便查看 创建maven项目 创建完成项目后配置maven项目 修改we ...
- laraver mongo 查询操作
1,mongo 不支持特殊where条件(&,|) 2,mongo 可以连接mysql的表查询,但不支持连表的where查询
- JS 劫持来源网站并做指定跳转
有时候给网站做流量,免不了要做一些网站劫持的JS跳转,这里贴上一段劫持来源网站的JS跳转方法,很简单 <script> // 获取来源网站 var slyar = document.ref ...
- WordPress登陆页和后台面空白解决方法
真没想到我居然也会碰到这么蛋疼的事情,有一天我登陆博客,输入账号密码之后登陆没有反应,之后我就试着用首页前台登陆(因为这个模板前台带登陆功能),之后成功登陆进入后台更新文章.我想算了.这小毛病就丢那吧 ...
- Redis一个异常的解决办法,异常描述:Could not get a resource from the pool
异常描述: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poo ...
- ARM: STM32F7: hardfault caused by unaligned memory access
ARM: STM32F7: hardfault caused by unaligned memory access ARM: STM32F7: 由未对齐的内存访问引起的hardfault异常 Info ...