I was wondering how does this equation:
$$\frac n2 + \frac n4 + \frac n8 + \dots + 1$$
go to:
$$1 + 2 + 4 + \dots + \frac n2$$
and then to:
$$\sum_{i=0}^{h-1} 2^i,\; \text{where $h$ is the height of the tree}$$
and then the result of: $n-1$
Regards

I was wondering how does this equation:
$$\frac n2 + \frac n4 + \frac n8 + \dots + 1$$
go to:
$$1 + 2 + 4 + \dots + \frac n2$$
and then to:
$$\sum_{i=0}^{h-1} 2^i,\; \text{where $h$ is the height of the tree}$$
and then the result of: $n-1$
Regards

Copyright © 2021 JogjaFile Inc.
Having seen this question two times I'm still not sure what exactly it is about. I suppose that the question is something like this: "Given a full binary tree, how do we calculate the number of internal nodes in it?".
Assume that the height is
hand the number of leaf nodes isn. Then the next level will haven/2nodes, next level -n/4until at the root there is just one node. This gives us the formula:$$\frac n2 + \frac n4 + \frac n8 + \dots + 1$$
The next formula is the same with all the summands written in the reverse order. To make it more clear let's write 3 terms from both ends in both formulas
$$\frac n2 + \frac n4 + \frac n8 + \dots + 4 + 2 + 1$$
$$1 + 2 + 4 + \dots + \frac n8 + \frac n4 + \frac n2$$
The other way to see this formula is to start counting from the root. There is 1 root, then there are 2 children on the 2-nd level, then there are 4 children at the 3-rd level and so on until the last internal level of
n/2nodes. And this logic is exactly what gives us the formula of$$\sum_{i=0}^{h-1} 2^i,\; \text{where $h$ is the height of the tree}$$
The fact that such a sum is equal to
2^h - 1is easy to prove by induction. And2^his the same asn. So this give usn-1.Another simple way to see that the number of internal nodes in a tree with
nleaves is to consider it as a tree representation of knock-out tournament: each leaf is a team and each internal node represents a winner of a match between the two "incoming" nodes. See it like this it is clear that in each "match" one of the teams is eliminated and the other goes on. So if we start withnteams and at the end have just 1 "champion", it means thatn - 1teams got eliminated so there weren - 1"matches" i.e.n - 1internal nodes. (Note: unlike the rest of the answer, this argument works for every tree, not just full binary trees).Update (proof that
sum(2^i) = 2^n-1)Proof by induction works like this:
h = 0:$$\sum_{i=0}^{0} 2^i = 1 = 2 - 1 = 2^1 - 1$$
hthat$$\sum_{i=0}^{h-1} 2^i = 2^h - 1$$
and we want to prove that for
h+1it holds as well that:$$\sum_{i=0}^{h} 2^i = 2^{h+1} - 1$$
It's actually very easy to prove, just take the last term from the new sum and substitute the old sum with the known proposition:
$$\sum_{i=0}^{h} 2^i = \sum_{i=0}^{h-1} 2^i + 2^h = (2^h - 1) + 2^h = 2*2^h-1 = 2^{h+1} - 1$$