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 (≤) 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
#include<cstdio>
#include<algorithm>
using namespace std; struct Node
{
int v;
int height;
Node *lchild, *rchild;
}*root; void Insert(Node* &root,int v);
Node* NewNode(int v);
void updateHeight(Node *root);
int getHeight(Node* root);
int getBalanceFactor(Node* root);
Node* R(Node* &root);
Node* L(Node* &root); int main()
{
int n;
int v;
scanf("%d",&n);
for (int i = ; i < n; i++)
{
scanf("%d",&v);
Insert(root,v);
}
printf("%d",root->v);
return ;
} void Insert(Node* &root, int v)
{
if (NULL == root)
{
root = NewNode(v);
return ;
} if (root->v > v)
{
Insert(root->lchild,v);
updateHeight(root);
if ( == getBalanceFactor(root))
{
if ( == getBalanceFactor(root->lchild))
{
R(root);
}
else if(- == getBalanceFactor(root->lchild))
{
L(root->lchild);
R(root);
}
}
}
else
{
Insert(root->rchild,v);
updateHeight(root);
if (- == getBalanceFactor(root))
{
if (- == getBalanceFactor(root->rchild))
{
L(root);
}
else if( == getBalanceFactor(root->rchild))
{
R(root->rchild);
L(root);
}
}
}
} Node* NewNode(int v)
{
Node* node = new Node;
node->lchild = node->rchild = NULL;
node->v = v;
node->height = ;
return node;
} void updateHeight(Node *root)
{
root->height = max(getHeight(root->lchild),getHeight(root->rchild)) + ; } int getHeight(Node* root)
{
if (NULL == root)
{
return ;
}
else
{
return root->height;
}
} int getBalanceFactor(Node* root)
{
return getHeight(root->lchild) - getHeight(root->rchild);
} Node* R(Node* &root)
{
Node *tmp = root->lchild;
root->lchild = tmp->rchild;
tmp->rchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
} Node* L(Node* &root)
{
Node *tmp = root->rchild;
root->rchild = tmp->lchild;
tmp->lchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}

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

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

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

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

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

  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. C#程序计算N阶行列式的值及N元一次方程组

    C#程序计算N阶行列式的值及N元一次方程组 用了挺长时间自行完成了C#程序计算N阶行列式的值及N元一次方程组.由于自己没有在网上查阅其他资料,所以只能硬着头皮用最朴素的思想和基础的算法进行编程.在给出 ...

  2. 为什么K8s会成为主流?

    容器技术和K8s是云原生概念的核心和基础.云计算诞生已有超过10年,但云计算时代的应用到底该是什么样子,一直没人能说清楚,也没人能确定云计算的基础架构将会如何发展.在K8s出现之前,没人设想过会有一个 ...

  3. js中 !==和 !=的区别是什么

    1.比较结果上的区别 !=返回同类型值比较结果. !== 不同类型不比较,且无结果,同类型才比较. 2.比较过程上的区别 != 比较时,若类型不同,会偿试转换类型. !== 只有相同类型才会比较. 3 ...

  4. 给router-link 标签添加事件@click 、@mouseover等无效

    需要加上native修饰符. 所以如果在想要在router-link上添加事件的话需要@click.native这样写 所以如果要事件有效的话,改成如下: <router-link v-for= ...

  5. 英语bitellos钻石bitellos单词

    大颗粒的钻石叫做bitellos,四大钻石指的就是“摄政王”.“南非之星”.“蓝色希望”和“光明之山”四颗钻石.经过琢磨的钻石光彩夺目.灿烂无比,历来被誉为“宝石之王”,科研领域里大颗粒的钻石叫做bi ...

  6. [linux]查找最大的文件

    查找最大的日志文件,命令: find ./ -type f | xargs -I{} du -m {}|sort -rnk1|head -10 offline一台机器保留限产,其他机器删除日志,命令: ...

  7. url请求时,参数中的+在服务器接收时为空格,导致AES加密报出javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher

    报错的意思的是使用该种解密方式出入长度应为16bit的倍数,但实际的错误却不是这个,错误原因根本上是因为在http请求是特殊字符编码错误,具体就是base64生成的+号,服务器接收时成了空格,然后导致 ...

  8. refPoint 别名与指针

    // refPoint.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...

  9. django-评论

    视图函数views.py # 订单评论 class OrderCommentView(View): def get(self, request, order_id): # 获取用户信息 user = ...

  10. list数组排序 Collections 按Date时间降序排列

    @ResponseBody @RequestMapping(value = {"K12", "12"}) public String refurbishLigh ...