题目:

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

分析:主要是训练平衡树的基本操作,四种旋转方式。

代码:

#include <stdio.h>
typedef struct treeNode {
int data;
struct treeNode *left;
struct treeNode *right;
int height;
} AVLTreeNode; // 在PAT提交时出现MAX宏未定义的编译错误,故添加以下几行代码
#ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif // 获取节点高度
int GetHeight(AVLTreeNode *treeNode)
{
if (!treeNode) {
return ;
} else {
return MAX(GetHeight(treeNode->left), GetHeight(treeNode->right)) + ;
}
} AVLTreeNode *SingleLeftRotation(AVLTreeNode *A)
{
AVLTreeNode *B = A->left;
A->left = B->right;
B->right = A;
A->height = MAX(GetHeight(A->left), GetHeight(A->right)) + ;
B->height = MAX(GetHeight(B->left), GetHeight(B->right)) + ;
return B;
} AVLTreeNode *SingleRightRotation(AVLTreeNode *A)
{
AVLTreeNode *B = A->right;
A->right = B->left;
B->left = A;
A->height = MAX(GetHeight(A->left), GetHeight(A->right)) + ;
B->height = MAX(GetHeight(B->left), GetHeight(B->right)) + ;
return B;
} AVLTreeNode *DoubleLeftRightRotation(AVLTreeNode *A)
{
A->left = SingleRightRotation(A->left);
return SingleLeftRotation(A);
} AVLTreeNode *DoubleRightLeftRotation(AVLTreeNode *A)
{
A->right = SingleLeftRotation(A->right);
return SingleRightRotation(A);
} // 将data插入到AVL树tree中,并返回调整后的AVL树
AVLTreeNode *AVL_insertion(int data, AVLTreeNode *tree)
{
if (!tree) { // 若插入到空树中,新建一个节点
tree = (AVLTreeNode *)malloc(sizeof(AVLTreeNode));
tree->data = data;
tree->height = ;
tree->left = tree->right = NULL;
} else if (data < tree->data) { // 插入到左子树中
tree->left = AVL_insertion(data, tree->left);
if (GetHeight(tree->left) - GetHeight(tree->right) == ) { // 需要左旋
if (data < tree->left->data) { // 左单旋
tree = SingleLeftRotation(tree);
} else { // 左右双旋
tree = DoubleLeftRightRotation(tree);
}
}
} else if (data > tree->data) { // 插入到右子树中
tree->right = AVL_insertion(data, tree->right);
if (GetHeight(tree->right) - GetHeight(tree->left) == ) { // 需要右旋
if (data > tree->right->data) { //右单旋
tree = SingleRightRotation(tree);
} else {
tree = DoubleRightLeftRotation(tree); // 右左旋
}
}
} /* else data == tree->data 无需插入*/ tree->height = MAX(GetHeight(tree->left), GetHeight(tree->right)) + ; return tree;
} int main()
{
// 读取输入
int count = ;
scanf("%d", &count); AVLTreeNode *tree = NULL;
for (int i = ; i < count; i++) {
int data = ;
scanf("%d", &data);
tree = AVL_insertion(data, tree);
}
printf("%d", tree->data);
}

运行结果:

PAT004 Root of AVL Tree的更多相关文章

  1. 04-树5 Root of AVL Tree + AVL树操作集

    平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...

  2. PAT 1066 Root of AVL Tree[AVL树][难]

    1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...

  3. PTA (Advanced Level) 1066 Root of AVL Tree

    Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...

  4. PAT甲级1066. Root of AVL Tree

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

  5. 04-树4. Root of AVL Tree (25)

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

  6. pat04-树4. Root of AVL Tree (25)

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

  7. pat1066. 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 (25)

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

  9. Root of AVL Tree

    04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...

随机推荐

  1. .css()与.addClass()设置样式的区别

    对于样式的设置,addClass与css方法两者之间有什么区别? 可维护性: .addClass()的本质是通过定义个class类的样式规则,给元素添加一个或多个类.css方法是通过JavaScrip ...

  2. Andorid上拉加载更多的几种实现方式

    1.前言 Andriod中上拉加载更多的效果随处可见,因为一次性要展现的数据太多信息量太大的话,给用户的体验就很差(加载慢,界面卡顿.流量消耗大等),而加载更多可以控制每次加载条目的数量以达到快速加载 ...

  3. .gitignore 里面常写的值

    一般用这个文件来控制一些不想提交的内容 这个可以做一个参考 # Windows image file caches Thumbs.db ehthumbs.db   # Folder config fi ...

  4. hibernate 一对多关联

    package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistenc ...

  5. 移动端页面弹幕小Demo实例说明

    代码地址如下:http://www.demodashi.com/demo/11595.html 弹幕小Demo实例地址,点击看效果 写在前面:尝试做了一下弹幕的实例,欢迎提出并指正问题 问题说明: D ...

  6. iOS 扫雷游戏

    代码地址如下:http://www.demodashi.com/demo/11254.html 1.项目结构图 Viewcontroller:扫雷逻辑代码 LevelModel:扫雷难度选择代码 2. ...

  7. Git 创建仓库

    本文将为大学介绍如何创建一个远程的Git仓库.您可以使用一个已经存在的目录作为Git仓库或创建一个空目录. 使用您当前的目录作为Git仓库,我们只需要使它初始化. git init 使用我们指定目录作 ...

  8. C#调用Windows CMD命令并,返回输出结果或错误信息

    public static string InvokeExcute(string Command) { Command = Command.Trim().TrimEnd('&') + &quo ...

  9. python selenium --处理下拉框

    下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再定位到下拉框内里的选项. drop_down.html <html&g ...

  10. SpringCloud系列十七:Hystrix的监控

    1. 回顾 上文讲解了使用Hystrix为Feign添加回退,并通过Fallback Factory检查回退原因以及如何为Feign客户端禁用Hystrix. 2. Hystrix的监控 除实现容错外 ...