pat(A) 1066. Root of AVL Tree
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#define Max(a,b) ((a)>(b)? (a):(b))
using namespace std; struct Node
{
int value;
Node* l;
Node* r;
int BF; //左子树高度 - 右子树高度
void init(int v)
{
value=v;
l=NULL;
r=NULL;
BF=0;
}
}; int Abs(int a)
{
return a>=0?a:-a;
} int getBF(Node* p)
{
if(p == NULL)
return -1;
return p->BF; } bool isBalanced(Node* p)
{
return Abs(getBF(p->l) - getBF(p->r)) < 2;
} Node* LL(Node* p)
{
Node* child = p->l;
p->l = child->r;
child->r = p; p->BF = Max(getBF(p->l),getBF(p->r)) + 1;
child->BF = Max(getBF(child->l),getBF(child->r)) + 1;
return child;
} Node* RR(Node* p)
{
Node* child = p->r;
p->r= child->l;
child->l = p; p->BF = Max(getBF(p->l),getBF(p->r)) + 1;
child->BF = Max(getBF(child->l),getBF(child->r)) + 1;
return child;
} Node* LR(Node* p)
{
Node* child = p->l;
p->l = RR(child);
return LL(p);
} Node* RL(Node* p)
{
Node* child = p->r;
p->r =LL(child);
return RR(p);
} Node* Insert(Node* p,int x)
{
if(p!=NULL)
{
if(x>p->value) //R
{
p->r= Insert(p->r,x);
if(!isBalanced(p))
{
if(x> p->r->value) //R-R
p = RR(p);
else //R-L
p=RL(p);
}
}
else //L
{
p->l= Insert(p->l,x);
if(!isBalanced(p))
{
if(x > p->l->value) //L-R
p = LR(p);
else //L-L
p = LL(p);
}
}
}
else
{
p=(Node*)malloc(sizeof(Node));
(*p).init(x);
}
p->BF = Max(getBF(p->l),getBF(p->r)) + 1;
return p;
} /*void PrintTree(Node* root)//先序遍历AVL树
{
if(root != NULL)
{
cout<<root->value<<"--";
PrintTree(root->l);
PrintTree(root->r);
}
}*/ int main()
{
int n;
while(scanf("%d",&n)==1)
{
Node* root=NULL;
int x;
for(int i=0; i<n; i++)
{
scanf("%d",&x);
root=Insert(root,x);
}
printf("%d\n",root->value);
//PrintTree(root);
}
return 0;
}
pat(A) 1066. Root of AVL Tree的更多相关文章
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 甲级 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
- 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[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- 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 ...
随机推荐
- AlertDialog自己定义View的使用方法+怎样改变弹出框的大小
android系统定义了弹出框,支持我们自己定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = get ...
- 【Swift】学习笔记(六)——函数
函数 懂编程语言的来说这个是最主要的了,不论什么语言都有函数这个概念.函数就是完毕特定任务的独立代码块. 函数怎么创建: 1.创建一个无參无返回值的函数(实际上全部的函数都有返回值,这个函数返回vo ...
- oracle rac下调节redo log file 文件大小
rac下调节redo log file 文件大小 (1)查看当前日志信息: select * from v$logfile; (步骤2中得路径能够在这里MEMBER列看到,redo文件名称自己命名.比 ...
- 深刻理解Java中的String、StringBuffer和StringBuilder的差别
声明:本博客为原创博客,未经同意.不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(链接为http://blog.csdn.net/bettarwang/article/detai ...
- 2016.02.25,英语,《Vocabulary Builder》Unit 02
ag:来自拉丁语do.go.lead.drive,an agenda是要做事情的清单,an agent是代表他们做事的人,同时也是为他人做事的机构.拉丁语litigare包括词根lit,即lawsui ...
- 道里云SDN云网络技术:使云能够“众筹”
容器云来了! 容器云的网络规模将比虚拟机云的情况扩大10-100倍,容器云与虚拟机云互联需求也将使云网络管控复杂度成数倍增长.SDN业界迎来了空前挑战.本报告分享道里云公司SDN技术:怎样将云的 ...
- Laravel异常处理
Laravel异常处理 标签(空格分隔): php 自定义异常类 <?php namespace App\Exceptions; use Throwable; use Exception; cl ...
- 18.QT消息链筛选机制以及组合键
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 5 #include <Q ...
- tomcat配置一个服务监听两个端口
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" ...
- 在js在添版本号
为了增加用户访问网站体验,快速打开网页,许多网站都对不常更新的js,css文件在浏览器端设置了缓存.但如果在服务器端做了更新,浏览器使用的仍是缓存在本地的js文件,除非强制清缓存(ctrl+F5).为 ...