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 ...
随机推荐
- input输入的数据只允许整数和浮点型数据
//第一步:引入jquery //第二步:input输入框绑定该函数 例如:<input type="text" name="price" onInput ...
- Vim和Vi的常用命令
Vim 文本编辑器 1.Vim 和 Vi: 两者都是多模式编辑器: Vim 是 Vi 升级版,再兼容 Vi 所有指令的同时增加了一些新功能支持: 特点: 语法加亮:使用不同的颜色加亮代码: 多级撤销: ...
- MySQL面试题之为什么要为innodb表设置自增列做主键?
为什么要为innodb表设置自增列做主键? 1.使用自增列做主键,写入顺序是自增的,和B+数叶子节点分裂顺序一致 2.表不指定自增列做主键,同时也没有可以被选为主键的唯一索引,InnoDB就会选择内置 ...
- MySQL客户端工具的选择
最近因系统云化项目,学习使用MySQL集群,为了找一款顺手的mysql客户端,反复使用了多个工具,并筛选出一个自认为最满意的,在此分享. 先说我的选择:SQLyog. 尝试的客户端:Toad for ...
- matplotlib numpy scipy 的安装
一:windows 端的安装 #cmd指令 python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sym ...
- 计算机基础-CPU
CPU(Central Processing Unit中央处理器)由运算器和控制器组成--微机性能的集成度最高的核心部件 1.金属触点 2.附带散热器 风冷式 热管散热式 水冷式等 扣具结构要和CPU ...
- 拓普微智能TFT液晶显示模块
关键词: 串口屏, 液晶屏, TFT,人机界面 概述: 智能模块(Smart LCD)是专为工业显示应用而设计的TFT液晶显示模块. 模块自带主控IC.Flash存储器.实时嵌入式操作系统,客户主机可 ...
- 使用KVM虚拟机遇到的问题(持续更新)
1.qemu-kvm.kvm.qemu 和 qemu-system-x86_64四种命令的差别 qemu 和 qemu-system-x86_64:启动qemu虚拟机的命令,x86_64为64位系统时 ...
- P1481 魔族密码 (LIS)
题的连接:https://www.luogu.org/problemnew/show/P1481 简单思路: 就是LIS,最长上升子序列,当然把条件改一下,从模板里的A[ i ]> A[ j ] ...
- Omi框架学习之旅 - 插件机制之omi-finger 及原理说明
以前那篇我写的alloyfinger源码解读那篇帖子,就说过这是一个很好用的手势库,hammer能做的,他都能做到, 而且源码只有350来行代码,很容易看懂. 那么怎么把这么好的库作为omi库的一个插 ...