Search code examples
data-structurestree

Why is there a big O for the minimum height of a tree?


In my algorithms and data structures course we’re given a proof for the upper bounds of the minimum height of a tree, a general tree, not a binary tree. In the course, the degree of a tree is based upon the largest amount of child nodes found in a parent node that’s defined as d. Secondly, the total number of nodes in the tree is defined as n. The big O for the minimum height of the tree is h <= ceiling(log_d(n)).

I tried to find out what it means by using an example: d = 4, n = 21 The result is h <= 3. What I’m now trying to understand is what the meaning of this result is. My biggest difficulty is just understanding why there is a big O used for a minimum.


Solution

  • The minimum height for a tree with 𝑛 nodes and a maximum degree of 𝑑 is when you pack those 𝑛 nodes with as many as possible in the upper levels of the tree, because you'll want to use as few levels as possible.

    Practically, when you have 𝑛 nodes, you'd use the first as the root, the second batch of 𝑑 nodes as direct children of the root, and so filling up levels of the tree as needed. This is called a complete tree. There is no way to reduce the height in such a tree by moving nodes around. All levels -- except maybe the bottom one -- will already be filled at full capacity.

    So for 𝑛=21 and 𝑑=4 you would build a complete tree as follows:

           __________________1___________________
          /            /           \             \
      ___2__       ___3___       ___4___       ___5___
     /  /\  \     /  / \  \     /  / \  \     /  / \  \
    6  7  8  9   10 11 12 13   14 15 16 17   18 19 20 21
    

    Note how this is a perfect tree: all levels are filled to maximum capacity. Adding even one more node will necessarily increase the tree's height.

    It seems you are using a definition of height that is slightly different from the standard definition: the above pictured tree has height 2, not 3. Height is defined (standard) as the length of the longest path from root to leaf, expressed as the number of edges on that path.

    Now to derive the formula for the minimum height we can observe how a level can contain 𝑑 times more nodes than the previous level (since all nodes on that level can have at most 𝑑 children).

    So we have this maximum capacity of the levels:

    Β  Β  Β  1, 𝑑, 𝑑², 𝑑³, ... π‘‘β„Ž

    The sum of those terms is a geometric series, and is therefore:

    Β  Β  Β  (π‘‘β„Ž+1βˆ’1) / (π‘‘βˆ’1)

    Just to verify, for β„Ž=2 and 𝑑=4 (the example), we then have:

    Β  Β  Β  (42+1βˆ’1) / (4βˆ’1) = (64βˆ’1) / 3 = 21

    For a given height β„Ž, the number of nodes in a complete tree is thus bounded like this:

    Β  Β  Β  (π‘‘β„Žβˆ’1) / (π‘‘βˆ’1) < 𝑛 ≀ (π‘‘β„Ž+1βˆ’1) / (π‘‘βˆ’1)

    Let's move things around to isolate β„Ž:

    Β  Β  Β  π‘‘β„Ž < 𝑛(π‘‘βˆ’1) + 1 ≀ π‘‘β„Ž+1

    If we subtract 1 from all terms, we can change the comparators:

    Β  Β  Β  π‘‘β„Ž ≀ 𝑛(π‘‘βˆ’1) < π‘‘β„Ž+1

    Taking the logarithm with base 𝑑, we get:

    Β  Β  Β  β„Ž ≀ log𝑑(𝑛(π‘‘βˆ’1)) < β„Ž+1

    And so we can now derive the minimum β„Ž by rounding downwards:

    Β  Β  Β  β„Ž = ⌊ log𝑑(𝑛(π‘‘βˆ’1)) βŒ‹

    Verification

    Let's try again with the example 𝑛=21 and 𝑑=4:

    Β  Β  Β  β„Ž = ⌊ log4(21(4βˆ’1)) βŒ‹

    Β  Β  Β  β„Ž = ⌊ log4(63) βŒ‹ = ⌊ 2.9886... βŒ‹ = 2

    Big O

    In Big O notation, the base of the logarithm is not significant, and things like rounding up or down are not relevant either.

    So the big O for the minimum height given 𝑛 and 𝑑 is:

    Β  Β  Β  O(⌊ log𝑑(𝑛(π‘‘βˆ’1)) βŒ‹) = O(log(𝑛𝑑))