PAT 1066. Root of AVL Tree (25) (AVL树)
来源:互联网 发布:002517恺英网络股票 编辑:程序博客网 时间:2024/06/05 05:41
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 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:588 70 61 96 120Sample Output 1:
70Sample Input 2:
788 70 61 96 120 90 65Sample Output 2:
88
提交代码
知识点:(1)AVL(平衡二叉树)。 (2) AVL树的建立,请参考这篇博文: http://blog.csdn.net/gabriel1026/article/details/6311339 。
请结合图文参考一下代码:
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>#define PI acos(-1)#define inf 0x3f3f3f3f#define SCD(a) scanf("%d",&a)#define SCDD(a,b) scanf("%d%d",&a,&b)#define SCF(a) scanf("%lf",&a)#define PTD(a) printf("%d\n",a)#define PTS(a) printf("%s\n",a)#define MST(a,b) memset(a, b, sizeof(a))using namespace std;// PTA (Advanced Level) Practise 1066 Root of AVL Tree (25)const int L = 10010;struct AVLNode{ int data; AVLNode* lchild; AVLNode* rchild;};AVLNode* T;void LL(AVLNode* &T){ AVLNode* t = T->rchild; T->rchild = t->lchild; t->lchild = T; T = t;}void RR(AVLNode* &T){ AVLNode* t = T->lchild; T->lchild = t->rchild; t->rchild = T; T = t;}void RL(AVLNode* &T){ RR(T->rchild); LL(T);}void LR(AVLNode* &T){ LL(T->lchild); RR(T);}int Height(AVLNode* &T){ if(!T) return 0; return max( Height(T->lchild), Height(T->rchild) ) + 1;}void InsertAVLNode(AVLNode* &T, int val){ if(!T){ T = new AVLNode; T->data = val; T->lchild = NULL; T->rchild = NULL; return; } if(val < T->data){ InsertAVLNode(T->lchild, val); if(Height(T->lchild) - Height(T->rchild) == 2){ if(val < T->lchild->data) RR(T); else LR(T); } }else{ InsertAVLNode(T->rchild, val); if(Height(T->rchild) - Height(T->lchild) == 2){ if(val > T->rchild->data) LL(T); else RL(T); } }}int main(){ int i, b, t, j, n; SCD(n); for(i=0;i<n;i++){ SCD(t); InsertAVLNode(T, t); } PTD(T->data);}
- PAT 1066. Root of AVL Tree (25) (AVL树)
- pat甲1066. Root of AVL Tree(AVL树)
- 【PAT】1066. Root of AVL Tree (25)
- 【PAT】1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree (25)
- pat 1066. Root of AVL Tree (25)
- pat甲 1066. Root of AVL Tree (AVL树)
- PAT (Advanced Level) 1066. Root of AVL Tree (25) AVL树的插入建树
- 1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise
- PAT 1066. Root of AVL Tree
- PAT 1066. Root of AVL Tree
- pat(A) 1066. Root of AVL Tree
- PAT 1066. Root of AVL Tree
- 【PAT】1066. Root of AVL Tree
- PAT A1066. Root of AVL Tree (25)
- PAT A1066. Root of AVL Tree (25)
- PAT A 1066. Root of AVL Tree (25)
- nginx 搭配php
- Oracle常见名词解释
- imx6q sgtl5000 alsa驱动分析与调试
- CVPR2016一些文章整理
- Android design tabLayout改变tabline颜色,及居中显示
- PAT 1066. Root of AVL Tree (25) (AVL树)
- ubuntu QT开发环境(三种方法安装Qt4.8,其中apt-get方法安装QT库最简单)good
- JS调用window.print() 方法时由于a链接的href过长导致样式错乱
- 在使用AndroidStudio打包apk时,遇到“xxx is not translated in xxx”错误的解决办法
- springboot(四):thymeleaf使用详解
- iOS
- 25个经典的Spring面试问答
- Excel在统计分析中的应用—第五章—统计指数-Part9-平均指数(调和平均指数)
- js核心基础之理解原型对象以及原型继承(二)