Table of contents
When learning trees in data structures, you will encounter the following two conclusions:
① The maximum number of nodes of a fork tree with height is h
( m
mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① (m^h-1)/(m-1)... ..........①(mh−1)/(m−1)............①
② The minimum height of n
a fork tree with nodes m
is ⌈ logm ( n ( m − 1 ) + 1 ) ⌉ . . . . . . . . . . . ② \left \lceil log_m(n(m-1) +1) \right \rceil .........②⌈logm(n(m−1)+1)⌉............②
Many beginners don't know how to deduce it, and it takes time to memorize it.
This article will deduce the formula in detail.
The maximum number of nodes in an m-ary tree with height h
From the conclusion, the degree is mmm 's tree, theiiThere are at mostmi − 1 m^{i-1} on layer imi − 1 nodes
So the following table can be obtained:
high | Total number of nodes |
---|---|
1 | 1 1 1 |
2 | 1 + m 1+m 1+m |
3 | 1 + m + m 2 1+m+m^2 1+m+m2 |
4 | 1 + m + m 2 + m 3 1+m+m^2+m^3 1+m+m2+m3 |
5 | 1 + m + m 2 + m 3 + m 4 1+m+m^2+m^3+m^4 1+m+m2+m3+m4 |
··· | ⋅ ⋅ ⋅ ··· ⋅⋅⋅ |
i | 1 + m + m 2 + ⋅ ⋅ ⋅ + m i − 1 1+m+m^2+···+m^{i-1} 1+m+m2+⋅⋅⋅+mi−1 |
Now, assuming that the maximum number of nodes required for an m-ary tree with height is h
, thenn
combining
the above table, we can get:
n = 1 + m + m 2 + ⋅ ⋅ ⋅ + mh − 1 n=1+m+m^2+ ···+m^{h-1}n=1+m+m2+⋅⋅⋅+mh − 1
is summed bythe geometric sequence formula
S = a 1 1 − qn 1 − q S = a_1\frac{1 - q^n}{1 - q}S=a11−q1−qn
Among them, a1
is the first item, q
is the common ratio, n
is the number of items, and S
is the sum of the first n items of the geometric sequence.
So the original formula can be transformed into
n = ( m h − 1 ) / ( m − 1 ) n=(m^h-1)/(m-1) n=(mh−1)/(m−1 )
Since n must be an integer,n ≤ ( mh − 1 ) / ( m − 1 ) n≤(m^h-1)/(m-1)n≤(mh−1)/(m−1) ,
Right nowThe maximum number of nodes in an m-ary tree with height his
( mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① (m^h-1)/(m-1).......... ①(mh−1)/(m−1)............①
The minimum height of an m-ary tree with n nodes
Now proceed to deduce the minimum height of n
a fork tree with nodes .m
First of all, from the ① formula just introduced above
n = ( mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① n=(m^h-1)/(m-1) ..........①n=(mh−1)/(m−1 ) .........①The expression
abouth
n ( m − 1 ) = m h − 1 n(m-1)=m^h-1 n(m−1)=mh−1 m h = n ( m − 1 ) + 1 m^h=n(m-1)+1 mh=n(m−1)+1 l o g m m h = l o g m ( n ( m − 1 ) + 1 ) log_mm^h=log_m(n(m-1)+1) logmmh=logm(n(m−1)+1) h = l o g m ( n ( m − 1 ) + 1 ) h=log_m(n(m-1)+1) h=logm(n(m−1)+1)
Since h can only be a positive integer, the original formula is rounded up. final expression
Right nowThe minimum height of an m-ary tree with n nodes:
h = ⌈ l o g m ( n ( m − 1 ) + 1 ) ⌉ . . . . . . . . . . . . ② h=\left \lceil log_m(n(m-1)+1) \right \rceil............② h=⌈logm(n(m−1)+1)⌉...... ②In
summary, this article deduces the following two formulas:
①The maximum number of nodesh
ofm
( mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① (m^h-1)/(m-1)..........①(mh−1)/(m−1)............①
② The minimum height of n
a fork tree with nodes m
is ⌈ logm ( n ( m − 1 ) + 1 ) ⌉ . . . . . . . . . . . ② \left \lceil log_m(n(m-1) +1) \right \rceil .........②⌈logm(n(m−1)+1)⌉...... ② Ifplease give it
a thumbs up so that more people can seeit
Thanks♪(・ω・)ノ!
Comes with a wallpaper: