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 (≤) 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<cstdio>
#include<algorithm>
using namespace std; struct Node
{
int v;
int height;
Node *lchild, *rchild;
}*root; void Insert(Node* &root,int v);
Node* NewNode(int v);
void updateHeight(Node *root);
int getHeight(Node* root);
int getBalanceFactor(Node* root);
Node* R(Node* &root);
Node* L(Node* &root); int main()
{
int n;
int v;
scanf("%d",&n);
for (int i = ; i < n; i++)
{
scanf("%d",&v);
Insert(root,v);
}
printf("%d",root->v);
return ;
} void Insert(Node* &root, int v)
{
if (NULL == root)
{
root = NewNode(v);
return ;
} if (root->v > v)
{
Insert(root->lchild,v);
updateHeight(root);
if ( == getBalanceFactor(root))
{
if ( == getBalanceFactor(root->lchild))
{
R(root);
}
else if(- == getBalanceFactor(root->lchild))
{
L(root->lchild);
R(root);
}
}
}
else
{
Insert(root->rchild,v);
updateHeight(root);
if (- == getBalanceFactor(root))
{
if (- == getBalanceFactor(root->rchild))
{
L(root);
}
else if( == getBalanceFactor(root->rchild))
{
R(root->rchild);
L(root);
}
}
}
} Node* NewNode(int v)
{
Node* node = new Node;
node->lchild = node->rchild = NULL;
node->v = v;
node->height = ;
return node;
} void updateHeight(Node *root)
{
root->height = max(getHeight(root->lchild),getHeight(root->rchild)) + ; } int getHeight(Node* root)
{
if (NULL == root)
{
return ;
}
else
{
return root->height;
}
} int getBalanceFactor(Node* root)
{
return getHeight(root->lchild) - getHeight(root->rchild);
} Node* R(Node* &root)
{
Node *tmp = root->lchild;
root->lchild = tmp->rchild;
tmp->rchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
} Node* L(Node* &root)
{
Node *tmp = root->rchild;
root->rchild = tmp->lchild;
tmp->lchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}
04-树5 Root of AVL Tree (25 分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- 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 ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
随机推荐
- C# ThreadStart中如何带参数
1.ThreadStart 线程执行带参数的方法,new Thread(new ThreadStart(delegate { ThreadTask(firstPage, lastPage); })); ...
- python爬虫---js加密和混淆,scrapy框架的使用.
python爬虫---js加密和混淆,scrapy框架的使用. 一丶js加密和js混淆 js加密 对js源码进行加密,从而保护js代码不被黑客窃取.(一般加密和解密的方法都在前端) http:// ...
- Redux 和React 结合
当Redux 和React 相接合,就是使用Redux进行状态管理,使用React 开发页面UI.相比传统的html, 使用React 开发页面,确实带来了很多好处,组件化,代码复用,但是和Redux ...
- 禁用浏览器自动给input填充账号和密码
如果input输入框type为text,设置autoComplete="off" <el-input v-model="ruleForm.loginId" ...
- 89.canvas制作爱心
<!DOCTYPE html> <html> <head> <title>JavaScript和html53D玫瑰花(程序员的情人节礼物)< ...
- Java JDBC 数据源
数据源有2种: 普通数据源 即数据库驱动自带的数据源 连接池 包括数据库驱动自带的连接池,以及DBCP.C3P0等常用的第三方连接池. 数据库驱动自带的数据源 //从propertie ...
- FastJson--阿里开源的速度最快的Json和对象转换工具 https://www.cnblogs.com/kaituorensheng/p/8082631.html
import java.util.ArrayList;import java.util.List;import java.util.HashMap;import java.util.Map; impo ...
- AOP的动态实现cglib和jdk
动态代理的两种实现以:cglib和jdk,spring的aop(切面)的实现原理就是采用的动态代理技术. 看完代码.动态代理的作用是什么: Proxy类的代码量被固定下来,不会因为业务的逐渐庞大而庞大 ...
- python之处理excel表格
xlrd xlrd是python中一个第三方的用于读取excle表格的模块,很多企业在没有使用计算机管理前大多使用表格来管理数据,所以导入表格还是非常常用的! 安装xlrd pip install ...
- golang之数据转换
golang按位取反符号和异或符号都是^. fmt.Printf("0x%X\n", 0xFF^0x55) var a uint8 = 0x55 fmt.Printf(" ...