题目

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node difer by at most one; if at any time they difer by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5

88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7

88 70 61 96 120 90 65

Sample Output 2:

88

题目分析

已知平衡二叉树建树序列,求建树后的根节点

解题思路

1.建树(平衡二叉树insert节点)

2.打印根节点

易错点

左旋、右旋、插入节点方法,参数列表中要用指针引用node *&root,否则是值传递,方法中对root本身的修改不会在main函数中生效

Code

#include <iostream>
using namespace std;
struct node {
int data;
int heigh=0;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {
heigh=1;
}
};
int getHeigh(node * root) {
if(root==NULL)return 0;
return root->heigh;
}
void updateHeigh(node * root) {
root->heigh=max(getHeigh(root->left),getHeigh(root->right))+1;
}
void L(node * &root) {
//左旋
node * temp=root->right;
root->right=temp->left;
temp->left=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
void R(node * &root) {
//右旋
node * temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
int getBalanceFactor(node *root) {
return getHeigh(root->left)-getHeigh(root->right);
}
void insert(node * &root, int val) {
if(root==NULL) {
root=new node(val);
return;
}
if(val<root->data) {
insert(root->left,val);
updateHeigh(root);
if(getBalanceFactor(root)==2) {
if(getBalanceFactor(root->left)==1) {
//LL
R(root);
} else if(getBalanceFactor(root->left)==-1) {
//LR
L(root->left);
R(root);
}
}
} else {
insert(root->right,val);
updateHeigh(root);
if(getBalanceFactor(root)==-2) {
if(getBalanceFactor(root->right)==-1) {
//RR
L(root);
} else if(getBalanceFactor(root->right)==1) {
//RL
R(root->right);
L(root);
}
}
}
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
node * root=NULL;
for(int i=0; i<n; i++) {
scanf("%d",&m);
insert(root,m);
}
printf("%d",root->data);
return 0;
}

PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]的更多相关文章

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

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

  2. PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]

    题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...

  3. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

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

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

  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 (25)

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

  8. PAT甲级1066. Root of AVL Tree

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

  9. pat1066. Root of AVL Tree (25)

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

随机推荐

  1. Java 将数据写入磁盘并读取磁盘上的文件

    package test; import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;impo ...

  2. dede开启会员功能

    登陆后台,找到菜单里面的系统基本参数设置>会员设置>开启会员功能,选择“是”,保存即可

  3. 19 03 02 HTTP和https

    HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer Protocol ov ...

  4. web安全(xss攻击和csrf攻击)

    1.CSRF攻击: CSRF(Cross-site request forgery):跨站请求伪造. (1).攻击原理: 如上图,在B网站引诱用户访问A网站(用户之前登录过A网站,浏览器 cookie ...

  5. Django——优美的Path()函数

    path( )作用:解析URL地址 path( ) 标准语法: (<>为必须的参数,[]为可选参数) path(<route>, <view>, [name=Non ...

  6. 八十八、SAP中ALV事件之二,事件的定义和事件子例程

    一.我们来到SE37,找到REUSE_ALV_EVENTS_GET相关的定义 二.我们需要用到下面这3个事件 三.我们添加一个第五步,并把显示ALV座位第六步.在第五步中定义三个事件 四.在末尾,添加 ...

  7. 136-PHP 使用类名访问static修饰的类方法

    <?php class test{ //定义一个类 public static function class_info(){ //定义类方法 return '这是一个用于测试的类.'; } } ...

  8. C++ for无限循环~

    无限循环 如果条件永远不为假,则循环将变成无限循环.for 循环在传统意义上可用于实现无限循环.由于构成循环的三个表达式中任何一个都不是必需的,您可以将某些条件表达式留空来构成一个无限循环. #inc ...

  9. 一、thinkphp安装

    参考:https://www.kancloud.cn/manual/thinkphp5/118006 0.在此之前安装好phpstudy https://www.xp.cn 1.下载composer, ...

  10. Elasticsearch 集群 - 健康检查

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...