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, 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 ythe 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树的实现。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 100+5
typedef long long ll;
int n,a[N_MAX];
struct AVLtree {
int key=INF;
AVLtree* l_child, *r_child;
int height;
}T;
AVLtree *NIL;
AVLtree* root;
int Height(AVLtree* T) {//返回x的高度
int l_height=, r_height=;
if (T) {
l_height = Height(T->l_child);
r_height = Height(T->r_child);
return T->height = max(l_height, r_height) + ;
}
return ;//节点不存在,没有高度
} AVLtree* LeftRotation(AVLtree* a) {//左单旋
AVLtree *b = a->l_child;
a->l_child = b->r_child;
b->r_child = a;
a->height = Height(a);
b->height = Height(b);
return b;
}
AVLtree* RightRotation(AVLtree* a) {//右单旋
AVLtree* b = a->r_child;
a->r_child = b->l_child;
b->l_child = a;
a->height = Height(a);
b->height = Height(b);
return b;
} AVLtree* LeftRightRotation(AVLtree* a) {
a->l_child = RightRotation(a->l_child);
return LeftRotation(a);
}
AVLtree* RightLeftRotation(AVLtree* a) {
a->r_child = LeftRotation(a->r_child);
return RightRotation(a);
} AVLtree* insert(int key,AVLtree*root) {//root是当前AVL树的根, 返回根
if (root == NIL) {
root = new AVLtree;
root->key = key;
root->height = ;
root->l_child = root->r_child = NIL;
}
if (key < root->key) {
root->l_child = insert(key,root->l_child);
if (Height(root->l_child)-Height(root->r_child)==) {
if (key < root->l_child->key) {
root = LeftRotation(root);
}
else root = LeftRightRotation(root);
}
}
else if (key > root->key) {
root->r_child = insert(key,root->r_child);
if (Height(root->r_child) - Height(root->l_child) == ) {
if (key > root->r_child->key) {
root = RightRotation(root);
}
else root = RightLeftRotation(root);
}
} root->height = Height(root);//节点插入完毕,计算当前根的高度
return root;
} int main() {
while (scanf("%d",&n)!=EOF) {
for (int i = ; i < n; i++) {
int a; scanf("%d",&a);
root=insert(a, root);
}
printf("%d\n",root->key);
}
return ;
}
pat 甲级 1066. Root of AVL Tree (25)的更多相关文章
- 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 ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲级——A1066 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
随机推荐
- 深入浅出:了解JavaScript的六种继承
了解继承前我们需要了解函数的构造,方便我们理解. 常见六种继承方式: 1.原型继承call和apply: 2.原型拷贝:循环父函数protype的key值=子函数prototype的key值: 3.原 ...
- Linux监控一之Nagios的安装与配置
一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...
- linux磁盘满了怎么办??删掉无用的大文件
今天公司网站突然无法访问,因为之前遇到过是因为磁盘问题,所以使用 df 命令查看结果,结果果然是有100%的东西,那么怎么解决呢,我们想到得查找大文件,并删掉无用的大文件比如log 那么linux如何 ...
- 【php】session_start 报 no such file
如果是yum安装修改php-fpm.conf 里面的 session.save_path 如果是编译的,修改php.ini 的session.save_path (此条未测试)
- 快速排序,对于相同元素的优化,c++
#include<iostream>using namespace std; void middl(int &p,int &q,int &r)//找枢轴,然后把枢轴 ...
- Linux文件类型 扩展名的作用
链接类型文件 查找显示管道文件 普通文件类型 file 查看文件的类型 data文件类型 创建块字和符设备 mknod 1,.tar .tar.gz .tgz .zip tar.bz 表示压缩文件,创 ...
- 裸奔着造房子——对政府禁止采购Win8系统的一些看法
前段时间有消息称政府招标的项目将禁止使用Win8系统,原因是Win8系统的安全架构将有利于暴露敏感信息给微软,而微软的老子是美利坚,老子想要知道什么,儿子当然不敢不从.因此Win8也被打入冷宫,微软多 ...
- Django基于Pycharm开发之四[关于静态文件的使用,配置以及源码分析](原创)
对于django静态文件的使用,如果开发过netcore程序的开发人员,可能会比较容易理解django关于静态文件访问的设计原理,个人觉得,这是一个middlerware的设计,但是在django中我 ...
- Nodejs-非阻塞I/O&事件驱动
1.关于es6变量 const 定义常量,不会发生改变的就用这个 let 定义局部变量 如: const fs=require('fs');//require()表示载入这个模块 function a ...
- 手机端sticker布局,底部按钮在屏幕底部
<template> <div class="product-detail-container"> <div class="detail&q ...