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 ;
}

最新文章

  1. 求解最大正方形面积 — leetcode 221. Maximal Square
  2. Java遍历List的时候删除item
  3. import了sun开头的类,虽然它在代码里压根就没派上用处!但是必须得引用!
  4. py继续
  5. 【C++基础】指针好难啊,一点点啃——基本概念
  6. postfix反垃圾邮件说明
  7. ajax 文件上传,ajax
  8. [android]清单文件中MAIN与LAUNCHER的区别
  9. iOS事件响应链(Responder Chain)
  10. python基础(四)字符串处理
  11. ASP.NET(C#) Repeater分页的实现
  12. HashMap和Hashtable的同和不同(详细比较)
  13. Facebook开源最先进的语音系统wav2letter++
  14. LeetCode【100. 相同的树】
  15. Mysqldump备份说明及数据库备份脚本分享-运维笔记
  16. python 多线程 并发socket实例
  17. React-Native-Android-Studio整合开发+环境配置+官方实例
  18. Atitit hibernate3 hinernate4 hibernate5新特性attilax总结
  19. iptable 大量需要封杀的ip地址便捷方法
  20. Android 如何监听一个线程的开始和结束

热门文章

  1. 使用jdbc的方式访问kylin cube的数据
  2. (zxing.net)二维码PDF417的简介、实现与解码
  3. Python笔记之format()格式输出全解
  4. Python 日常学习
  5. Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作
  6. iOS仿UC浏览器顶部频道滚动效果
  7. Maven 依赖管理问题小计
  8. JavaScript变量那些事
  9. MySQL数据库的账户管理
  10. Q312 戳气球