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 subtrees of any node differ by at most one; if at any time they differ 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

思路

模拟一下AVL树的过程。

四个旋转方法,左单旋,右单旋,左右双旋,右左双旋。具体看代码,函数名写得挺清楚的。

还需要一个高度函数,递归一下就得出来了。

最后再弄一个insert 函数,注意AVL树的特点,左右子树的高度差为2时必须发生平衡旋转。

code

#include <iostream>
using namespace std;
struct node{
int data;
node *left, *right;
node(int data){this->data = data, this->left = this->right = NULL;}
};
node* rotate_left(node* root){
node* temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;
}
node* rotate_right(node* root){
node* temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;
}
node* rotate_left_right(node* root){
root->right = rotate_left(root->right);
return rotate_right(root);
}
node* rotate_right_left(node* root){
root->left = rotate_right(root->left);
return rotate_left(root);
}
int getHeight(node* root){
if(root == NULL) return 0;
return max(getHeight(root->right), getHeight(root->left)) + 1;
}
node* insert(int data, node* root){
if(root == NULL) root = new node(data);
else if(data > root->data) {
root->right = insert(data, root->right);
if(getHeight(root->right) - getHeight(root->left) >= 2)
root = root->right->data > data ? rotate_left_right(root) : rotate_right(root);
}
else {
root->left = insert(data, root->left);
if(getHeight(root->left) - getHeight(root->right) >= 2)
root = root->left->data < data ? rotate_right_left(root) : rotate_left(root);
}
return root;
}
int main(){
int n = 0, temp = 0;
scanf("%d", &n);
node *root = NULL;
for(int i = 0; i < n; i++){
scanf("%d", &temp);
root = insert(temp, root);
}
printf("%d", root->data);
return 0;
}

PAT甲级: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)

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

  3. PAT甲级1066. Root of AVL Tree

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

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

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

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

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

  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. 外部NORFlash是第一个以硬件为基础的信任

    外部NORFlash是第一个以硬件为基础的信任 External NOR Flash memory is first with hardware root-of-trust 英飞凌科技公司宣布了它声称 ...

  2. Usb-type-C端口实现的挑战与设计方案

    Usb-type-C端口实现的挑战与设计方案 USB Type-C port implementation challenges and design solutions USB from 1.1 t ...

  3. Redis系列(二):常用操作

    一.数据类型 如果学过数据结构就会知道,操作往往是在特定的数据结构上的,不同的数据结构就会有不同的操作,Redis支持以下的数据类型: 字符串(Strings),列表(Lists),集合(Sets), ...

  4. 关于JAVA的FlowLayout流动布局的换行问题--图形界面

    我在网上寻找Java流动布局换行的方法,看了好久,也没有找到满意的答案. FlowLayout是流式布局,所以如果需要让换行有意义,就得锁定窗口的大小,否则随着窗口的伸缩,布局将被彻底打乱. 网上的方 ...

  5. NOIP模拟测试19「count·dinner·chess」

    反思: 我考得最炸的一次 怎么说呢?简单的两个题0分,稍难(我还不敢说难,肯定又有人喷我)42分 前10分钟看T1,不会,觉得不可做,完全不可做,把它跳了 最后10分钟看T1,发现一个有点用的性质,仍 ...

  6. 关于kubernetes的十七个实验(二)

    写在开头 时隔好几天,结束了毕业设计中期答辩,更新第二节. 实验二与Kubeadm Kubeadm解决了以下问题:处理TLS加密配置,部署核心Kubernetes组件并确保其他节点可以轻松加入集群. ...

  7. WEB安全新玩法 [3] 防护交易数据篡改

    在任何涉及交易的系统中,客户与商家之间的交易数据具有核心作用,如购买商品的价格.数量.型号和优惠券等.在客户挑选商品的过程中,这些交易数据逐渐形成:待客户提交订单时,交易数据被商家接收,形成双方认可的 ...

  8. 为什么 Python 没有函数重载?如何用装饰器实现函数重载?

    英文:https://arpitbhayani.me/blogs/function-overloading 作者:arprit 译者:豌豆花下猫("Python猫"公众号作者) 声 ...

  9. php 安装 yii 报错: phpunit/phpunit 4.8.32 requires ext-dom *

    php 安装 yii 报错: phpunit/phpunit 4.8.32 requires ext-dom * 我的版本是7.0,以7.0为例演示. 先装这两个拓展试试: sudo apt-get ...

  10. excel匹配函数vlookup和lookup

    1.vlookup(查找的条件,查找的区域,满足查找条件后需要返回的值在选中的查找区域的第几列,精确匹配还是近似匹配(精确匹配为0或False表示,反之为1或True)) =VLOOKUP(J2,$G ...