1066 Root of AVL Tree (25 分)(平衡二叉树)
就是AVL的模板题了 注意细节

#include<bits/stdc++.h> using namespace std;
typedef struct node;
typedef node * tree;
struct node
{
int v;
int heigh;
tree L,R;
}; int getheigh(tree root)
{
if(root==NULL) return ;
return root->heigh;
} void updataheigh(tree root)
{
root->heigh=max(getheigh(root->L),getheigh(root->R))+;
} int getBalance(tree root)
{
return getheigh(root->L)-getheigh(root->R);
} void L(tree &root)
{
tree temp;
temp=root->R;
root->R=temp->L;
temp->L=root;
updataheigh(root);
updataheigh(temp);
root=temp;
} void R(tree &root)
{
tree temp;
temp=root->L;
root->L=temp->R;
temp->R=root;
updataheigh(root);
updataheigh(temp);
root=temp;
}
void insertt(tree &root,int v)
{
if(root==NULL){
root=new node;
root->v=v;
root->heigh=;
root->L=root->R=NULL;
return;
}
if(v<root->v){
insertt(root->L,v);
updataheigh(root);
if(getBalance(root)==){
if(getBalance(root->L)==){
R(root);
}
else if(getBalance(root->L)==-){
L(root->L);
R(root);
}
}
}
else{
insertt(root->R,v);
updataheigh(root);
if(getBalance(root)==-){
if(getBalance(root->R)==-){
L(root);
}
else if(getBalance(root->R)==){
R(root->R);
L(root);
}
}
} }
int main()
{
int n;
scanf("%d",&n);
int x;
tree root;
root=NULL;
for(int i=;i<n;i++){
scanf("%d",&x);
insertt(root,x);
}
printf("%d\n",root->v);
return ;
}
1066 Root of AVL Tree (25 分)(平衡二叉树)的更多相关文章
- 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分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 Advanced 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 ...
- 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 sub ...
- PAT 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 sub ...
- 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 sub ...
随机推荐
- Unity3d-制作粒子光环特效
http://blog.csdn.net/ozhangseno/article/details/70799611
- uCOS-II消息邮箱的使用
具体使用方法与信号量的方式大同小易. 首先建立一个OS_EVENT结构体(事件控制块)的指针: OS_EVENT *MSBOX; 然后建立消息邮箱,返回值为事件控制块的指针: MSBO ...
- 在CentOS7系统上安装MySQL数据库
1.下载安装MySQL官方repo文件 下载MySQL的官方repo文件 [root@centos7 ~]# wget -i -c http://dev.mysql.com/get/mysql57-c ...
- oracle日常监控语句
oracle常用的性能监控SQL语句 一.查询历史SQL: ---正在执行的SQL语句: select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT fro ...
- Mbatis错误信息整理
***每存在一对接口和xml文件,必须在xml文件中定义好mapper标签及namespace ***每对接口必须和xml文件名必须一致 <mapper>标签中的names ...
- 洛谷P3871 [TJOI2010]中位数(splay)
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- flask之请求钩子
from flask import Flask from flask import abort app = Flask(__name__) # 在第一次请求之前调用,可以在此方法内部做一些初始化操作 ...
- python计算MD5
python有自带的MD5模块hashlib,用起来简单很多.Python Hashlib模块的使用说明 http://docs.python.org/2/library/hashlib.htmlfd ...
- elasticsearch 5.x 系列之五 数据导入导出
一.首先给大家发一个福利,分享一个elasticsearch 数据导出工具. esm github 源码地址: https://github.com/medcl/esm 下载编译好的对应elastic ...
- Java --本地提交MapReduce作业至集群☞实现 Word Count
还是那句话,看别人写的的总是觉得心累,代码一贴,一打包,扔到Hadoop上跑一遍就完事了????写个测试样例程序(MapReduce中的Hello World)还要这么麻烦!!!?,还本地打Jar包, ...