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. 如何为SharePoint文档库、文件夹、文件单独设置权限

    在这里使用截图的方式简单描述两个问题:设置SharePoint Server文档库权限和文档库中的文件夹权限 一.设置SharePoint Server文档库权限 Figure 1 - 打开文档库后, ...

  2. sql 存储过程带有模糊查询条件

    一个简单的存储过程: Create procedure [dbo].[Proc_SeachJob] (@startRecordIndex int, @endRecordIndex int, @seac ...

  3. 会HTML/CSS就可以轻松创建网站

    网站其本质就是HTML + CSS 外加一些JavaScript构成的.所以基本上只要你会一些前端,就可以开始花样搭网站了. 如果只用HTML/CSS那做出来的网站只能叫静态网站,性能好但维护不方便, ...

  4. iOS 添加 Watch OS 1 应用后无法运行 An error was encountered while running (Domain = LaunchServicesError, Code = 0)

    在 iOS 应用基础上我添加了一个 Watch OS 2 应用,运行良好.又加了一个 Watch OS 1 应用,然后就所有 Target 都不能运行了. 运行时说 An error was enco ...

  5. Exp5 MSF基础应用 20164323段钊阳

    网络对抗技术 20164323 Exp5 MSF基础应用 靶机 ip:192.168.1.229 kali ip:192.168.1.216 exploit选取 主动攻击:ms17_010_psexe ...

  6. javascript学习日记1

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

  7. php性能优化三(PHP语言本身)

    0.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...

  8. asp.net c# 虾米音乐API

    最近用到虾米音乐的功能,主要是做一个分享音乐功能,找到好多代码,但是比较杂,有用的很少,因 此在此记录下,方便以后自己使用. 对于第三方网站,只要获取了唯一标识,基本上能抓取一些信息. 虾米 音乐的I ...

  9. package.json和bower的参数解释

    package.json和bower的参数解释   一.package.json解释: package.json是用来声明项目中使用的模块,这样新的环境部署时,只要在package.json文件所在的 ...

  10. HDU-1087-Super Jumping! Jumping! Jumping!(线性DP, 最大上升子列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...