//测试数据
//第一组: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. 汇编语言写出的helloworld运行过程

    一:首先说一点,这篇文章建立在懂一点汇编的基础上,有几个简单的命令,说以下: 1:-r命令 -r 查看寄存器 -r 寄存器 (如 -r AX) 修改寄存器的值: 2:-d命令 -d 地址:xxxx:x ...

  2. sshfs三步走----实用(mac)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff } p.p2 { margin: 0.0px 0. ...

  3. 【Java】多线程_学习笔记

    多线程 1.进程 进程:当一个程序进入内存运行时,它就成为了进程.进程具有独立性.动态性.并发性. A.独立性:进程是系统中独立存在的实体,它可以拥有自己独立的资源,每一个进程都拥有自己私有的地址空间 ...

  4. PBOC金融IC卡,卡片与终端交互的13个步骤,简介-第四组(转)

    十:联机处理-可选项终端根据卡片行为分析的结果,执行对应的处理.若卡片响应联机,则终端发起联机操作.联机处理使得发卡行后台可以根据基于后台的风险管理参数检查并授权批准或拒绝交易.除了传统的联机欺诈和信 ...

  5. javascript正则表达式

    引用:http://www.jb51.net/article/72192.htm

  6. Java类中中文问题

    一个奇怪问题 java类中要保存一个xml文件到数据库,2种传值方式其中1种不知何故会最终导致解析xml时报错. xml文件内容由StringBuffer定义,其中一段内容如下: sb.append( ...

  7. ADO.NET 使用通用数据库操作类Database (SQL Server)

    一.Web.config配置 <connectionStrings> <add name="constr_name" connectionString=" ...

  8. 可以结合react的ui组件

    https://ant.design/components/switch-cn/

  9. iOS多线程

    关于iOS多线程 概述 这篇文章中,我不会说多线程是什么.线程和进程的区别.多线程有什么用,当然我也不会说什么是串行.什么是并行等问题,这些我们应该都知道的. 在 iOS 中其实目前有 4 套多线程方 ...

  10. 使用ajaxfileupload插件进行Ajax Post 异步提交多个文件

    前台代码: <div> <div> <img src="images/pro_upload.png" onclick="javascript ...