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. CentOS7.5安装网易云音乐

    CentOS7中一直没有一个像样的音乐播放器,网易云音乐与深度科技团队在半年前就启动了“网易云音乐Linux版“, 但是只提供了Ubuntu(14.04&16.04)和deepin15版本,并 ...

  2. 转:Python安全 - 从SSRF到命令执行惨案

    转:https://www.leavesongs.com/PENETRATION/getshell-via-ssrf-and-redis.html Python安全 - 从SSRF到命令执行惨案 PH ...

  3. jQuery筛选

    1.filter筛选出与指定表达式匹配的元素集合 html: <p>Hello</p><p>Hello Again</p><p class=&qu ...

  4. 剑指offer-二叉查找树的第 K 个结点

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  5. 洛谷——P1609 最小回文数

    题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. 对于一个给定的N,请你寻找一个回文数P,满足P>N. 满足这样条件的回文数很多, ...

  6. Codeforces Round #119 (Div. 2) Cut Ribbon(DP)

    Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. 有的系统区apk需要对其系统签名,才能正常使用。

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 向方案公司索要platform.x509.pem 和platform.pk8这两个文件. ...

  8. [BZOJ1563][NOI2009]诗人小G(决策单调性优化DP)

    模板题. 每个决策点都有一个作用区间,后来的决策点可能会比先前的优.于是对于每个决策点二分到它会比谁在什么时候更优,得到新的决策点集合与区间. #include<cstdio> #incl ...

  9. 【构造】AtCoder Regular Contest 079 D - Decrease (Contestant ver.)

    从n个t变化到n个t-1,恰好要n步,并且其中每一步的max值都>=t,所以把50个49当成最终局面,从这里开始,根据输入的K计算初始局面即可. #include<cstdio> # ...

  10. 【FFT】BZOJ2179- FFT快速傅立叶

    [题目大意] 给出n位十进制a和b,求a*b. [思路] FFT.感觉弄起来比较麻烦,不如直接背板子. 注意一下MAXN的取值,我一开始非常随意地就写了60000*2+50,其实n是要扩展到最接近的2 ...