Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With your definition of perfectly balanced, all the variation in structure happens at the deepest level of the tree, so you only need to worry about that one level.</p> <p>A maximal balanced tree with height h will have 2^(h-1) leaves - e.g. for height 1, the only leaf is the root. These are all at the deepest level.</p> <p>A minimal balanced tree with height h has only one node at the deepest level.</p> <p>The number of ways you can construct a perfectly balanced binary tree is therefore the same as the number of ways you can have between 1 and 2^(h-1) nodes at the deepest level.</p> <p>There are 2^(h-1) nodes that may or may not be present at that level (a combinations problem, not permutations), so you get 2^(2^(h-1)) possiblilities, of which only one (the "none" case) is invalid.</p> <p>So I think your answer is (2^(2^(h-1)))-1. So if you can determine the correct h...</p> <p>That's assuming a binary <em>search</em> tree (with item values unique and in order), so the binary tree is fully determined by the choice of which deepest-level nodes are present. Otherwise, you multiply that by the number of permutations of the sequence of values.</p> <p>Take care with my definition of h - a zero-height tree would have no nodes at all, and give a nonsense result - sqrt(2)-1 is an irrational answer in at least two senses.</p> <p><strong>EDIT</strong></p> <p>Marcs comment made me think some more. For a particular height I think my answer is right. The problem is that a particular height allows various different numbers of total nodes, because it allows various different numbers of nodes in that deepest layer. So I can't get the correct answer for one particular node count this way. So... lets try again...</p> <p>If our tree has height h, it can have at most (2^h)-1 nodes in total. Excluding the deepest level, it has (2^(h-1))-1 nodes - all of which must be present.</p> <p>We have c-((2^(h-1))-1) nodes at the deepest level, and we get to choose where to put those nodes from the 2^(h-1) possible positions at the deepest level. I use c for count, because I want to define...</p> <pre><code>n = 2^(h-1) k = c-((2^(h-1))-1) </code></pre> <p>answer = <a href="http://en.wikipedia.org/wiki/Binomial_coefficient" rel="nofollow">n choose k</a></p> <p>I still haven't derived h from c - it should be the floor of the base 2 logarithm, but I have that out-by-one-somewhere feeling.</p>
    singulars
    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.
 

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