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. 使用NVIDIA GRID vPC支持视频会议和算力工具

    随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在任何地方安全地工作.然而,最新的算力和视频会议应用程序需要 ...

  2. Java基础知识之this关键字知识讲解

    this关键字这里对java中this关键字的基础知识进行讲解,希望对热爱java的小伙伴有帮助!! /* this关键字代表了所属函数的调用者对象. this关键字的作用: 1. 如果存在同名成员变 ...

  3. Python OpenCV图片转视频 工具贴(三)

    Python OpenCV图片转视频 粘贴即用,注意使用时最好把自己的文件按照数字顺序命名.按照引导输入操作. # 一键傻瓜式引导图片串成视频 # 注意使用前最好把文件命名为数字顺序格式 import ...

  4. 深入解读Redis分布式锁

    之前码甲哥写了两篇有关线程安全的文章: 你管这叫线程安全? .NET八股文:线程同步技术解读 分布式锁是"线程同步"的延续 最近首度应用"分布式锁",现在想想, ...

  5. 权限管理(基本权限、附加权限、ACL权限)

    基本权限 •访问方式(权限) 读取:允许查看内容-read   r 写入:允许修改内容-write    w 可执行:允许运行和切换-execute  x 对于文本文件 r: cat   head  ...

  6. Pytorch CNN网络MNIST数字识别 [超详细记录] 学习笔记(三)

    目录 1. 准备数据集 1.1 MNIST数据集获取: 1.2 程序部分 2. 设计网络结构 2.1 网络设计 2.2 程序部分 3. 迭代训练 4. 测试集预测部分 5. 全部代码 1. 准备数据集 ...

  7. 深入理解Spring的两大特征(IOC和AOP)

    一.spring 的优点? 1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很 ...

  8. 【题解】数颜色 STL vector数组

    小 C 的兔子不是雪白的,而是五彩缤纷的. 题目 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n只兔子排 ...

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

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

  10. 迁移Report Server DataBase时遇到的坑

    1.项目背景 由于历史原因,公司部分系统的Report是基于SQL Server Report Service搭建的,且Reporting Services 和Report Server DataBa ...