就是AVL的模板题了 注意细节

#include<bits/stdc++.h>

using namespace std;
typedef struct node;
typedef node * tree;
struct node
{
int v;
int heigh;
tree L,R;
}; int getheigh(tree root)
{
if(root==NULL) return ;
return root->heigh;
} void updataheigh(tree root)
{
root->heigh=max(getheigh(root->L),getheigh(root->R))+;
} int getBalance(tree root)
{
return getheigh(root->L)-getheigh(root->R);
} void L(tree &root)
{
tree temp;
temp=root->R;
root->R=temp->L;
temp->L=root;
updataheigh(root);
updataheigh(temp);
root=temp;
} void R(tree &root)
{
tree temp;
temp=root->L;
root->L=temp->R;
temp->R=root;
updataheigh(root);
updataheigh(temp);
root=temp;
}
void insertt(tree &root,int v)
{
if(root==NULL){
root=new node;
root->v=v;
root->heigh=;
root->L=root->R=NULL;
return;
}
if(v<root->v){
insertt(root->L,v);
updataheigh(root);
if(getBalance(root)==){
if(getBalance(root->L)==){
R(root);
}
else if(getBalance(root->L)==-){
L(root->L);
R(root);
}
}
}
else{
insertt(root->R,v);
updataheigh(root);
if(getBalance(root)==-){
if(getBalance(root->R)==-){
L(root);
}
else if(getBalance(root->R)==){
R(root->R);
L(root);
}
}
} }
int main()
{
int n;
scanf("%d",&n);
int x;
tree root;
root=NULL;
for(int i=;i<n;i++){
scanf("%d",&x);
insertt(root,x);
}
printf("%d\n",root->v);
return ;
}

1066 Root of AVL Tree (25 分)(平衡二叉树)的更多相关文章

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

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

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

  4. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

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

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

  6. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

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

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

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

  10. 04-树5 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. 【luogu P1343 地震逃生】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1343 菜 #include <queue> #include <cstdio> #i ...

  2. 【luogu P3879 [TJOI2010]阅读理解】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3879 我先说一句: 我永远都喜欢StellaSTL 这个题,很明显就是 trie树 hash map+vec ...

  3. ActiveX控件注册不起作用的解决办法

    公司写了一个ActiveX打印插件.其中一个同事的电脑死活不能用.于是我就上网找办法 这位兄弟写的比较清晰. ActiveX交互时浏览器的设置以及ActiveX控件注册的检测 http://blog. ...

  4. LeetCode6.Z字形变换 JavaScript

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  5. Angularjs基础(九)

    AngularJS 应用应用程序讲解 实例: <html ng-app="myNoteApp"> <head> <meat charset=" ...

  6. C++ push方法与push_back方法

    [转载他人的文章,下午在搞c++,谢谢共享] [摘要] push与push_back是STL中常见的方法,都是向数据结构中添加元素.初识STL,对于添加元素的方法以产生混淆,这里暂对两种方法作出比较分 ...

  7. 大数据开发从入门小白到删库跑路(一)- 获取Hadoop

    Hadoop是一个可以通过相对简单编程模型实现跨多台计算机集群分布式处理大型数据集的框架.它不是依赖于高额成本的硬件可靠性来提供高可用性,Hadoop的设计能从单个服务器扩展到数千台机器,每个机器提供 ...

  8. vue 导出流文件excel

    第一种方法:需要设置响应类型,这里还需要安装 npm install js-file-download --save ,然后引用 var fileDownload = require('js-file ...

  9. JDK6 新特性

    JDK6新特性目录导航: Desktop类和SystemTray类 JAXB2实现对象与XML之间的映射 StAX Compiler API 轻量级 Http Server API 插入式注解处理AP ...

  10. u-boot.bin生成过程分析

    ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...