HDU 2193 AVL Tree
AVL Tree
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的更多相关文章
- HDU 5513 Efficient Tree
HDU 5513 Efficient Tree 题意 给一个\(N \times M(N \le 800, M \le 7)\)矩形. 已知每个点\((i-1, j)\)和\((i,j-1)\)连边的 ...
- 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 ...
- 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 ...
- 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 ...
- 树的平衡 AVL Tree
本篇随笔主要从以下三个方面介绍树的平衡: 1):BST不平衡问题 2):BST 旋转 3):AVL Tree 一:BST不平衡问题的解析 之前有提过普通BST的一些一些缺点,例如BST的高度是介于lg ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- centos7 安装SSH
1.安装OpenSSH服务(CentOS系统默认安装了openssh) yum install openssh-server -y 2.配置OpenSSH服务(默认的配置已可以正常工作) O ...
- C#基础笔记(第十五天)
1.Directory//File 文件 Path 路径 FileStream StreamReader StreamWriter 流 Directory 文件夹 目录 //创建文件夹 Directo ...
- 流式处理框架storm浅析(下篇)
本文来自网易云社区 作者:汪建伟 举个栗子 1 实现的目标 设计一个系统,来实现对一个文本里面的单词出现的频率进行统计. 2 设计Topology结构: 这是一个简单的例子,topology也非常简单 ...
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 二分
B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...
- 利用Python爬取电影网站
#!/usr/bin/env python #coding = utf-8 ''' 本爬虫是用来爬取6V电影网站上的电影资源的一个小脚本程序,爬取到的电影链接会通过网页的形式显示出来 ''' impo ...
- Spring表单验证
表单验证 给表单添加验证的步骤如下 1.在 pom.xml 里添加 hibernate-validator 依赖http://hibernate.org/validator/documentation ...
- leetcode-36-有效的数独
题目描述: 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以 ...
- Https 客户端与服务器交互过程梳理
本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...
- mysql工具——mysqlcheck(MYISAM)
基本介绍 演示: 使用optimize的时候,可能会出现 Table does not support optimize, doing recreate + analyze instead 这时候参考 ...
- .Net Core 使用NLog记录日志到文件和数据库
NLog 记录日志是微软官方推荐使用. 接下来,通过配置日志记录到文件和Sql Server数据库. 第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包 ...