就是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. PS快捷键和常用小知识

    1.快捷键: ctrl+引号 隐藏参考线 ctrl+冒号 隐藏网格线 ctrl+alt 复制选中区域 ctrl+alt+向下箭头 针对单行和单列选框复制移动 ctrl+shift+i 反向选择区域 c ...

  2. 深入浅出C指针

    http://bbs.9ria.com/blog-164422-18039.html 初学者在学习C语言时,通常会遇到两个瓶颈,一个是“递归”,一个是“指针”.大学老师在讲述这两个知识点时通常都是照本 ...

  3. 菜鸟笔记 -- Chapter 6.2 类的构成

    在前面我们讲过高级开发语言大多由7种语法构成,但这是一个很空泛的概述,下,面我们仅就针对Java程序来说一下构成一个Java程序的几大部分,其中类是最小的基本元素.类是封装对象属性和行为的载体,而在J ...

  4. rm -f + 文件名+* 与 rm -f + 文件名* 的不同效果,大坑呀。

    rm -f catalina.2018-10-22.*    与*号间无空格 rm -f catalina.2018-10-22. *    :多了空格:

  5. Angularjs基础(九)

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

  6. CASE WHEN 小结

    1.简单的一个case when 例子: CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END 2. case when 在一整个表为空强行让其显示出一个值,在其后 ...

  7. [Git add . ] 遇到The file will have its original line endings in your working directory 解决办法

    1.在新项目中使用[ git add . ]时出现: warning: LF will be replaced by CRLF in ...... The file will have its ori ...

  8. Co. - Microsoft - Windows - 通过任务计划,备份本地MySQL,数据上传Linux备份服务器

    需求 客户为Windows系统,安装MySQL,需要每日备份数据库到指定目录,并且上传到公司的备份服务器(Linux). 1.使用mysqldump备份MySQL数据库,使用FTP上传到阿里云Linu ...

  9. dom4j支持Xpath的具体操作

    ***默认情况下,dom4j不支持xpath. 如果想要使用xpath,需要引入jaxen-1.1-beta-6.jar包. 在dom4j中提供了两个方法来支持xpath. ***selectNode ...

  10. php如何将base64数据流文件转换为图片文件?

    2017-03-07 在开发中,自己遇到一个前端在上传图片的时候,使用的base64数据流文件显示的图片. 也就是说 <img src="data:image/jpg;base64,& ...