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的更多相关文章

  1. 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题

    http://www.cnblogs.com/cvbnm/articles/1947743.html 多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 < ...

  2. MS SQL大值数据类型varchar(max)、nvarchar(max)、varbinary(max)

    在MS SQL2005及以上的版本中,加入大值数据类型(varchar(max).nvarchar(max).varbinary(max) ).大值数据类型最多可以存储2^30-1个字节的数据. 这几 ...

  3. (算法)Binary Tree Max Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

  4. 关于varchar(max), nvarchar(max)和varbinary(max)

    在MS SQL2005及以上的版本中,加入大值数据类型(varchar(max).nvarchar(max).varbinary(max) ).大值数据类型最多可以存储2^30-1个字节的数据.这几个 ...

  5. SQL Server中VARCHAR(MAX)和NVARCHAR(MAX)使用时要注意的问题(转载)

    在Microsoft SQLServer2005及以上的版本中,对于varchar(n).nvarchar(n)和varbinary(n)有了max的扩展.可以使用如:varchar(max).nva ...

  6. [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 ...

  7. [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 ...

  8. [Algorithm] 7. Serialize and Deserialize Binary Tree

    Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...

  9. #Leet Code# Binary Tree Max[待精简]

    描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max. 代码:待优化 def getLargeNode(se ...

随机推荐

  1. section

    @RenderSection("Header")   @section Header { <div class="view"> @foreach ( ...

  2. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练

    A - Archery Tournament 题目大意:按时间顺序出现靶子和射击一个位置,靶子的圆心为(x, y)半径为r,即圆与x轴相切,靶子不会重叠,靶子被击中后消失, 每次射击找出哪个靶子被射中 ...

  3. CentOS为中文显示

    [sudo yum groupinstall chinese-support]命令即可安装

  4. 【ASP.NET MVC】提高页面加载速度:脚本优化

    在这里我们说一下脚本优化的三个方法: 一.在我们做Web开发的时候,当我们引用Js文件的时候,我们一般会将js文件放在文档的head标签中,这时当页面加载的时候,浏览器会按着由上到下的顺序,当浏览器遇 ...

  5. YII2源码阅读:autoload_real.php 22~23行

    spl_autoload_register(array('ComposerAutoloaderInit32b8eb537f8e12e57c5e7bade69d01f0', 'loadClassLoad ...

  6. 洛谷P2168 [NOI2015] 荷马史诗 [哈夫曼树]

    题目传送门 荷马史诗 Description 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马 ...

  7. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. apache配置httpd.conf相关

    1.apache开启压缩AddOutputFilterByType 找到并打开apache/conf目录中的httpd.conf文件 在httpd.conf中打开deflate_Module,head ...

  9. hdu 5692 Snacks(dfs时间戳+线段树)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  10. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...