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 ...
随机推荐
- 彻底禁用resource manager
禁用resource manager 由于发现系统的一个等待事件:resmgr:cpu quantum.这是由于resource manager的原因.看来resource manager 的bug还 ...
- VMware-workstation安装
下载:百度搜索VMware-workstation 开始安装:VMware-workstation-full_12.5.5.17738 更改安装目录F:\softwore\VMware\VMware ...
- ios-UI-汤姆猫德游戏实现
// // ViewController.m // UI-猜拳游戏 // // Created by jzq_mac on 15/7/15. // Copyright (c) 2015年 jz ...
- Ubuntu+caffe训练cifar-10数据集
1. 下载cifar-10数据库 ciffar-10数据集包含10种物体分类,50000张训练图片,10000张测试图片. 在终端执行指令下载cifar-10数据集(二进制文件): cd ~/caff ...
- DNS配置外网
dnssec开启,部分dns请求为不信任链导致解析延迟或者解析失败(error显示为不信任)解决方法: vi /etc/named.conf dnssec-enable no; dnssec-vali ...
- 11.QT窗口布局切割
int main(int argc, char *argv[]) { QApplication a(argc, argv); //MainWindow w; //w.show(); //左右分割 7 ...
- 使用IDEA 创建 MAVEN 项目
一,项目创建 1.File---New---project 选择maven 勾选Create from archtype,找到并选择org.apache.maven.archtype ...
- redis的windows版本下载地址及windows的客户端工具
源码:https://github.com/MSOpenTech/redis 安装包:https://github.com/MSOpenTech/redis/releases 客户端工具:https: ...
- 利用hexo+github创建个人博客
因为想拥有一个独属于自己的个人博客啊. 安装部署hexo 进入一个安全的目录,cd ~/Desktop 在 GitHub 上新建一个空 repo,repo 名称是「你的GitHub用户名.github ...
- UWP tips (与wp8.1的不同)
一.异步调用之后,要更新UI时,代码如下 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&g ...