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:

  1. 5
  2. 88 70 61 96 120

Sample Output 1:

  1. 70

Sample Input 2:

  1. 7
  2. 88 70 61 96 120 90 65

Sample Output 2:

  1. 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=;
  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 ;
  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))+;
  37. h->h=max(GetHigh(h->l),GetHigh(h->r))+;
  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))+;
  45. h->h=max(GetHigh(h->l),GetHigh(h->r))+;
  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)==){
  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)==-){
  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或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=;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 ;
  119. }

模板:

  1. typedef struct AVLTreeNode *AVLTree;
  2. typedef struct AVLTreeNode{
  3. ElementType Data;
  4. 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 = ;
  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) == )
  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) == - )
  28.     /* 需要右旋 */
  29.       if (X > T->Right->Data)
  30. 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))+;
  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))+;
  46.     B->Height = Max(GetHeight(B->Left), A->Height)+;
  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. }

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. 安卓手机传递文件到Windows系统电脑

    1.需求说明 安卓手机传递文件到Windows系统电脑上不太方便,传递文件的原理花样太多.这里介绍纯净原生的蓝牙文件传递方式. 2.操作步骤 2.1 打开侧边栏面板 2.2 打开蓝牙,右键转至设置 2 ...

  2. 1233: 传球游戏 [DP]

    1233: 传球游戏 [DP] 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: 3 统计 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做 ...

  3. Redhat系的Linux系统里,网络主要设置文件简介【转载】

    以下是原文地址,转载请指明出处: http://blog.chinaunix.net/uid-26495963-id-3230810.html 一.配置文件详解在RHEL或者CentOS等Redhat ...

  4. Link cut tree 实现不高效的 LCA

    https://www.luogu.org/problemnew/show/P3379 求 a 和 b 的 LCA 考虑先 access(a),此时 a 和 root 在一条链上,再 access(b ...

  5. 百度编辑器 Ueditor使用记录

    Ueditor官网: http://fex.baidu.com/ueditor/#dev-bale_width_grunt UeditorAPI文档: https://ueditor.baidu.co ...

  6. 基于python-opencv3的图像显示和保存操作

    import cv2 as cv import numpy as np                        #导入库 print("------------------------ ...

  7. Linux常用的命令(3)

    1 文件的内容显示 cat 显示全部 more: 分屏幕显示,只能向后翻 less: 分屏幕显示,可以向上翻 head:查看前n行 默认10行 tail:查看后n行 -n -f: 查看文件尾部,不退出 ...

  8. Eclipse 使用TFS

    Install Soft , –> add http://dl.microsoft.com/eclipse/tfs   form:http://msdn.microsoft.com/en-us/ ...

  9. js 有用信息集

    1.java.cookie.js 库:轻易操作cookie 2.jquery.form.js 库:通过ajaxForm,ajaxsubmit 两个函数,将form转为ajax提交方式:https:// ...

  10. [SDOi2012]Longge的问题 BZOJ2705 数学

    题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). ...