04-树4. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

提交代码

平衡二叉树的建立、插入、更新。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int num;
struct node{
int v,h;
node *l,*r;
node(){
l=r=NULL;
h=;
}
};
int max(int a,int b){
if(a>b){
return a;
}
else{
return b;
}
}
int GetHigh(node *h){
if(!h){
return ;
}
return h->h;
}
void LeftRotation(node *&h){
node *p=h->l;
h->l=p->r;
p->r=h;
h=p;
h->r->h=max(GetHigh(h->r->l),GetHigh(h->r->r))+;
h->h=max(GetHigh(h->l),GetHigh(h->r))+;
}
void RightRotation(node *&h){
node *p=h->r;
h->r=p->l;
p->l=h;
h=p;
h->l->h=max(GetHigh(h->l->l),GetHigh(h->l->r))+;
h->h=max(GetHigh(h->l),GetHigh(h->r))+;
}
void RightLeftRotation(node *&h){
LeftRotation(h->r);
//h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
RightRotation(h);
}
void LeftRightRotation(node *&h){
RightRotation(h->l);
//h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
LeftRotation(h);
}
void AVLInsert(int v,node *&h){
if(!h){//已经到了最底层
h=new node();
h->v=v;
return;
}
//bool can=false;
if(v<h->v){
AVLInsert(v,h->l);
if(GetHigh(h->l)-GetHigh(h->r)==){ //can=true; if(v<h->l->v){//左单旋
LeftRotation(h);
}
else{//左右双旋
LeftRightRotation(h);
}
}
}
else{
AVLInsert(v,h->r);
if(GetHigh(h->l)-GetHigh(h->r)==-){ //can=true; if(v>h->r->v){//左单旋
RightRotation(h);
}
else{//左右双旋
RightLeftRotation(h);
}
}
}
//if(!can)
h->h=max(GetHigh(h->l),GetHigh(h->r))+; //更新树高为1或2的树的树高
}
/*void prefind(node *h){
if(h){
//cout<<11<<endl;
cout<<h->v<<endl;
prefind(h->l);
prefind(h->r);
}
}*/
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF){
node *h=NULL;
int i;
for(i=;i<n;i++){
scanf("%d",&num);
AVLInsert(num,h);
} //prefind(h); //检测 printf("%d\n",h->v);
}
return ;
}

模板:

 typedef struct AVLTreeNode *AVLTree;
typedef struct AVLTreeNode{
ElementType Data;
4 AVLTree Left;
AVLTree Right;
int Height;
};
AVLTree AVL_Insertion ( ElementType X, AVLTree T )
{ /* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */
if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
T = (AVLTree)malloc(sizeof(struct AVLTreeNode));
T->Data = X;
T->Height = ;
    T->Left = T->Right = NULL;
} /* if (插入空树) 结束 */
else if (X < T->Data) { /* 插入 T 的左子树 */
    T->Left = AVL_Insertion(X, T->Left);
    if (GetHeight(T->Left) - GetHeight(T->Right) == )
    /* 需要左旋 */
      if (X < T->Left->Data)
        T = SingleLeftRotation(T); /* 左单旋 */
      else
        T = DoubleLeftRightRotation(T); /* 左-右双旋 */
} /* else if (插入左子树) 结束 */
else if (X > T->Data) { /* 插入 T 的右子树 */
    T->Right = AVL_Insertion(X, T->Right);
    if (GetHeight(T->Left) - GetHeight(T->Right) == - )
    /* 需要右旋 */
      if (X > T->Right->Data)
30         T = SingleRightRotation(T); /* 右单旋 */
      else
        T = DoubleRightLeftRotation(T); /* 右-左双旋 */
} /* else if (插入右子树) 结束 */
/* else X == T->Data,无须插入 */
T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+;
/*更新树高*/
return T;
}
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意: A 必须有一个左子结点 B */
  /* 将 A 与 B 做如图 4.35 所示的左单旋,更新 A 与 B 的高度,返回新的根结点 B */
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right))+;
    B->Height = Max(GetHeight(B->Left), A->Height)+;
    return B;
}
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意: A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */
  /* 将 A、 B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */
    A->Left = SingleRightRotation(A->Left); /*将 B 与 C 做右单旋, C 被返回*/
    return SingleLeftRotation(A); /*将 A 与 C 做左单旋, C 被返回*/
}

pat04-树4. Root of AVL Tree (25)的更多相关文章

  1. 04-树4. Root of AVL Tree (25)

    04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  2. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

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

  4. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  5. pat1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  6. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  7. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  8. PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  9. PAT 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. web集群时session同步的3种方法

    在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,一个登录用户,一会是登录状态,一会又不是 ...

  2. android android各种应用的许可

    android各种应用的许可 在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里将各类访问权限一一罗列 ...

  3. sqlServer组合主键

    sqlServer   组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...

  4. kill 进程的一些小细节

    终止前台进程,可以用Ctrl+C组合键.但对于后台进程需要用kill命令. kill PID 还可以加信号(参数),默认情况下是编号为15的信号.term信号将终止所有不能捕捉该信号的进程. -s 可 ...

  5. 12、OpenCV Python 图像梯度

    __author__ = "WSX" import cv2 as cv import numpy as np def lapalian_demo(image): #拉普拉斯算子 # ...

  6. 190320运算符&数据类型

    一.运算符 1.算术运算符 + 加 - 减 * 乘 / 除 ** 平方 // 整除 % 取余 2.比较运算符 == 等于 > 大于 < 小于 <= 小于等于 >= 大于等于 ! ...

  7. 初用sqlite3.exe

    1.记得要先建立数据库文件 为了进行数据库的编写,我安装了sqlite3,由于刚接触数据库,我尝试着建立表,并插入元组,属性,用select from语句也可以调出写入的内容,但是不知道如何保存,直接 ...

  8. [HAOI2011]Problem b BZOJ2301 数学

    题目描述 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 输入输出格式 输入格式: 第一行一个整数 ...

  9. 红蓝对抗 - 蓝队手册(BTFM)(转载)

    本文已发表在嘶吼RoarTalk,未经授权,请勿转载! http://www.4hou.com/technology/10173.html 最佳阅读体验版:https://stackedit.io/v ...

  10. MySql 碎片

    查看某个表所占空间,以及碎片大小. select table_name,engine,table_rows,data_length+index_length length,DATA_FREE from ...