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


提交代码

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

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<queue>
  6 #include<vector>
  7 using namespace std;
  8 int num;
  9 struct node{
 10     int v,h;
 11     node *l,*r;
 12     node(){
 13         l=r=NULL;
 14         h=1;
 15     }
 16 };
 17 int max(int a,int b){
 18     if(a>b){
 19         return a;
 20     }
 21     else{
 22         return b;
 23     }
 24 }
 25 int GetHigh(node *h){
 26     if(!h){
 27         return 0;
 28     }
 29     return h->h;
 30 }
 31 void LeftRotation(node *&h){
 32     node *p=h->l;
 33     h->l=p->r;
 34     p->r=h;
 35     h=p;
 36     h->r->h=max(GetHigh(h->r->l),GetHigh(h->r->r))+1;
 37     h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
 38 }
 39 void RightRotation(node *&h){
 40     node *p=h->r;
 41     h->r=p->l;
 42     p->l=h;
 43     h=p;
 44     h->l->h=max(GetHigh(h->l->l),GetHigh(h->l->r))+1;
 45     h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
 46 }
 47 void RightLeftRotation(node *&h){
 48     LeftRotation(h->r);
 49     //h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
 50     RightRotation(h);
 51 }
 52 void LeftRightRotation(node *&h){
 53     RightRotation(h->l);
 54     //h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
 55     LeftRotation(h);
 56 }
 57 void AVLInsert(int v,node *&h){
 58     if(!h){//已经到了最底层
 59         h=new node();
 60         h->v=v;
 61         return;
 62     }
 63     //bool can=false;
 64     if(v<h->v){
 65         AVLInsert(v,h->l);
 66         if(GetHigh(h->l)-GetHigh(h->r)==2){
 67
 68             //can=true;
 69
 70             if(v<h->l->v){//左单旋
 71                 LeftRotation(h);
 72             }
 73             else{//左右双旋
 74                 LeftRightRotation(h);
 75             }
 76         }
 77     }
 78     else{
 79         AVLInsert(v,h->r);
 80         if(GetHigh(h->l)-GetHigh(h->r)==-2){
 81
 82             //can=true;
 83
 84             if(v>h->r->v){//左单旋
 85                 RightRotation(h);
 86             }
 87             else{//左右双旋
 88                 RightLeftRotation(h);
 89             }
 90         }
 91     }
 92     //if(!can)
 93     h->h=max(GetHigh(h->l),GetHigh(h->r))+1;  //更新树高为1或2的树的树高
 94 }
 95 /*void prefind(node *h){
 96     if(h){
 97         //cout<<11<<endl;
 98         cout<<h->v<<endl;
 99         prefind(h->l);
100         prefind(h->r);
101     }
102 }*/
103 int main(){
104     //freopen("D:\\INPUT.txt","r",stdin);
105     int n;
106     while(scanf("%d",&n)!=EOF){
107         node *h=NULL;
108         int i;
109         for(i=0;i<n;i++){
110             scanf("%d",&num);
111             AVLInsert(num,h);
112         }
113
114         //prefind(h);    //检测
115
116         printf("%d\n",h->v);
117     }
118     return 0;
119 }

模板:

 1 typedef struct AVLTreeNode *AVLTree;
 2 typedef struct AVLTreeNode{
 3    ElementType Data;
 4    AVLTree Left;
 5    AVLTree Right;
 6    int Height;
 7 };
 8 AVLTree AVL_Insertion ( ElementType X, AVLTree T )
 9 { /* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */
10    if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
11        T = (AVLTree)malloc(sizeof(struct AVLTreeNode));
12        T->Data = X;
13        T->Height = 0;
14     T->Left = T->Right = NULL;
15 } /* if (插入空树) 结束 */
16 else if (X < T->Data) { /* 插入 T 的左子树 */
17     T->Left = AVL_Insertion(X, T->Left);
18     if (GetHeight(T->Left) - GetHeight(T->Right) == 2 )
19     /* 需要左旋 */
20       if (X < T->Left->Data)
21         T = SingleLeftRotation(T); /* 左单旋 */
22       else
23         T = DoubleLeftRightRotation(T); /* 左-右双旋 */
24 } /* else if (插入左子树) 结束 */
25 else if (X > T->Data) { /* 插入 T 的右子树 */
26     T->Right = AVL_Insertion(X, T->Right);
27     if (GetHeight(T->Left) - GetHeight(T->Right) == -2 )
28     /* 需要右旋 */
29       if (X > T->Right->Data)
30         T = SingleRightRotation(T); /* 右单旋 */
31       else
32         T = DoubleRightLeftRotation(T); /* 右-左双旋 */
33 } /* else if (插入右子树) 结束 */
34 /* else X == T->Data,无须插入 */
35 T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+1;
36 /*更新树高*/
37 return T;
38 }
39 AVLTree SingleLeftRotation ( AVLTree A )
40 { /* 注意: A 必须有一个左子结点 B */
41   /* 将 A 与 B 做如图 4.35 所示的左单旋,更新 A 与 B 的高度,返回新的根结点 B */
42     AVLTree B = A->Left;
43     A->Left = B->Right;
44     B->Right = A;
45     A->Height = Max(GetHeight(A->Left), GetHeight(A->Right))+1;
46     B->Height = Max(GetHeight(B->Left), A->Height)+1;
47     return B;
48 }
49 AVLTree DoubleLeftRightRotation ( AVLTree A )
50 { /* 注意: A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */
51   /* 将 A、 B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */
52     A->Left = SingleRightRotation(A->Left); /*将 B 与 C 做右单旋, C 被返回*/
53     return SingleLeftRotation(A); /*将 A 与 C 做左单旋, C 被返回*/
54 }

转载于:https://www.cnblogs.com/Deribs4/p/4732690.html

pat04-树4. Root of AVL Tree (25)相关推荐

  1. PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树)

    文章目录 题目分析 题目链接 题目分析 图片来源:acwing 分析 平衡树(AVL树)是平衡二叉搜索树的简称,当然需要满足二叉搜索树的性质,左子树小于根,根小于等于右子树:然后还要满足平衡树的基本特 ...

  2. PAT 1066. Root of AVL Tree (25) 回レ!雪月AVL

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

  3. PAT1066 Root of AVL Tree (25)(AVL树)

    题意: 给出一系列要插入平衡搜索二叉树的数,要求输出最后的根节点 思路: 没其他办法,完完全全是AVL树的插入节点模拟,这题就不会写,看别人代码写的. #include<iostream> ...

  4. 1066 Root of AVL Tree (25 分)【难 / 知识点: 平衡树 未完成】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 平衡树之前学过,不过有忘完了,有时间补吧

  5. 1066 Root of AVL Tree——PAT甲级 | 参考mooc实现完整代码

    Root of AVL Tree  2013年浙江大学计算机学院免试研究生上机考试真题,是关于AVL树的基本训练. 原题链接:PTA | 程序设计类实验辅助教学平台 题目描述 AVL 树是一种自平衡的 ...

  6. PAT A1066 Root of AVL Tree ——春水碧于天,画船听雨眠

    PAT A1066 Root of AVL Tree AVL这东西记一次忘一次,每次看就像披着初恋外衣的旧情人(or reverse) 以下应该是较为标准的模板方法,只是好久没有用过指针了,所以写了个 ...

  7. 平衡查找树C语言程序,树4. Root of AVL Tree-平衡查找树AVL树的实现

    对于一棵普通的二叉查找树而言,在进行多次的插入或删除后,容易让树失去平衡,导致树的深度不是O(logN),而接近O(N),这样将大大减少对树的查找效率.一种解决办法就是要有一个称为平衡的附加的结构条件 ...

  8. 【PAT A1066】Root of AVL Tree

    #include <cstdio> #include <algorithm> using namespace std;struct node {int v, height; / ...

  9. 1066 Root of AVL Tree 需再做

    1. 这题如果不知道平衡二叉树怎么平衡的(左旋右旋那一套)应该不可能做出吧,那就输出中位数回点血了. 2. 需要具备的基础知识:怎么将结点插入平衡二叉树. 3. 我犯的一个错误:把更新高度的函数直接返 ...

最新文章

  1. qfdw.xyz sq.php,GitHub - STORMSQ/sqphp: 練習用框架,使用PHP搭建
  2. 冬奥会夺金的背后杀手锏,竟是位 AI 虚拟教练
  3. mysql主主和F5高可用_MYSQL 主主热备高可用方案与实现
  4. python从入门到实践_Python编程从入门到实践日记Day32
  5. excel几个表合成一张_快速将多个excel表合并成一个excel表
  6. 【Spring】Spring高级话题-@Enable***注解的工作原理
  7. Martin Fowler 微服务的原文翻译(转载)
  8. Parameter ‘username‘ not found. Available parameters are [arg1, arg0, param1, param2] 绑定参数异常
  9. (七)、Java异常类型及处理
  10. QPG分布框架1.1.1
  11. php 替换alt,PHP 实现自动添加或者替换 内容的IMG标签的 alt title 属性
  12. Java8 stream toMap 解决 key 冲突
  13. vscode运行c语言
  14. 微信公众平台技术揭秘之Referer的妙用
  15. Notepad++编译Verilog代码(精简)
  16. matplotlib.pyplot 标记出曲线上最大点和最小点的位置
  17. 初级会计资料-常用会计公式(三)
  18. LCD(GEC6818)
  19. 风林评《如何写影评》|如何写影评
  20. html 实现格子效果图,css 实现的九宫格图片展示

热门文章

  1. Mysql大量插入随机数据方法--存储过程
  2. 正确使用日志的10个技巧(转)
  3. ASP.NET MVC3 通过Url传多个参数方法
  4. ubuntu系统编译sh出错 默认dash不是bash
  5. 如何在vue项目中使用md5加密
  6. eclipse建java项目不见_秒建一个后台管理系统?用这5个开源免费的Java项目就够了...
  7. LeetCode 46. Permutations
  8. 【数据结构】堆的建立(边输入数据边建立)(给定数字顺序插入)
  9. 1044. 火星数字(20)-PAT乙级真题
  10. APM应用性能管理的过去二十年