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 ...
随机推荐
- QT5 Failed to load platform plugin "windows" 终极解决方式 命令行问题
Failed to load platform plugin "windows" 这个错误在双击exe运行时不会出现,当传递命令行參数时出问题 ,解决方法: int main(in ...
- 0x35 高斯消元与线性空间
颓了十天回来做题果然…… 感觉还是很有收获的,这两以前都没学过 bzoj1013: [JSOI2008]球形空间产生器sphere poj1830(upd) 之前做得很烂还被 D飞*2 了..重做一次 ...
- ASP.NET Core-组件:目录
ylbtech-ASP.NET Core-组件:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http ...
- [jzoj 6080] [GDOI2019模拟2019.3.23] IOer 解题报告 (数学构造)
题目链接: https://jzoj.net/senior/#main/show/6080 题目: 题意: 给定$n,m,u,v$ 设$t_i=ui+v$ 求$\sum_{k_1+k_2+...+k_ ...
- Django迁移到mysql数据库时的错误
pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ ...
- PHP 导出excel 数据量大时
public function ceshiexcel1(){ set_time_limit(0); $filename = '病毒日志'; header('Content-Type: applicat ...
- 在Windows环境下使用短信猫收发短信的简单配置:
Windows简单配置: 1.插入usb接口,打开电脑的设备管理器,装驱动后,查看端口下设备的com(串口)是多少,接下来就是配置短信猫的必须参数: 如果短信猫是COM(串口)口的,一般端口是COM1 ...
- 路飞学城Python-Day30
11-僵尸进程与孤儿进程 现象:运行程序会产生父进程,在父进程中开子进程,这两个进程公用一个打印终端,运行的时候就只运行父进程,父进程虽然自己结束了,但是要等子进程结束完才会结束. 父进程可以开多个子 ...
- HTML 捕获window.close() 并做窗口关闭前的处理工作
转自:http://www.xinotes.net/notes/note/261/ <html> <head> <script language="JavaSc ...
- [洛谷P3697]开心派对小火车
题目:洛谷P3697 题目大意是有各站停列车(慢车,相邻2站时间A)和特急列车(相邻2站时间B),特急列车在特定站点停靠. 现在加一种快速列车(相邻2站时间C,A>C>B),停靠K站(包括 ...