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 ...
随机推荐
- CodeForces 501B Misha and Changing Handles(STL map)
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...
- ios学习路线—Objective-C(堆(heap)和栈(stack))
Objective-C的对象在内存中是以堆的方式分配空间的,并且堆内存是由你释放的,即release 栈由编译器管理自动释放的,在方法中(函数体)定义的变量通常是在栈内,因此如果你的变量要跨函数的话就 ...
- NEC html规范
HTML规范 - 整体结构 HTML基础设施 文件应以“<!DOCTYPE ......>”首行顶格开始,推荐使用“<!DOCTYPE html>”. 必须申明文档的编码cha ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--E-回旋星空
链接:https://www.nowcoder.com/acm/contest/90/E 来源:牛客网 1.题目描述 曾经有两个来自吉尔尼斯的人(A和C)恋爱了,他们晚上经常在一起看头上的那片名为假的 ...
- python写爬虫的弯路
一开始按照视频上的找了笔趣阁的网站先爬一部小说, 找了<遮天>,但是章节太多,爬起来太慢, 就换了一个几十章的小说. 根据视频里的去写了代码, 在正则表达式哪里出了很大的问题. from ...
- Cent-OS——服务状态
查看MySQL状态: systemctl start mysqld #启用 systemctl stop mysqld #关闭 systemctl restart mysqld #重启MySQL服务 ...
- 转:java23种设计模式
以下是学习过程中查询的资料,别人总结的资料,比较容易理解(站在各位巨人的肩膀上,望博主勿究) 概述 设计模式是针对某一类问题的最优解决方案,是从许多优秀的软件系统中总结出的. Java中设计模式(ja ...
- 【原创】面向对象作业:选课系统中用pickle储存多个对象间组合引用关系的那些坑
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9030121.html 想直接看结论先提前列出: 1.存储一个对象,文件不是真的给你存储的了对象这种东西, ...
- 如何解决thinkphp5中验证码常见问题?
对于thinkphp如何实现验证码,我这里就不介绍了.直接看之前的文章 http://www.cnblogs.com/qqblog/p/6639419.html.下面,我能想出来的是,我自己在开发过程 ...
- 首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法
首层nginx(172.25.10.1):先获取真实IP($remote_addr),再将真实IP传递给X-Forwarded-For proxy_set_header X-Real-IP $r ...