PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6803291.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~
题意:给你一个插入的序列,问你最后AVL树的根节点是多少
AVL树模板题
如果会AVL树,那么就是水题
如果不会的话,那么就是难题
我刚开始也不会,所以根本写不出来AVL树。。。
后来花了一些时间学习了下,感觉网上很多模板都是用class写的,太麻烦了
模板就是应该要简洁点、方便的,A题的时候哪有功夫写那么复杂的模板
我就用的struct结构体来写的,一样好用,尽可能地简洁
后面(具体时间还不确定)我会给出AVL树的学习专栏
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
const int maxn=;
int n;
struct Node{
int l,r;
int val;
int h;
}; struct AVLTree{
Node node[maxn];
int cnt=;
int height(int u){
if(u==-)
return ;
return node[u].h;
}
/**
k1 is the current root,(向)右旋,顺时针旋转
对k1的左儿子L的左子树L进行了一次插入,所以是LL
*/
int RotateLL(int k1){
int k2;
k2=node[k1].l;
node[k1].l=node[k2].r;
node[k2].r=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].l),node[k1].h)+;
return k2; //new root
}
/**
k1 is the current root,(向)左旋,逆时针旋转
对k1的右儿子R的右子树R进行了一次插入,所以是RR
*/
int RotateRR(int k1){
int k2;
k2=node[k1].r;
node[k1].r=node[k2].l;
node[k2].l=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].r),node[k1].h)+;
return k2;// new root
}
/**
对k1的左儿子L的右子树R进行插入,所以是LR
先对k1的左儿子进行(向)左旋操作
再对k1进行(向)右旋操作
*/
int RotateLR(int k1){
node[k1].l=RotateRR(node[k1].l);
int root=RotateLL(k1);
return root;
}
/**
对k1的右儿子R的左子树L进行插入,所以是RL
先对k1的右儿子进行(向)右旋操作
再对k1进行(向)左旋操作
*/
int RotateRL(int k1){
node[k1].r=RotateLL(node[k1].r);
int root=RotateRR(k1);
return root;
}
/**
插入操作
就分LL\LR\RR\RL四种情况
*/
int insert_val(int val,int root){
//int res=root;
if(root==-){
node[cnt].l=node[cnt].r=-;
node[cnt].val=val;
node[cnt].h=;
root=cnt;
cnt++;
//return cnt;
}
else if(val<node[root].val){
node[root].l=insert_val(val,node[root].l);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==){
if(val<node[left].val){
root=RotateLL(root);
}
else{
root=RotateLR(root);
}
}
}
else if(val>node[root].val){
node[root].r=insert_val(val,node[root].r);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==-){
if(val>node[right].val){
root=RotateRR(root);
}
else{
root=RotateRL(root);
}
}
}
else{
//nothing
}
node[root].h=max(height(node[root].l),height(node[root].r))+;
return root;
}
}avltree;
/*
void dfs(int u){
if(u==-1)
return;
dfs(avltree.node[u].l);
printf("%d:%d\n",u,avltree.node[u].val);
dfs(avltree.node[u].r);
}
*/
int main()
{
int a;
int root=-;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a);
root=avltree.insert_val(a,root);
//printf("root:%d\n",root);
//dfs(root);
}
printf("%d\n",avltree.node[root].val);
return ;
}
如果之前不了解AVL数的基本旋转操作,可以参考下面这个博客,算是讲的比较清楚的:
http://blog.chinaunix.net/uid-25324849-id-2182877.html
PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题的更多相关文章
- 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 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT甲级题解-1047. Student List for Course (25)-排序
一开始是建立了course[2501][40001]数组,存储每节课的学生编号然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时后来发现最多40000学生,每个 ...
- PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
随机推荐
- Pandas:让你像写SQL一样做数据分析
1. 引言 Pandas是一个开源的Python数据分析库.Pandas把结构化数据分为了三类: Series,1维序列,可视作为没有column名的.只有一个column的DataFrame: Da ...
- python 从外部获取传入的参数
有时候我们在执行python程序的时需要接收到外部传入的参数 python的 sys.argv[]就能实现 # test.py import sys #引入模块 str = sys.argv[1]pr ...
- java 封装及this 用法
封装:主要用于将类中的成员名(类变量)通过 private关键字进行访问权限的设定,使用 private后,成员变量只能在当前类中进行访问,超过该类时访问提示不存在,当然也可以用于方法中,但较少.如果 ...
- 前端性能优化成神之路--图片懒加载(lazyload image)
图片懒加载(当然不仅限于图片,还可以有视频,flash)也是一种优化前端性能的方式.使用懒加载可以想要看图片时才加载图片,而不是一次性加载所有的图片,从而在一定程度从减少服务端的请求 什么是懒加载 懒 ...
- GUI练习3
将阿里山的积分卡拉斯的发生的咖啡机啊圣考虑到发送到敬爱费卢卡斯加
- c++のmap的遍历
一.定义如:map < int, CString > 或者 map < int, 结构体名>的元素遍历 map < int, CString > map; ...
- php 请求另一个服务器接口返回数据
<?php /** * Created by PhpStorm. * User: thinkpad * Date: 2015/7/17 0017 * Time: 13:24 */ class A ...
- Bug 14143011 : ORA-19606: CANNOT COPY OR RESTORE TO SNAPSHOT CONTROL FILE
Bug 14143011 : ORA-19606: CANNOT COPY OR RESTORE TO SNAPSHOT CONTROL FILE [oracle@test]$ tail -f rma ...
- springboot+mybatis+springmvc整合实例
以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是“零配置”,&quo ...
- C++ 指针常量和常量指针
1.指针常量(*const):对应指针变量,即指针本身是常量,指针指向的内容可以被修改. 2.常量指针(const*):常量的指针,即指针指向的内容不能被修改,但指针本身是变量,可以被修改.