[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 ...
随机推荐
- IllegalStateException: Unable to find a @SpringBootConfiguration
此处需要改掉包名和类名
- mysql查看表结构,字段等命令
mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名;
- ZSTU OJ 4272 最佳淘汰算法
线段树. 处理出每个位置下一个位置是哪里.然后搞个线段树找一下最大值就可以了. #include<map> #include<set> #include<ctime> ...
- 洛谷P1197 [JSOI2008] 星球大战 [并查集]
题目传送门 星球大战 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这 ...
- Linux基础系列-Day5
网络管理 ifconfig网络管理工具 ifconfig依赖于命令中使用一些选项属性,不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置,但是通过ifconfig修改的通常为临时配置,即系统 ...
- JQuery总结+实例
JQuery是什么? Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...
- 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids
给你一个1~n的排列p,n是偶数,每次从中任选一对相邻的数出来,插到排列q的开头,如此循环,问你所能得到的字典序最小的排列q. 我们先确定q开头的两个数q1,q2,q1一定是p的奇数位的最小的数,而q ...
- Largest Smallest Cyclic Shift
Largest Smallest Cyclic Shift 题目来源: Atcoder Code Festival 2017 Qual B Problem F 题目大意: 有\(X\)个字符'a',\ ...
- Java学习笔记(13)
StringBuffer 增加 append(boolean b) 可以添加任意类型的数据到容器中 insert(int offset,boolean b) 指定插入的索引值,插入对应的内容 ...
- mysql-mmm故障整理
Auth: JinDate: 20140414 1.master-slave同步问题1)故障描述和错误代码:监控报警slave故障登录slave服务器查看mysql> show slave st ...