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# Linq 使用总结

    隐式类型匿名类型自动属性初始化器委托泛型泛型委托匿名方法Lambda表达式扩展方法迭代器LINQ System.Linq var arr = new[] { "c", " ...

  2. 3DESC加密算法

    3DESC 请求参数和响应参数全采用3des加密规则,由于我是用.NET对接的,而第三方是Java开发的,所以两种程序之间采用的算法有一点差异,java的3des加密采用的是"DESede/ ...

  3. tiny-spring 分析

    tiny-spring 分析 前言 在阅读 Spring 的源代码(依赖注入部分和面向切面编程部分)时遇到不少困惑,庞大的类文件结构.纷繁复杂的方法调用.波诡云谲的多态实现,让自己深陷其中.一头雾水. ...

  4. js 递归遍历对象 插入属性 遍历树结构

    // 向 info下面 每一项 插入 isShow test() { const _this = this; _this.info.isShow = false; let iteration = fu ...

  5. Beego学习笔记6:分页的实现

    实现分页的效果 1>     分页的实现的业务逻辑 1->每个页面显示N条数据,总的数据记录数M,则分页的个数为M%N==0?M/N:M/N+1; 2->页面渲染分页的html部分 ...

  6. 汽车制造商表态:必须依靠MES系统来管控流程

    汽车行业特点 汽车工业是一个高投入,高产出,集群式发展的产业部门. 汽车自身的投资,生产,研发,供应,销售,维修:前序的原材料,零部件,技术装备,物流:后序的油料,服务,信贷,咨询,保险,直至基础设施 ...

  7. 理解 spring 事务传播行为与数据隔离级别

    事务,是为了保障逻辑处理的原子性.一致性.隔离性.永久性. 通过事务控制,可以避免因为逻辑处理失败而导致产生脏数据等等一系列的问题. 事务有两个重要特性: 事务的传播行为 数据隔离级别 1.事务传播行 ...

  8. mac php thinkphp5 验证码报错 Call to undefined function think\captcha\imagettftext()

    百度一下,是GD库里缺少了freetype支持,然后各种拓展的方法都试了半天,php-v里都生效了,phpinfo里还是不生效,原来是各种文章里都缺少了最关键的一步,修改Apache的配置(我使用的是 ...

  9. SQL注入流程图

    http://127.0.0.1/sqli-labs-master/Less-2/index.php? id=1 1 输入单引号   ‘            进行检验是否存在输入 http://12 ...

  10. 自制Linux操作系统

    自制Linux操作系统 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.添加一块新的磁盘设备 1>.将虚拟机关机,点击"编辑虚拟机设置" 2>.点 ...