代码:

#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. 6 Javascript:函数

    函数 函数是面向任务的. 当我们面临一个须要可问题的时候.往往无处下手.这时候.须要将问题分解为多个任务,从而逐一击破. 这里就须要函数的帮助. 语法 function Name() { Body() ...

  2. 在eclipse中关联android源代码

    1打包源代码成jar: 1 新建一个java项目 2  import  想打包的源代码文件 3 export 这个文件 : 选择java->jar file .  这里会让你选择输出路径 2 加 ...

  3. c语言数组小谈

    #include <stdio.h> #include <stdlib.h> #define N 5 int main() { double score[5]; int i; ...

  4. html 中表单元素input type="hidden"的作用

    转自:https://blog.csdn.net/xiaobing_122613/article/details/54946559 (隐藏只是在网页页面上面不显示输入框,但是虽然隐藏了,还是具有for ...

  5. windows bat更改系统时间 & 同步internet时间

    最近刚好用到一款软件学习版,30天试用期满,但是还想继续用,不具体透露是什么软件了. 发现更改了系统时间后,这个软件就又能启动了,不过每次改来改去很麻烦,就写了段脚本 @echo off date 1 ...

  6. TP5异常处理

    TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...

  7. 关于懒加载中的self.和_

    ---恢复内容开始--- 在开发中,经常会用到懒加载,最常用的如加载一个数组 如图,在这个懒加载数组中有的地方用到了_array有的地方用到了self.array 原因是_array是直接访问,而se ...

  8. Redis学习笔记(五) 基本命令:Hash操作

    原文链接:http://doc.redisfans.com/hash/index.html 学习前先明确一下概念,这里我们把Redis的key称作key(键),把数据结构hash中的key称为fiel ...

  9. C++中的pair,make_pair学习

    std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型.例如std::pair<int,float> 或者 std::pair<double,do ...

  10. JS进阶 - 浏览器工作原理

    一.浏览器的结构 浏览器的主要组件为: 用户界面 - 包括地址栏.前进/后退按钮.书签菜单等.除了浏览器主窗口(显示页面),其他部分都属于用户界面. 浏览器引擎 - 在用户界面和渲染引擎之间传送指令. ...