Data structure - the minimum height of an m-ary tree with a height of h to a multi-node/n-node m-ary tree (formula derivation)


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)... ..........①(mh1)/(m1)............①

The minimum height of na fork tree with nodes mis ⌈ logm ( n ( m − 1 ) + 1 ) ⌉ . . . . . . . . . . . ② \left \lceil log_m(n(m-1) +1) \right \rceil .........②logm(n(m1)+1)............②

Many beginners don't know how to deduce it, and it takes time to memorize it. /kk

This article will deduce the formula in detail.

insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here

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+⋅⋅⋅+mi1

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=a11q1qn
Among them, a1is the first item, qis the common ratio, nis the number of items, and Sis 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=(mh1)/(m1 )
Since n must be an integer,n ≤ ( mh − 1 ) / ( m − 1 ) n≤(m^h-1)/(m-1)n(mh1)/(m1)

Right nowThe maximum number of nodes in an m-ary tree with height his
( mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① (m^h-1)/(m-1).......... ①(mh1)/(m1)............①

insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here


The minimum height of an m-ary tree with n nodes

Now proceed to deduce the minimum height of na 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=(mh1)/(m1 ) .........①The expression
abouth

n ( m − 1 ) = m h − 1 n(m-1)=m^h-1 n(m1)=mh1 m h = n ( m − 1 ) + 1 m^h=n(m-1)+1 mh=n(m1)+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(m1)+1) h = l o g m ( n ( m − 1 ) + 1 ) h=log_m(n(m-1)+1) h=logm(n(m1)+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(m1)+1)...... ②In



insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here
summary, this article deduces the following two formulas:
The maximum number of nodeshofm
( mh − 1 ) / ( m − 1 ) . . . . . . . . . . . ① (m^h-1)/(m-1)..........①(mh1)/(m1)............①

The minimum height of na fork tree with nodes mis ⌈ logm ( n ( m − 1 ) + 1 ) ⌉ . . . . . . . . . . . ② \left \lceil log_m(n(m-1) +1) \right \rceil .........②logm(n(m1)+1)...... ② Ifplease give it


a thumbs up so that more people can seeit
Thanks♪(・ω・)ノ!

Comes with a wallpaper:
insert image description here

Guess you like

Origin blog.csdn.net/m0_56494923/article/details/129698936