The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

前端 未结 3 876
感情败类
感情败类 2020-12-02 17:35

This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond

相关标签:
3条回答
  • 2020-12-02 18:01

    Since there are four proofs in the wikipedia article you referenced, it seems you aren't looking for a mathematical explanation for the correspondence between the Catalan numbers and the permutations of a binary tree.

    So instead, here are two ways to try and intuitively visualise how the Catalan sequence (1, 2, 5, 14, 42, ...) arises in combinatorial systems.

    Dicing polygons into triangles

    For a polygon of side N, how many ways can you draw cuts between the vertices that chop the polygon up entirely into triangles?

    • Triangle (N=3): 1 (It's already a triangle)
    • Square (N=4): 2 (Can slice at either diagonal)
    • Pentagon (N=5): 5 (Two slicing lines emanating from a vertex. Five vertices to choose from)
    • Hexagon (N=6): 14 (Try drawing it)
    • ...and so on.

    Drawing a path through a grid without crossing the diagonal

    In this case, the number of unique paths is the Catalan number.

    2x2 grid => 2 paths

      _|       |
    _|       __|
    

    3x3 grid => 5 paths

        _|       |       _|         |         |
      _|      _ _|      |          _|         |
    _|      _|       _ _|      _ _|      _ _ _|
    

    4x4 grid => 14 paths
    5x5 grid => 42 paths

    and so on.

    If you try drawing the possible binary trees for a given N, you will see that the way the tree permutes is just the same.

    Your desire not to just blindly accept the correspondence between the tree and the sequence is admirable. Unfortunately, it's difficult to go much further with this discussion (and explain why the Catalan formula 'happens to be' the way it is) without invoking binomial mathematics. The Wikipedia discussion of binomial coefficients is a good starting point if you want to understand combinatorics (which includes permutation counting) itself in more depth.

    0 讨论(0)
  • 2020-12-02 18:02

    Well here is the recursive solution to count the trees...

    int countTrees(int numkeys){
    
    if(numkeys > 1){
        int i =1;
        int sum=0;
    
        for(i = 1; i <= numkeys; i++){
    
            int lcount = countTrees(i-1);
            int rcount = countTrees(numkeys-i);
            sum += lcount*rcount;
        }
        return(sum);
    }else
        return(1);
    }
    
    0 讨论(0)
  • 2020-12-02 18:06

    catalan http://www.nohre.se/publicImages/catalan.png

    Any binary search tree can be encoded by visiting all nodes pre-order and encode a 1 for every parent and a 0 for every leaf. If the tree has n parents it will have n+1 leafs and consequently the binary code will have n 1:s and (n+1) 0:s. Moreover, and any prefix of the code will have at least as many 1:s as it has 0:s. Therefore, the number of possible trees equals the number of paths below the diagonal.

    0 讨论(0)
提交回复
热议问题