[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 ...
随机推荐
- php smtp发送邮件功能
<?php header("Content-Type: text/html; charset=utf-8"); class smtp { /* Public Variable ...
- 用Logger来解释拦截
HZ 动态代理学了 不知道在工作中杂用哦 HE 现在一般不会直接用吧,一般都是用aspectJ这种完整aop的实现 STST 拦截方法调用 HZ 我见过把所有accessor方法放到切面的 还有tra ...
- Flask实战第57天:UEditor编辑器集成以及配置上传文件到七牛
相关链接 UEditor:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/website/download.html# ...
- Flask实战第54天:cms删除轮播图功能完成
后台逻辑 编辑cms.views.py @bp.route('/dbanner/',methods=['POST']) @login_required def dbanner(): banner_id ...
- shell脚本--字符串处理和动态数组
Linux下的文本处理命令,以清晰的列分割数据为高效处理源: awk 的gsub函数可替换指定字符串 echo "<tr><td>col1</td>< ...
- [BZOJ1563][NOI2009]诗人小G(决策单调性优化DP)
模板题. 每个决策点都有一个作用区间,后来的决策点可能会比先前的优.于是对于每个决策点二分到它会比谁在什么时候更优,得到新的决策点集合与区间. #include<cstdio> #incl ...
- [TCO2013]Block3Checkers
题意:一个网格上有一些障碍和$3$个在网格边界上的棋子,你要添加一些障碍使得没有两个棋子四连通,问最少添加多少个障碍 官方题解——一张图教你做人... 三个棋子将网格边界分成三段,添加障碍后网格中一定 ...
- 【二分】【线段树】hdu6070 Dirt Ratio
size(l,r)表示区间l,r权值的种类数,让你求min{size(l,r)/(r-l+1)}(1<=l<=r<=n). last[r]表示a[r]上一次出现的位置, 就是二分验证 ...
- 活动中使用提示框(Toast)
任务名称:活动中使用Toast 任务现象:点击button时,会弹出提示框:You Click Button 步骤 1.创建一个项目,新建活动和加载布局.参考: http://8c925c9a.wiz ...
- 5月学习总结(Ant-Design,mustache,require.js,grunt)
一.Ant-Design学习 因为Ant-Design是基于React实现的,之前自己也学过一段时间的React,对React还是相对比较熟悉的,在学习Ant-Design也还不算吃力. 最开始是从源 ...