Binary tree height

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

+6
source share
8 answers

Firstly, there may be some difference in how computing calculates the height of the tree compared to how the height is determined in discrete mathematics (graph theory), this may be due to the existence of data at any node (or vertex), while in mathematics it is a method of pure theory.

So, perhaps best of all, you will specify which one you need.

m- , - - 2- . 2 ^ h = L (). , , , 2 ^ 0 = 1 ... 1 ... .

, n n = 2 ^ (h + 1) - 1

h, log2 n = 2 ^ (h + 1) - 1

log2 (n + 1) = log2 (2 ^ (h + 1)) (log2 (n + 1) - 1) = h

= (n - 1) , n , , - (2 ^ h = L)

.

+12

N - .
H - .

:
H N :

2^H <= N <= (2^(H+1) - 1)

, ; :

H <= lg(N)  and  H >= (lg(N+1) - 1)

, , :

H = floor( lg(N) ) = ceil( (lg(N+1) - 1) )   //as H is integer

(lg: log base 2)

( ):

Max height = N;  

Min Height = floor( lg(N) ) = ceil( (lg(N+1) - 1) )

, .

+4

N , log2 (N) +1.

N/2.

N.

+3

2 (0,1,2), :

  • n-1. , . node .
  • [log2 (n)], [x] - x.

, . , n = 1 = 0; n = 2 - n = 3, height = 1; n = 4 n = 7, height = 2; n = 8 n = 15, height = 3 ..

, , n p, :

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

+2

, .

, , - node . , node .

, .

+1

n, (IE - ) - ( 2 (n + 1)) - 1

0

h = (log (n + 1)/log (2) -1) .

0

n-nodes floor(log(n))= ceil (log(n+1))-1.

n-nodes n-1.

-1

All Articles