04-树5 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 the 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<iostream>
using namespace std;
struct treenode{
int data,h;
treenode* left=NULL;
treenode* right=NULL;
};
using tree=treenode*;
int height(tree t){
//cout<<"height(tree t)"<<endl;
if(!t) return ;
return max(height(t->left),height(t->right))+;
}
tree RotateLL(tree t){
//cout<<" RotateLL(tree t)"<<endl;
tree a=t->left;
t->left=a->right;
a->right=t;
a->h=max(height(a->left),height(a->right))+;
t->h=max(height(t->left),height(t->right))+;
return a;
}
tree RotateRR(tree t){
//cout<<"RotateRR(tree t)"<<endl;
tree a=t->right;
t->right=a->left;
a->left=t;
a->h=max(height(a->left),height(a->right))+;
t->h=max(height(t->left),height(t->right))+;
return a;
}
tree RotateLR(tree t){
//cout<<"RotateLR(tree t)"<<endl;
t->left=RotateRR(t->left);
return RotateLL(t);
}
tree RotateRL(tree t){
//cout<<"RotateRL(tree t)"<<endl;
t->right=RotateLL(t->right);
return RotateRR(t);
}
tree insert(tree t,int v){
//cout<<" insert(tree t,int v)"<<endl;
if(t==NULL){
t=new treenode();
t->data=v; t->h=;
return t;
}else if(v<t->data){
t->left=insert(t->left,v);
if(height(t->left)-height(t->right)==)
if(v<t->left->data)
t=RotateLL(t);
else t=RotateLR(t);
}else{
t->right=insert(t->right,v);
if(height(t->left)-height(t->right)==-)
if(v>t->right->data)
t=RotateRR(t);
else t=RotateRL(t);
}
t->h=height(t);
return t;
}
int main(){
int n;
cin>>n;
tree t=NULL;
for(int i=;i<n;i++){
int v; cin>>v;
t=insert(t,v);
}
cout<<t->data<<endl;
return ;
}

Root of AVL Tree的更多相关文章

  1. 04-树5 Root of AVL Tree + AVL树操作集

    平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...

  2. PAT 1066 Root of AVL Tree[AVL树][难]

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

  3. PTA (Advanced Level) 1066 Root of AVL Tree

    Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...

  4. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

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

  6. pat04-树4. Root of AVL Tree (25)

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

  7. pat1066. Root of AVL Tree (25)

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

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

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

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

随机推荐

  1. JSP | 基础 | 两种方式循环输出

    用循环连续输出三个你好,字体从小变大 第一种: <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = "&qu ...

  2. 记录Jmeter集成Jenkins运行Ant做接口监听

    最近在鼓捣Jmeter的接口测试,把他集成到了Jenkins上做自动化接口监听.把操作记录下来. 首先就是进行接口测试的编写.打开Jmeter.主要是把接口的测试逻辑和断言处理调通后就OK了,接口程序 ...

  3. 正则表达式test报错 is not a function

    var reg = "/^1[34578]\d{9}$/"; //错误格式,这是一个字符串 var reg2 = /^1[34578]\d{9}$/; //正确格式 reg .te ...

  4. C. Quiz 贪心 + 数学

    http://codeforces.com/problemset/problem/337/C 题意是给出n个题目,那个人答对了m道,然后如果连续答对了k道,就会把分数double 求最小的分数是什么. ...

  5. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...

  6. SOLR-disMax查询参数

    dismax参数用于处理用户输入的简单短语,并根据字段的重要度进行加权查询,查询范围为多个字段区域.dismax会忽略搜索字符串中的 "AND","OR", & ...

  7. 使用css3 制作switch开关

    使用css3来实现switch开关的效果: html代码: <!--switch开关--><div class="switch-btn"> <inpu ...

  8. OCP 11g 第二章练习

    练习 2-1 在Windows计算机上安装SQL Developer 在本练习中,将在Windows计算机上安装SQL Developer 1. 从以下URL下载当前SQL Developer版本: ...

  9. 一段字符串中间提取json字符串

    项目过程中经常打日志:LOG.error("[failure][CreateOrder] param:{}", JSON.toJSONString(userCreateOrderD ...

  10. oss图片上传失败

    在生产上跑的正常代码,新搭了个测试环境,发现oss上传失败! 开始分析oss是否有以各种类似于白名单的功能,不认识测试域名导致的...结果不是! 改变访问类型 因为oss节点Endpoint是在杭州, ...