7

A BST is generated (by successive insertion of nodes) from each permutation of keys from the set {1,2,3,4,5,6,7}. How many permutations determine trees of height two?

I been stuck on this simple question for quite some time. Any hints anyone.

By the way the answer is 80.

zw324
  • 26,764
  • 16
  • 85
  • 118
user2473033
  • 93
  • 2
  • 6
  • OK, the only question you asked has the answer at the end. What is your real question? *How* to get that answer? – vcsjones Jun 14 '13 at 23:40
  • Just wanted to know how to get to the posted answer, I can see it now thanks to the posted comments. – user2473033 Jun 15 '13 at 00:59

5 Answers5

5

Consider how the tree would be height 2?

-It needs to have 4 as root, 2 as the left child, 6 right child, etc.

How come 4 is the root?

-It needs to be the first inserted. So we have one number now, 6 still can move around in the permutation.

And?

-After the first insert there are still 6 places left, 3 for the left and 3 for the right subtrees. That's 6 choose 3 = 20 choices.

Now what?

-For the left and right subtrees, their roots need to be inserted first, then the children's order does not affect the tree - 2, 1, 3 and 2, 3, 1 gives the same tree. That's 2 for each subtree, and 2 * 2 = 4 for the left and right subtrees.

So?

In conclusion: C(6, 3) * 2 * 2 = 20 * 2 * 2 = 80.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • 1
    Why did we do C(6,3) since only {1,2,3} can form left subtree and {5,,6,7} can form right subtree? We don't have a choice. Please explain. – Gaurang Singhal Oct 16 '17 at 08:41
3

Note that there is only one possible shape for this tree - it has to be perfectly balanced. It therefore has to be this tree:

         4
       /   \
      2     6
     / \   / \
    1   3 5   7

This requires 4 to be inserted first. After that, the insertions need to build up the subtrees holding 1, 2, 3 and 5, 6, 7 in the proper order. This means that we will need to insert 2 before 1 and 3 and need to insert 6 before 5 and 7. It doesn't matter what relative order we insert 1 and 3 in, as long as they're after the 2, and similarly it doesn't matter what relative order we put 5 and 7 in as long as they're after 6. You can therefore think of what we need to insert as 2 X X and 6 Y Y, where the X's are the children of 2 and the Y's are the children of 6. We can then find all possible ways to get back the above tree by finding all interleaves of the sequences 2 X X and 6 Y Y, then multiplying by four (the number of ways of assigning X and Y the values 1, 3, 5, and 7).

So how many ways are there to interleave? Well, you can think of this as the number of ways to permute the sequence L L L R R R, since each permutation of L L L R R R tells us how to choose from either the Left sequence or the Right sequence. There are 6! / 3! 3! = 20 ways to do this. Since each of those twenty interleaves gives four possible insertion sequences, there end up being a total of 20 × 4 = 80 possible ways to do this.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

I've created a table for the number of permutations possible with 1 - 12 elements, with heights up to 12, and included the per-root break down for anybody trying to check that their manual process (described in other answers) is matching with the actual values.

http://www.asmatteringofit.com/blog/2014/6/14/permutations-of-a-binary-search-tree-of-height-x

1

Here is a C++ code aiding the accepted answer, here I haven't shown the obvious ncr(i,j) function, hope someone will find it useful.


int solve(int n, int h) {
    if (n <= 1)
        return (h == 0);

    int ans = 0;

    for (int i = 0; i < n; i++) {
        int res = 0;
        for (int j = 0; j < h - 1; j++) {
            res = res + solve(i, j) * solve(n - i - 1, h - 1);
            res = res + solve(n - i - 1, j) * solve(i, h - 1);
        }
        res = res + solve(i, h - 1) * solve(n - i - 1, h - 1);
        ans = ans + ncr(n - 1, i) * res;
    }
    return ans
}

0

The tree must have 4 as the root and 2 and 6 as the left and right child, respectively. There is only one choice for the root and the insertion should start with 4, however, once we insert the root, there are many insertion orders. There are 2 choices for, the second insertion 2 or 6. If we choose 2 for the second insertion, we have three cases to choose 6: choose 6 for the third insertion, 4, 2, 6, -, -, -, - there are 4!=24 choices for the rest of the insertions; fix 6 for the fourth insertion, 4, 2, -, 6, -,-,- there are 2 choices for the third insertion, 1 or 3, and 3! choices for the rest, so 2*3!=12, and the last case is to fix 6 in the fifth insertion, 4, 2, -, -, 6, -, - there are 2 choices for the third and fourth insertion ((1 and 3), or (3 and 1)) as well as for the last two insertions ((5 and 7) or (7 and 5)), so there are 4 choices. In total, if 2 is the second insertion we have 24+12+4=40 choices for the rest of the insertions. Similarly, there are 40 choices if the second insertion is 6, so the total number of different insertion orders is 80.