k-ary Trees Induction Proof - algorithm

I'm getting stumped by this problem:
You have a tree with every internal node having k children, with k >= 2.
What is the maximum number of nodes that such a tree can have, if its depth is d? Prove your
answer by induction on d.
So I realize that if k was 2, the geometric series would be 1 + 2 + 4 + 8...+2^n, but I can't figure out how to include depth and how to prove it inductively.

The number of items in a full k-ary tree of n levels is (k^n - 1)/(k - 1).
A binary tree of 5 levels, for example, has 31 nodes (1 + 2 + 4 + 8 + 16). Or:
(2^5 - 1)/(2 - 1) = 31/1 = 31
A 4-ary tree of 4 levels has 85 nodes (1 + 4 + 16 + 64)
(4^4 - 1)/(4 - 1) = 256/3 = 85
If you write out a few of those for different values of k, you should be able to derive the inductive proof.

Related

Intuitive derivation of formulas for finding the i-th child of a node in a traversal-numbered complete tree

In my answer to this question, I used two formulas that I arrived at by ad-hoc means, and I am at a loss for a simple explanation for why these formulas work. Here is the problem in full:
Consider a perfect or complete K-ary tree of height H where every node is labeled by their rank in a breadth-first traversal, and the its dual where every node is labeled in depth-first order. Here is an example with K=2, H=2:
_ 0 _ _ 0 _
/ \ / \
1 2 1 4
/ \ / \ / \ / \
3 4 5 6 2 3 5 6
For the BF-ordered tree, the i-th child of a node N at depth D is given by:
K*N + 1 + i
For the DF-ordered tree, the i-th child of a node N at depth D is given by:
N + 1 + i*step, where step = (K^(H - D) - 1) / (K - 1)
What is an intuitive explanation for these formulas?
The BF-ordered formula makes sense to me when looking at any hand-drawn example, but I can't put into words why it works. In the DF-ordered case, the best I can come up with is this:
For a node N at depth D in a DFS-numbered K-ary tree of height H, its first child is simply N+1 because it is the next node to be visited in a depth-first traversal. The second child of N will be visited directly after visiting the entire sub-tree rooted at the first child (N+1), which is itself a complete K-ary tree of height H - (D + 1). The size of any complete, K-ary tree is given by the sum of a finite geometric series as explained here. The size of said sub-tree is the distance between the first and second children, and, in fact, it is the same distance between all siblings since each of their sub-trees are the same size. If we call this distance step, then:
1st child is N + 1
2nd child is N + 1 + step
3rd child is N + 1 + step + step
...and so on.
Can anyone provide a better explanation for how or why these formulas work?
For the BFS:
If node N is at depth D and there is a nodes before N at depth D (and b nodes after):
N = K^0 + K^1 + ... + K^(D-1) + a
How many nodes will be labeled before its first child? There is b remaining nodes at depth D and a * K "child" nodes at depth D+1 that will come before. So if C is the label of the first child of N:
C = N + b + a * K + 1
C = K^0 + K^1 + ... + K^(D-1) + a + b + a * K + 1
C = K^0 + K^1 + ... + K^(D-1) + K^D + a * K
Indeed there is K^D nodes at depth D so a + b + 1 = K^D, therefor:
C = 1 + (K^0 + ... + K^(D-2) + K^(D-1) + a )* K
C = 1 + N*k
For the DFS:
To compute the size of the step you have to compute the size of the remaining sub-tree, and like a sub-tree of a perfect K-ary tree is itself a perfect K-ary tree, you can compute its number of nodes.

What is the minimal depth of a 4-ary tree with n nodes?

The question is:
What is the minimal depth of a 4-ary tree with n nodes?
I can't find the correct log that is the answer, I know that for n = 1 the depth is 0, if 2 <= n <= 5 it is 1, if 6 <= n <= 21 it is 2
Thanks in advance!
That is a math question.
Lets find the relation f between the height h and the number of nodes n in a full tree. I'll to it with recursion.
n = f(h). The base is easy, as you said: f(0)=1.
We can see that each level contains exactly 4^i nodes, where i is the distance from the root. So, after summarizing all levels we have:
f(h) = 4^h + f(h-1) = 4^h + 4^(h-1) + ... 4^1 + 4^0 = (4^(h+1)-1)/3 =n [sum of geometric series]
Isolating h:
h = log_4(3n+1) - 1 and you should take the ceil() of that, because you want it to apply on non-full trees as well.
Generalization for k-ary is easy now, as:
f_k(h) = (k^(h+1)-1)/(k-1), so h = ceil(log_k((k-1)n + 1) - 1)

Worst case in Max-Heapify - How do you get 2n/3?

In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY,
The children’s subtrees each have size at most 2n/3—the worst case
occurs when the bottom level of the tree is exactly half full.
I understand why it is worst when the bottom level of the tree is exactly half full. And it is also answered in this question worst case in MAX-HEAPIFY: "the worst case occurs when the bottom level of the tree is exactly half full"
My question is how to get 2n/3?
Why if the bottom level is half full, then the size of the child tree is up to 2n/3?
How to calculate that?
Thanks
In a tree where each node has exactly either 0 or 2 children, the number of nodes with 0 children is one more than the number of nodes with 2 children.{Explanation: number of nodes at height h is 2^h, which by the summation formula of a geometric series equals (sum of nodes from height 0 to h-1) + 1; and all the nodes from height 0 to h-1 are the nodes with exactly 2 children}
ROOT
L R
/ \ / \
/ \ / \
----- -----
*****
Let k be the number of nodes in R. The number of nodes in L is k + (k + 1) = 2k + 1. The total number of nodes is n = 1 + (2k + 1) + k = 3k + 2 (root plus L plus R). The ratio is (2k + 1)/(3k + 2), which is bounded above by 2/3. No constant less than 2/3 works, because the limit as k goes to infinity is 2/3.
Understand the maximum number of elements in a subtree happens for the left subtree of a tree that has the last level half full.Draw this on a piece of paper to realize this.
Once that is clear, the bound of 2N/3 is easy to get.
Let us assume that the total number of nodes in the tree is N.
Number of nodes in the tree = 1 + (Number of nodes in Left Subtree) + (Number of nodes in Right Subtree)
For our case where the tree has last level half full, iF we assume that the right subtree is of height h, then the left subtree if of height (h+1):
Number of nodes in Left Subtree =1+2+4+8....2^(h+1)=2^(h+2)-1 .....(i)
Number of nodes in Right Subtree =1+2+4+8....2^(h) =2^(h+1)-1 .....(ii)
Thus, plugging into:
Number of nodes in the tree = 1 + (Number of nodes in Left Subtree) + (Number of nodes in Right Subtree)
=> N = 1 + (2^(h+2)-1) + (2^(h+1)-1)
=> N = 1 + 3*(2^(h+1)) - 2
=> N = 3*(2^(h+1)) -1
=> 2^(h+1) = (N + 1)/3
Plugging in this value into equation (i), we get:
Number of nodes in Left Subtree = 2^(h+2)-1 = 2*(N+1)/3 -1 =(2N-1)/3 < (2N/3)
Hence the upper bound on the maximum number of nodes in a subtree for a tree with N nodes is 2N/3.
For a complete binary tree of height h, number of nodes is f(h) = 2^h - 1. In above case we have nearly complete binary tree with bottom half full. We can visualize this as collection of root + left complete tree + right complete tree. If height of original tree is h, then height of left is h - 1 and right is h - 2. So equation becomes
n = 1 + f(h-1) + f(h-2) (1)
We want to solve above for f(h-1) expressed as in terms of n
f(h-2) = 2^(h-2) - 1 = (2^(h-1)-1+1)/2 - 1 = (f(h-1) - 1)/2 (2)
Using above in (1) we have
n = 1 + f(h-1) + (f(h-1) - 1)/2 = 1/2 + 3*f(h-1)/2
=> f(h-1) = 2*(n-1/2)/3
Hence O(2n/3)
To add to swen's answer. How (2k + 1) / (3k + 2) tends to 2 / 3, when k tends to infinity,
Lim_(k -> inf) (2k + 1) / (3k + 2) = Lim_(k -> inf) k(2 + 1 / k) / k(3 + 2 / k) = Lim_(k -> inf) (2 + 1 / k) / (3 + 2 / k)
apply the limit, and you get 2/3
Number of nodes at -
level 0 i.e. root is 2^0
level 1 is 2^1
level 2 is 2^2
...
level n is 2^n
Summation of all nodes from level 0 up to level n,
S = 2^0 + 2^1 + 2^2 + ... + 2^n
From geometric series summation rule we know that
x^0 + x^1 + x^2 + ... + x^(n) = (x^(n+1) - 1)/(x-1)
Substituting x = 2, we get
S = 2^(n+1) - 1. i.e. 2^(n+1) = S + 1
As 2^(n+1) is the total nodes at level n+1, we can say that the number of nodes with 0 children is one more than the number of nodes with 2 children.
Now lets calculate number of nodes in left subtree, right tree and total ..
Assume that number of non-leaf nodes in the left subtree of root = k.
By the above reasoning, number of leaf nodes in the left subtree or
root = k + 1.
Number of non-leaf nodes in the right subtree of root = k as the tree is said to be exactly half full.
Total number of nodes in the left subtree of root = k + k + 1 = 2k +
Total number of nodes in the tree, n = (2k + 1) + k + 1 = 3k + 2.
Ratio of nodes in the left subtree and total nodes = (2k + 1) / (3k +
2) which is bounded above by 2/3.
That's the reason of saying that the children’s subtrees each have size at most 2n/3.

Ratio of leaves to total nodes in a Fibonacci call stack

If you were to look at a recursive implementation of calculating the nth Fibonacci number (root 100, children 99 and 98, grandchildren 98, 97, 97, and 96, etc. etc.), roughly what would be the ratio of the number of leaves to the total number of nodes in the recursive tree?
100
/ \
98 97
/ \ .
96 97 .
. . .
. .
Not homework, just academically curious about this. (And yes, I realize that a recursive implementation is a god-awful way to calculate Fibonacci numbers)
The number of leaves is simply F(n) since the F(i) is simply the number of leaves beneath that node. Do you see why? (hint: use induction)
The number of non-leaf nodes is the number of leaf nodes-1. This is a property of binary trees. So the total number of nodes is F(n) + F(n)-1 = 2F(n)-1.
The ratio thus approaches 1/2 as n grows large.
fib(x) consist of leaves fib(x-1) and leaves of fib(x-2). So you get the same recursive equation as you have for fibonacci numbers.
If the termination point (leaves) are Fib1 and Fib0, then
tree numofleaves
fib2 2
fib3 3
fib4 5
fib5 8
fib6 13
...
and numofleaves(x) = fib(x+1).
For the number of nodes you get the equation numnodes(x) = 1 + numnodes(x-1) + numnodes(x-2).
I actually worked out a proof by induction that shows that the number of leaves in a Fibonacci tree always exceeds the number of internal nodes.
Proof: Let E(n) be the # of leaves of fibonacci tree for input n,
and M(n) be the # of internal nodes of fibonacci tree for input n
E(n) >= M(n) + 1
Base cases:
f(0): E(0) = 1, M(0) = 0
f(1): E(1) = 1, M(1) = 1
f(2): E(2) = 2, M(2) = 1
f(3): E(3) = 3, M(3) = 2
The leaves of a tree of size n is equal to the leaves
of each sub-tree:
E(n) = E(n - 1) + E(n - 2)
The internal nodes of a tree of size n is equal to the internal nodes of each
sub-tree, plus the root
M(n) = M(n - 1) + M(n - 2) + 1
E(n) >= [M(n - 1) + 1] + [M(n - 2) + 1], (by the Inductive Hypothesis)
So, E(n) = M(n - 1) + M(n - 2) + 2
So, E(n) >= M(n) + 1,
QED

Finding the no of leaf nodes

An N-ary tree has N sub-nodes for each node. If the tree has M non-leaf nodes, How to find the no of leaf nodes?
First of all if the root is level 0, then the K-th level of the tree will have N^K nodes. You can start incrementing a counter level by level until you get M nodes. This way you will find how many levels is the tree consisting of. And the number of leaf nodes is the number of nodes on the last level - it is N^lastLevel.
Here is an example: N = 3, M = 4.
First level = 3^0 = 1
Second level = 3^1 = 3
1 + 3 = 4
So we found that the tree has two levels(counting from 0).
The answer is 3^2 = 9.
Note: You can find the level number also directly, by noticing that M is a sum of geometric progression: 1 + 3 + 9 + 27 ... = M
Hope it is clear.
Mathematically speaking the nodes increase in the geometric progression.
0th level - 1
1st level - n
2nd level - n ^2
3rd level - n ^ 3
....
mth level - n ^ m
So the total number of nodes at m-1st level is 1 + n + n^2 + .. + n ^ m-1.
Now there is a good formula to calculate 1 + a + a^2 + a^3 + ... + a^m , which is
(1 - n^(m+1))/(1-n), lets call this quantity K.
Now what we need is the number of leaf nodes which is n ^ m, and what we have is K. i.e. total number of non-leaf nodes. Doing some mathematical formula adjustment you will find that
n ^ m = K *(n-1) + 1.
e.g. Lets say in 3-ary tree the total number of non-leaf nodes are 40, then using this formula you get the total number of leaf-nodes as 81 which is the right answer.

Resources