Search code examples
data-structurestreeheightchildrenminimum

How to find minimum possible height of tree?


The fan-out of a node in a tree is defined to be the number of children the node has. The fan-out of a tree is defined to be the maximum fan-out of any node in the tree. Assume tree T has n nodes and a fan-out f > 1. What is the minimum possible height of T?

I have no idea how to begin this problem. I solved the first part which is to find the maximum number of nodes that can be T in terms of height h and fan-out f > 1. I got (f^(h+1) -1)/(f-1). I'm thinking you can use this to solve for the question above. Can someone please point me in the correct direction?

Thank you!


Solution

  • I would approach this problem by turning it around and trying to find the maximum number of nodes you can pack in a tree with a given height and fan-out T_max(h,f). This way, every other tree T(h,f) is guaranteed to have as much, or less nodes than T_max(h,f). Therefore, if you find such T_max(h,f), that

    total_nodes( T_max(h,f) ) > n > total_nodes( T_max(h-1,f))
    

    h will be guaranteed to be the minimum height of a tree with n nodes and f fan-out.

    In order to find such a tree, you need to maximize the number of nodes in every layer of a tree. In other words, every node of such a tree needs to have fan-out of f, no less. Therefore you start inserting nodes in a tree, one level at a time. After a layer is full, you start adding another layer. After n nodes are inserted in such a tree, you stop and check the height of the tree. This will be the minimal height you are looking for.

    Or, you can do a calculation instead:

    nodes_in_level(1) = 1
    nodes_in_level(2) = f
    nodes_in_level(3) = f * f
    ...
    nodes_in_level(x) = f ^ (x - 1)
    

    This is a standard geometric progression. The maximum nodes of a given tree with height x and fan-out f is therefore the sum of such geometric progression and it shouldn't be too much trouble to figure out the smallest x, for which the number of nodes is greater than n.