题目

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. vue-i18n多语言文件归类的两种方法

    1.按语言类型归类 流行的做法是按照语言对文件进行归类,目录结构类似于: --lang ----en ------test.json --------"abc": "ab ...

  2. G. Repeat it

    G. Repeat it time limit per test 2.0 s memory limit per test 64 MB input standard input output stand ...

  3. redis学习(三)

    一.Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 二.Redis 命令 1 ...

  4. CentOS7基于http方式搭建本地yum源

    1.创建yum软件保存目录[root@localhost ~]# mkdir -p /www/share/yum 2. 修改yum配置文件先备份yum配置文件,修改yum配置文件中yum软件包保存目录 ...

  5. 3 —— node —— 文件追加内容

    思想 : 先读取 , 再追加 const fs = require('fs') fs.readFile("./hello.txt","utf-8",(err,d ...

  6. excel提取数字

    部分提取,那么就用=-LOOKUP(,-MID(A1,MIN(FIND({0;1;2;3;4;5;6;7;8;9},A1&1234567890)),ROW($1:$1024))) ------ ...

  7. 3.python进制及其之间的转换

  8. 对plotTree的解释

    1.>>>a = 1/2/2   >>>a >>>0.25 2.def plotMidText(cntrPt,parentPt,txtString ...

  9. Flink Window窗口机制

    总览 Window 是flink处理无限流的核心,Windows将流拆分为有限大小的"桶",我们可以在其上应用计算. Flink 认为 Batch 是 Streaming 的一个特 ...

  10. JS笔记03

    JS图片库 标记 需求效果: 网页中的图片链接显示在网页中的图片框内部而不是打开新的页面 //html部分 <!DOCTYPE html> <html> <head> ...