题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/668

5-6 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 NN (\le 20≤20) which is the total number of keys to be inserted. Then NN 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树然后解。AVL树如果不涉及删除操作,复杂性没有想象的那么高。需要研究下结点旋转,以及树高度的计算和管理。
/*
2017-06-27 23:38 答案正确 25 5-6 gcc 7 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 4/4 1 1
测试点2 答案正确 4/4 1 1
测试点3 答案正确 4/4 2 1
测试点4 答案正确 4/4 1 1
测试点5 答案正确 4/4 3 1
测试点6 答案正确 4/4 7 1
测试点7 答案正确 1/1 2 1
查看代码
*/ //AVL的原理和图示见http://www.cnblogs.com/Camilo/p/3917041.html
#include <stdio.h>
#define MAX_N 20 typedef struct AVLTreeNode *AVLTree;
typedef struct AVLTreeNode{
int Data;
AVLTree left;
AVLTree right;
int Height;
};
AVLTree workT=NULL; int Max(int a,int b)
{
return a>b?a:b;
} int GetHeight(AVLTree T)
{
if(T==NULL)
return 0;
else
return T->Height;
} //旋转部分-------------------------------------------------
//左单旋算法
AVLTree SingleLeftRotation(AVLTree A)
{
//A必须有一个左子结点b
//问题出在左子树的左子树上
//将A与B做左单旋,更新A与B的高度,然后把B返回
AVLTree B = A->left;
A->left = B->right;
B->right = A;
A->Height = Max(GetHeight(A->left),GetHeight(A->right)) +1;
B->Height = Max(GetHeight(B->left),GetHeight(B->right)) +1;
return B;
} AVLTree SingleRightRotation(AVLTree A)
{
//右单旋,问题出在右子树的右子树上
AVLTree B = A->right;
A->right = B->left;
B->left = A;
A->Height = Max(GetHeight(A->left),GetHeight(A->right)) +1;
B->Height = Max(GetHeight(B->left),GetHeight(B->right)) +1;
return B;
}
AVLTree DoubleLeftRightRotation(AVLTree A)
{
//左右双旋,插入的不平衡出现在左孩子的右子树上
//先对A的左儿子进行右单旋,再对A进行左单旋
A->left = SingleRightRotation(A->left);
return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation(AVLTree A)
{
//右左双旋,插入的不平衡出现在右孩子的左子树上
//先对A的右儿子进行左单旋,再对A进行右单旋
A->right = SingleLeftRotation(A->right);
return SingleRightRotation(A);
} //插入操作-------------------------------------------- AVLTree AVL_Insertion(int X,AVLTree T)
{
if(T==NULL)
{
// printf("ready to malloc\n");
T=malloc(sizeof(struct AVLTreeNode));
T->Data = X;
T->Height = 0;
T->left = T->right = NULL;
// printf("create node done\n");
}
else if(X < T->Data)
{
T->left = AVL_Insertion( X , T->left);
if(GetHeight(T->left) - GetHeight(T->right) == 2)
if(X < T->left->Data)
T = SingleLeftRotation(T); //左单旋
else
T = DoubleLeftRightRotation(T);//左右双旋
}
else if(X > T->Data)
{
T->right = AVL_Insertion( X , T->right);
if(GetHeight(T->right) - GetHeight(T->left) == 2)
if(X > T->right->Data)
T = SingleRightRotation(T);
else
T= DoubleRightLeftRotation(T);
}
// X==T时无需插入
T->Height = Max(GetHeight(T->left),GetHeight(T->right)) + 1; //树高为子树高度+1; return T; //返回调整后的树
} //--------------------------------------------------------
//查找
AVLTree Find(int X,AVLTree T)
{
if (T == NULL)
return NULL;
if (T->Data == X)
return T;
else if(T->Data > X)
return Find(X,T->right);
else if(T->Data < X)
return Find(X,T->left);
} int gNum;
int gWorkarray[MAX_N];
int getinput()
{
int i,temp;
scanf("%d",&gNum);
for(i=0;i<gNum;i++)
{
scanf("%d",&temp);
workT=AVL_Insertion(temp,workT);
}
} int main()
{
getinput();
printf("%d",workT->Data);
}

  

另外还有种实在没办法,投机取巧的骗分办法

/*
不完全正确的解法
此解法只作快速骗分用
有大概率AVL树的根节点应该是整个序列的中位数
如果有奇数序列应该是正中间的值
故取巧排序后取序列中间的值作为结果返回。
最后得21/25,有一个4分测试点没通过 */
#include <stdio.h>
#define MAX_N 20
int gNum;
int gWorkarray[MAX_N];
int getinput()
{
int i;
scanf("%d",&gNum);
for(i=0;i<gNum;i++)
{
scanf("%d",&gWorkarray[i]);
}
} void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
} int InsertionSort()
{
int i,j,temp;
for(i=1;i<gNum;i++)
{
j=i;
temp=gWorkarray[j];
while(j>0 && temp<gWorkarray[j-1])
{
swap(&gWorkarray[j],&gWorkarray[j-1]);
j--;
}
gWorkarray[j]=temp;
}
} void showarray()
{
int i;
for(i=0;i<gNum;i++)
printf("%d ",gWorkarray[i]);
printf("\n");
} int main()
{
getinput();
InsertionSort();
printf("%d",gWorkarray[gNum%2==0?gNum/2+1:(gNum/2)]);
//showarray();
}

  

PTA 04-树5 Root of AVL Tree (25分)的更多相关文章

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

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

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

  4. 04-树5 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 ...

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

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

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

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

  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. pat1066. Root of AVL Tree (25)

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

随机推荐

  1. [已读]编写高质量代码 改善JavaScript程序的188个建议

    吐槽一万遍,买的最后悔的一本,没有之一,大量篇幅抄袭<高性能javascript>,我记得还有部分抄袭<javascript精粹>,<javascript模式>有没 ...

  2. XSS漏洞解析(三)

    系统存在xss漏洞就容易引发CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为: ...

  3. AJPFX总结之Socket编程

    一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...

  4. 找不到draw9patch.bat?已经不用找了

    Google 已经因为 draw9patch 热门的原因,把它集成在 Android Studio 里面了, 你现在可以直接在 Android Studio 里直接打开编辑了.

  5. Deprecated: Assigning the return value of new by reference is deprecated in报错

    出现了Deprecated: Assigning the return value of new by reference is deprecated in wwwroot\common.inc.ph ...

  6. HYSBZ 1503 郁闷的出纳员 (Splay树)

    题意: 作为一名出纳员,我的任务之一便是统计每位员工的工资.但是我们的老板反复无常,经常调整员工的工资.如果他心情好,就可能把每位员工的工资加上一个相同的量.反之,如果心情不好,就可能把他们的工资扣除 ...

  7. HYSBZ 1588 营业额统计 (Splay树)

    题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...

  8. 【转载】SQL Server 2012 日志传送

    SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...

  9. macos openssl 生成rsa证书 -mark

    创建私钥 openssl genrsa -out rsa_private_key.pem 1024 创建无密码私钥 openssl pkcs8 -topk8 -inform PEM –nocrypt ...

  10. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...