Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's call the minimum height required to fit <code>N</code> nodes in a binary tree <code>minheight(N)</code>.</p> <p>One way to derive a lower bound on the tree height for a given number <code>N</code> of nodes is to work from the other direction: given a tree of height <code>h</code>, what is the maximum number of nodes that can be packed into it?</p> <p>Let's call this function of height <code>maxnodes(h)</code>. Clearly the number of nodes on a binary tree of given height is maximised when the tree is <em>full</em>, i.e. when each internal node has 2 children. Induction will quickly show that <code>maxnodes(h) = 2^h - 1</code>.</p> <p>So, if we have <code>N</code> nodes, every <code>h</code> for which <code>maxnodes(h) &gt;= N</code> is <em>an</em> upper bound for <code>minheight(N)</code>: that is, you could fit all <code>N</code> nodes on a tree of that height. Of all these upper bounds, the best (tightest) one will be the minimum. So what we want to find is the smallest <code>h</code> such that</p> <pre><code>N &lt;= maxnodes(h) = 2^h - 1 </code></pre> <p>So how to find this smallest satisfying value of <code>h</code>?</p> <p>The important property of <code>maxnodes(h)</code> is that it is nondecreasing w.r.t. <code>h</code> (in fact it's strictly increasing, but nondecreasing is sufficient). What that means is that you can never fit more nodes into a full binary tree by reducing its height. (Obvious really but it helps to spell things out sometimes!) This makes rearranging the above equation to find the minimum value of <code>h</code> easy:</p> <pre><code>2^h - 1 &gt;= N 2^h &gt;= N+1 # This is the RHS of your inequality, just flipped around h &gt;= log2(N+1) # This step is only allowed because log(x) is nondecreasing </code></pre> <p><code>h</code> must be integer, so the smallest value of <code>h</code> satisfying <code>h &gt;= log2(N+1)</code> is <code>RoundUp(log2(N+1))</code>.</p> <p>I find this to be the most useful way to describe the lower bound, but it can be used to derive the LHS of the inequality you're asking about. Starting from the 2nd equation in the previous block:</p> <pre><code>2^h &gt;= N+1 </code></pre> <p>The set of <code>h</code> values that satisfy this inequality begins at <code>h = log2(N+1)</code> and stretches out to positive infinity. Since <code>h = log2(N+1)</code> is the minimum satisfying value in this set, anything lower must not satisfy the inequality, so in particular <code>h-1</code> will not satisfy it. If a <code>&gt;=</code> inequality does not hold between two real (non-infinite) numbers then the corresponding <code>&lt;</code> inequality must hold, so:</p> <pre><code>2^(h-1) &lt; N+1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload