Binary Tree Height

后端 未结 8 2029
迷失自我
迷失自我 2020-12-29 12:34

I need a general formula to calculate the minimum height of the binary tree and the maximum height of the binary tree. (not the binary search tree)

相关标签:
8条回答
  • 2020-12-29 13:14

    The max height is n and the min height (IE a perfect binary tree) is the (log base 2( n + 1)) - 1

    0 讨论(0)
  • 2020-12-29 13:18

    First there may be some difference as to how computer science calculates the height of a tree, versus the way height is determined in discrete mathematics (graph theory), this may be due to the existence of data at any one node (or vertice), while in mathematics, its a pure theory approach.

    So maybe its best you clarify which one you need.

    In discrete mathematics, trees are classified as m-ary trees, so a bin-ary tree is a 2-ary tree. Also at any given height, there can be at most 2^h = L (leaves). This is important to notice, since it confirms that the root is at height zero, hence 2^0 = 1 leaf...1 vertice...the root.

    So given n vertices, the height of a tree is given by the formula n = 2^( h + 1 ) - 1

    Since you are looking for h, you have to take the log2 of both sides of the formula n = 2^( h + 1 ) - 1

    For a full binary tree, the max height is log2( n + 1 ) = log2( 2^( h + 1 ) ) this equals ceiling( log2( n + 1 ) - 1 ) = h

    For a non-full binary tree, the max height = ( n - 1 ) therefore if you have n vertices, the root must be subtracted to get the max height, because of the previous formula above (2^h = L)

    For min heights, extrapolate from the above rules.

    0 讨论(0)
  • 2020-12-29 13:23

    If you have N elements, the minimum height of a binary tree will be log2(N)+1.

    For a full binary tree, the maximum height will be N/2.

    For a non-full binary tree, the maximum height will be N.

    0 讨论(0)
  • 2020-12-29 13:23

    With n-nodes the possible maximum height is floor(log(n)) = ceil (log(n+1))-1.

    With n-nodes the possible minimum height is n-1.

    0 讨论(0)
  • 2020-12-29 13:35

    Think about how the structure of the tree can change.

    For example, if the tree is completely unbalanced then this is the worst case - each node will have exactly one child. In the best case, the tree is completed balanced, and each node has two children.

    Since it sounds like homework, I'll leave it there.

    0 讨论(0)
  • 2020-12-29 13:37

    If a root can have any number of leaves up to 2 (0,1,2) then:

    • The max height is n-1. This is the case when your tree has only one leaf. No node has more than one branch.
    • The min height is [log2(n)] where [x] is the integer part of x.

    In order to obtain a minimal height every root must have as many branches as possible. In this case you'll notice that for n=1, height=0 ; for n=2 to n=3, height=1; for n=4 to n=7, height=2 ; for n=8 to n=15, height=3 etc.

    You can thus notice that, for every n, there exists a p such that:

    2^p <= n < 2^(p+1) and p=height, so height = [log2(n)]

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