AVL Tree

  An AVL tree is a kind of balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, they were the first dynamically balanced trees to be proposed. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time. Addition and deletion operations also take O(logn) time.
Definition of an AVL tree
An AVL tree is a binary search tree which has the following properties:
1. The sub-trees of every node differ in height by at most one.
2. Every sub-tree is an AVL tree.

Balance requirement for an AVL tree: the left and right sub-trees differ by at most 1 in height.An AVL tree of n nodes can have different height.
For example, n = 7:

So the maximal height of the AVL Tree with 7 nodes is 3.
Given n,the number of vertices, you are to calculate the maximal hight of the AVL tree with n nodes.

Input

  Input file contains multiple test cases. Each line of the input is an integer n(0<n<=10^9). 
A line with a zero ends the input. 
Output

  An integer each line representing the maximal height of the AVL tree with n nodes.Sample Input

1
2
0

Sample Output

0
1

解题思路:
  本题给出一个整数,要求输出其能建立的最高的平衡二叉树的高度。

  关于平衡二叉树最小节点最大高度有一个公式,设height[i]为高度为i的平衡二叉树的最小结点数,则height[i] = height[i - 1] + height[i - 2] + 1;

  因为高度为0时平衡二叉树:

  #

  高度为1时平衡二叉树:

0    #  或  #

       /         \

1  #             #

  

  高度为2时平衡二叉树:

0      #    或    #

         /    \          /   \

1    #     #     #     #

    /                 \

2  #                 #

  高度为i时平衡二叉树:

      #    或    #

        /    \          /   \

    i - 2   i - 1       i - 1    i - 2

  所以只需要将10^9内的数据记录后让输入的数据与之比较就可得到答案。(高度不会超过46)

 #include <cstdio>
using namespace std;
const int maxn = ;
int height[maxn];
int main(){
height[] = ;
height[] = ;
for(int i = ; i < maxn; i++){ //记录1 - 50层最小需要多少节点
height[i] = height[i - ] + height[i - ] + ;
}
int n;
while(scanf("%d", &n) != EOF){ //输入数据
if(n == ) //如果为0结束程序
break;
int ans = -;
for(int i = ; i < maxn; i++){ //从第0层开始比较
if(n >= height[i]) //只要输入的数据大于等于该点的最小需求答案高度加一
ans++;
else
break; //否则结束循环
}
printf("%d\n", ans); //输出答案
}
return ;
}

HDU 2193 AVL Tree的更多相关文章

  1. HDU 5513 Efficient Tree

    HDU 5513 Efficient Tree 题意 给一个\(N \times M(N \le 800, M \le 7)\)矩形. 已知每个点\((i-1, j)\)和\((i,j-1)\)连边的 ...

  2. 04-树5 Root of AVL Tree

    平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...

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

  4. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...

  5. 树的平衡 AVL Tree

    本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...

  6. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  7. 1123. Is It a Complete AVL Tree (30)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  8. A1123. Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. 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. Flask数据库

    一 数据库的设置 Web应用中普遍使用的是关系模型的数据库,关系型数据库把所有的数据都存储在表中,表用来给应用的实体建模,表的列数是固定的,行数是可变的.它使用结构化的查询语言.关系型数据库的列定义了 ...

  2. awk中NF的使用

    统计机器中网络连接各个状态个数 netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'   一下子不明白$NF是什么意 ...

  3. (zxing.net)二维码PDF417的简介、实现与解码

    一.简介 二维码PDF417是一种堆叠式二维条码.PDF417条码是由美国SYMBOL公司发明的,PDF(Portable Data File)意思是“便携数据文件”.组成条码的每一个条码字符由4个条 ...

  4. 如何实现一个IOS网络监控组件

    此文由作者朱志强授权网易云社区发布. Mobile Application Monitor IOS组件设计技术分享 背景 应用程序性能管理Application Performance Managem ...

  5. 和我一起学python

    近来python越来越火,很多人都出了教程,我也来出一个凑凑热闹吧. pycharm激活 http://idea.lanyus.com/ https://blog.csdn.net/u01404481 ...

  6. 如何让Gogland不过期,一直使用?

    Gogland是jetBrains公司出品的GO语言开发IDE,是目前最好的GO语言开发工具!!但是目前Gogland提供的试用版,有一定的使用期限,如何到期还能使用?经过我的测试,如果Gogland ...

  7. javascript学习日记1

    1.JavaScript:写入 HTML 输出 document.write("<h1>This is a heading</h1>"); document ...

  8. mysql常用日期、时间查询

    好记性不如烂笔头 select curdate(); --获取当前日期 select last_day(curdate()); --获取本月最后一天. day); -- 获取本月第一天 ,interv ...

  9. SaltStack Grains 详解

    简介 Grains 是SaltStack 的重要组件之一.主要记录minion的静态信息,比如CPU,内存,磁盘,网络信息等.Grains信息是minion启动时汇报给Master的. 刷新grain ...

  10. 考试题 T3

    题意分析 首先\(\%\%\%\%olinr\)以及花_Q\(julao\)当场切题 然后就是怎么求 \[max(|a-A|,|b-B|)=max(a-A,A-a,B-b,b-B)\] 我们令\(x_ ...