代码:

#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的更多相关文章

  1. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  2. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  3. 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 ...

  4. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  5. 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 ...

  6. 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 ...

  7. 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, ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Impala ODBC 安装笔记

    Impala在线文档介绍了 Impala ODBC接口安装和配置 http://www.cloudera.com/content/cloudera-content/cloudera-docs/CDH5 ...

  2. 使用Android design support library在Eclipse和Android Studio

    背景:为了在低版本号下使用Android 5.0的一些新特新 Eclipse篇: 1.将Android Manager中的Android support library升级到最新,我的为22.2.1' ...

  3. man pthread_mutex_init 或 man pthread_mutex_lock 没有结果的解决的方法

    问题: 在刚装好的 Mint/Ubuntu 可能会出现 man pthread_mutex 相关的函数没结果, 报No manual entry for pthread_mutex_init 的错误. ...

  4. UVA10370 Above Average

    Above Average It is said that 90% of frosh expect to be above average in their class. You are to pro ...

  5. Android - 加入Android的OpenCV依赖库(Android Dependencies) 问题

    加入Android的OpenCV依赖库(Android Dependencies) 问题 本文地址: http://blog.csdn.net/caroline_wendy 假设想要加入OpenCV的 ...

  6. 用select拼接insert into,单引号转义

    SELECT 'INSERT INTO dbo.CMS_Transformation ( TransformationName , TransformationCode , Transformatio ...

  7. Fine-tuning CaffeNet for Style Recognition on “Flickr Style” Data 数据下载遇到的问题

    (下载的时候没有提示 不知道是正在下 还是出现错误 卡着了)..一直没有反应 下载前要以管理员身份运行 sudo su 再 python examples/finetune_flickr_style/ ...

  8. Kylin基础教程(二)

    近期先把Kylin教程整理完毕,后续根据大家需求(可能会发起投票),整理其他技术栈知识教程. OK,那么接上一篇文章,感性认知了Kylin之后,我们先来看一下如何部署Kylin吧. 序号也依然沿用上一 ...

  9. Maven配置,使用IntelliJ IDEA和Maven创建Java Web项目

    1. 下载Maven 官方地址:http://maven.apache.org/download.cgi 解压并新建一个本地仓库文件夹 2.配置本地仓库路径   3.配置maven环境变量     4 ...

  10. Thingworx 使用REST API获取JSON数据

    版本:7.4.1 1.URL规则 http://localhost/Thingworx/Things/[Things名称]/Services/[Service名称]?method=POST&A ...