[Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree
A balanced binary tree is something that is used very commonly in analysis of computer science algorithms. In this lesson we cover how to determine the maximum number of items it can accommodate.
We follow this with a discussion on the maximum height of a binary tree given we need to accommodate n items.
O
╱ ╲
↙ ↘
O O
↙ ↘ ↙ ↘
O O O O
↙ ↘ ↙ ↘ ↙ ↘ ↙ ↘
O O O O O O O O level 0 : 1 (2 ^ 0)
level 1 : 2 (2 ^ 1)
level 2 : 4 (2 ^ 2)
... 2^0 + 2^1 + 2^2 + 2^3 .... + 2^n
=>
Height of tree | Level | Items in level | Total
1 0 1 (+1 lie) 2
2 1 2 4
3 2 4 8
4 3 8 16 2^level + 2^level - 1
2*(2^level) - 1
2^(level + 1) - 1
2^h - 1
/**
* Returns the max items that can exist in a perfectly balanced binary tree
*/
export function maxItems(height: number) {
return ** height - ;
}
/**
* Returns the max height of a balanced binary tree given n items
*/
export function maxHeight(items: number) {
return Math.log2(items + );
}
[Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree的更多相关文章
- 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
http://www.cnblogs.com/cvbnm/articles/1947743.html 多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 < ...
- MS SQL大值数据类型varchar(max)、nvarchar(max)、varbinary(max)
在MS SQL2005及以上的版本中,加入大值数据类型(varchar(max).nvarchar(max).varbinary(max) ).大值数据类型最多可以存储2^30-1个字节的数据. 这几 ...
- (算法)Binary Tree Max Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...
- 关于varchar(max), nvarchar(max)和varbinary(max)
在MS SQL2005及以上的版本中,加入大值数据类型(varchar(max).nvarchar(max).varbinary(max) ).大值数据类型最多可以存储2^30-1个字节的数据.这几个 ...
- SQL Server中VARCHAR(MAX)和NVARCHAR(MAX)使用时要注意的问题(转载)
在Microsoft SQLServer2005及以上的版本中,对于varchar(n).nvarchar(n)和varbinary(n)有了max的扩展.可以使用如:varchar(max).nva ...
- [Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...
- [Algorithm] Given the root to a binary tree, return the deepest node
By given a binary tree, and a root node, find the deepest node of this tree. We have way to create n ...
- [Algorithm] 7. Serialize and Deserialize Binary Tree
Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...
- #Leet Code# Binary Tree Max[待精简]
描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max. 代码:待优化 def getLargeNode(se ...
随机推荐
- 服务器迁移到 Windows Servec 2008 R2 64 bit 和IIS 7问题记录
近期公司把网站迁移到了新的环境,windows service 2008 64 bit 和IIS 7,问题有点多,对系统使用影响很多,也困扰了我几个星期,现在记录一些主要的注意点 1.组件权限 系统使 ...
- String的hashCode分析
/** * Returns a hash code for this string. The hash code for a * {@code String} object is computed a ...
- AttributeError: 'ForeignKey' object has no attribute 're' 解决办法
使用 field_object.rel.model.objects.filter(**db_condition) 报错 forekey中存在rel,为什么不能调用? 通过以下语句观察 print(fi ...
- 派(Dispatch)
单派与多派 (Single Dispatch and Multi Dispatch) "检查一个数据项的类型,并据此去调用某个适当的过程称为基于类型的分派". 上面是来自<计 ...
- Codeforces 1104 D. Game with modulo-交互题-二分-woshizhizhang(Codeforces Round #534 (Div. 2))
D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...
- MySQL workbench中的PK,NN,UQ,BIN,UN,ZF,AI说明
- PK: primary key (column is part of a pk) 主键- NN: not null (column is nullable) 是否为空 (非空)- UQ: uni ...
- Eclipse generate javadoc
注:若遇到导出文档乱码,则点击上图的[next]按钮,在vm options的输入框输入 -J-Xmx180m —- 设置内存大小 (若遇到内存溢出时) -encoding utf-8 ...
- 自定义loading dialog --- 后背景透明
自定义loading dialog --- 后背景透明 <style name="loading_dialog" parent="android:style/ ...
- 1.创建spring cloud父工程和子模块
创建父工程 idea创建父工程 idea创建一个工程.父工程管理公共资源 添加子模块 选择添加到父工程里面spring_cloud_parent 相应的子模块添加到父工程的pom.xml文件里
- 将int型数值拆分成4字节