1066. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  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

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

  3. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

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

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

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

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

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

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

随机推荐

  1. 自己写一个Promise

    参考Promise 的  官方规范  https://promisesaplus.com/ Promise 其实就是一个状态机 它只有两种状态变化 pending    =>   fulfill ...

  2. POI Excel 插入新的行,下面的行动态移动

    在做Excel 模板时,会有遇到  模板行数不固定,如下图  需要在行次4下面再插入一行:注意:(插入的行如果是下面空白行,需要创建行) 解决方法是使用shifRows方法,第1个参数是指要开始插入的 ...

  3. Java代码随机生成图片验证码

    package com.rchm.util.images; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2 ...

  4. shell基础及变量符号

    kernel主要的功能: 1.内存的管理 2.设备驱动程序 3.文件系统的管理 4.进程的管理 5.网络系统   vim /etc/profile.d/ profile(主配置文件) .d(子配置文件 ...

  5. 【转】iPhone通讯录AddressBook.framework和AddressBookUI.framework的应用

    通讯录中联系人相关的应用iPhone提供了两个框架:AddressBook.framework和AddressBookUI.framework,使用这两个框架我们可以在程序中访问并显示iPhone数据 ...

  6. 并查集:HDU5326-Work(并查集比较简单灵活的运用)

    Work HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Time Limit: 2000/1000 MS (Java/Others) M ...

  7. 笔记-python-standard library-12.1 pickle

    笔记-python-standard library-12.1 pickle 1.      pickle简介 source code: Lib/pickle.py pickle模块实质上是一个实现p ...

  8. 消息框模块-tkinter

    import tkinter.messagebox # 这个是消息框,对话框的关键from tkinter import * error_fp_list = [[973.45, '河北卡卡汽车贸易有限 ...

  9. BZOJ 1531: [POI2005]Bank notes

    按余数分类 单调队列优化 #include<cstdio> using namespace std; int n,m,b[205],c[205],F[20005]; struct node ...

  10. IDEA配置好maven后新建maven项目一直build失败的解决方法

    maven配置了aliyun中央仓库后,IDEA新建maven项目一直出现以下问题: 相信有遇到这个问题的小伙伴很蛋疼,明明maven配置没错,新建项目却一直build失败,为了这个问题我重装过几次I ...