Does the total number of nodes in a full binary tree have the same Θ value as the total number of nodes in a complete binary tree with the same height?
Full binary tree of height h has a sum of a geometric progression total number of nodes:
1 + 2 + 4 + ... + 2^h = 2^(h+1) - 1 thus :
#Nodes_Full = Θ(2 * 2^h - 1) = Θ(2^h)
Complete tree of height h has at most the same number of nodes as a full binary tree. It has at least number of nodes when it differs from a full bin-tree by at most 2^h - 1 nodes, thus:
#Nodes_Complete = Θ(2^(h+1) - 1 - (2^h -1)) + Θ(2*2^h - 2^h) = Θ(2^h(2-1)) = Θ(2^h)
The answer is yes.
Related
The original question is below.
For any binary tree with n nodes and i internal nodes, the relation between n and i is _____ <= i.
In my opinion
I thought n/2 <= i, but I can't not figure out what condition will let n/2 = i.
I also want to ask that the root node is internal node?
In a full tree there are 1 + 2 + ... + 2^(h-1) internal nodes and 2^h leaf nodes (where h is the height of the tree).
That means the total number of nodes is
n = 1 + 2 + .... + 2^h = 2^(h+1)-1
i = 1 + 2 + ... + 2^h-1 = 2^h - 1
Now, for the relation you are looking for:
n-i = 2^h+1 - 1 - 2^h + 1 = 2^h+1 - 2^h = 2*2^h - 2^h = 2^h
Since 2^h = i+1, you get:
n - i = i+1
n - 1 = 2i
(n - 1)/2 = i
(Note that there is no issue with non integer result, since n is always odd in full tree).
Now, all you have left to show is that full tree is indeed the one with the highest such ratio (this is left for the reader).
How do you prove that a binary heap with n nodes has exactly ⌈n / 2⌉ leaf nodes?
Let x be the height of tree in which case 2^x = no of leaves
=> 2^0 + 2^1+ 2^2 + 2^3 +...2^x = n
=> 2^(x+1) - 1 = n (By sum series power of 2 formula)
=>2^(x+1)= n+1
=> log(n+1) = x+1
=>log(n+1)-1 = x;
=>log(n+1)- log2 =x
x =log(n+1/2)
=> no of leaves = (n+1)/2 (which is 2^(log(n+1/2))
A good intuition for this is to think about this inductively. For the n = 0 case, there aren't any leaves, and for the n = 1 case the root is the only leaf. For each added node after that, it either (1) adds a child to a node that was previously a leaf and now has one child, not changing the number of leaf nodes, or (2) adds a child to a node that already has one child, increasing the number of leaves by one. Using induction, you can formalize this to prove that the number of leaves in a binary heap is ⌈n / 2⌉.
Lets say we have n nodes so
0-level
1-level
...
d-level // Total 2^d nodes at this level. #ofLeafNodes at this level depends on how many nodes are there at below level.
last-level-containing-X-nodes // All leaf nodes
Leaf nodes can come only from last two levels.
Last level have x leaf nodes.
While second last level would have total 2^d nodes and ⌈x / 2⌉ leaf nodes.
So, in total leaf nodes =
= (Leaf nodes at bottom-most level) + (Leaf nodes at second bottom-most level)
= (x + (2^d - ⌈x / 2⌉)) //Equation 1
Total nodes in the binary heap tree=>
n = (2^(d + 1) - 1) + x
2*(2^d) = n-x+1
2^d = (n-x+1)/2
Now substitute 2^d in above eq#1,
(x + (2^d - ⌈x / 2⌉))
=(x + (n-x+1)/2 - ⌈x / 2⌉)
= n/2 + x/2 + 1/2 - ⌈x / 2⌉
For even n, x will be odd:
= n/2 + x/2 + 1/2 - (x/2 + 1/2)
= n/2 + x/2 + 1/2 - x/2 - 1/2
= n/2
= ⌈n / 2⌉ /*Since n is even*/
For odd n, x will be even:
= n/2 + x/2 + 1/2 - (x/2 + 1/2)
= n/2 + x/2 + 1/2 - x/2
= n/2 + 1/2
= ⌈n / 2⌉
To solve the problem, we need to find the last internal node and the first leaf node of the heap.
From the heap property, we know that a node with index i,
Parent[i] = ceil(i/2)
Left[i] = 2i
Right[i] = 2i+1
Assume the last internal node is at floor(n/2), and first leaf node is at floor(n/2)+1.
Right[floor(n/2)]
= 2(floor(n/2)) + 1
<= 2 * (n-(2-1))/2 + 1
= 2 * (n-1)/2 + 1
= n
From Right[ floor(n/2) ] <= n, we know that floor(n/2) will have a right child in bound of the heap, thus, it is the last internal nodes. Also, all nodes before floor(n/2) will be internal nodes that have a right child in bound of the heap. Then, we know that internal nodes are indexed by 1, 2, 3, 4, ... floor(n/2), which means the index of last internal is number of internal nodes in the heap.
Left[floor(n/2) + 1]
= 2 * (floor(n/2) + 1)
> 2 * (n/2 - 1 + 1)
= 2 * n/2
= n
From Left[ floor(n/2)+1] <= n, we know that floor(n/2)+1 will have a left child out of bound of the heap, thus, it is the first leaf node. Also, all nodes after floor(n/2)+1 will have a left child out of bound of the heap. Then, we know that nodes after floor(n/2) (last internal node), in the heap will not have child nodes, so they are all leaves that are indexed by floor(n/2)+1, floor(n/2)+2, floor(n/2)+3, ... n.
Thus, from above we know that:
number of leaves = element of heap - number of internal nodes
= n - floor(n/2)
= ceil(n/2)
I am wondering about two questions that came up when studying about binary search trees. They are the following:
What is the maximum number of nodes in the bottom level of a balanced binary search tree with n nodes?
What is the minimum number of nodes in the bottom level of a balanced binary search tree with n nodes?
I cannot find any formulas in my textbook regarding this. Is there any way to answers these questions? Please let me know.
Using notation:
H = Balanced binary tree height
L = Total number of leaves in a full binary tree of height H
N = Total number of nodes in a full binary tree of height H
The relation is L = (N + 1) / 2 as demonstrated below. That would be the maximum number of leaf nodes for a given tree height H. The minimum number of nodes at a given height is 1 (cannot be zero, because then the tree height would be reduced by one).
Drawing trees with increasing heights, one can observe that:
H = 1, L = 1, N = 1
H = 2, L = 2, N = 3
H = 3, L = 4, N = 7
H = 4, L = 8, N = 15
...
The relation between tree height (H) and the total number of leaves (L)
and the total number of nodes (N) becomes apparent:
L = 2^(H-1)
N = (2^H) - 1
The correctness is easily proven using mathematical induction.
Examples above show that it is true for small H.
Simply put in the value of H (e.g. H=1) and compute L and N.
Assuming the formulas are true for some H, one can show they are also true for HH=H+1:
For L, the assumption is that L=2^(H-1) is true.
As each node has two children, increasing the height by one
is going to replace each leaf node with two new leaves, effectively
doubling the total number of leaves. Therefore, in case of HH=H+1,
the total number of leaves (LL) is going to be doubled:
LL = L * 2
= 2^(H-1) * 2
= 2^(H)
= 2^(HH-1)
For N, the assumption is that N=(2^H)-1 is true.
Increasing the height by one (HH=H+1) increases the total number
of nodes by the total number of added leaf nodes. Therefore,
NN = N + LL
= (2^H) - 1 + 2^(HH-1)
= 2^(HH-1) - 1 + 2^(HH-1)
= 2 * 2^(HH-1) - 1
= (2^HH) - 1
Applying the mathematical induction, the correctness is proven.
H can be expressed in terms of N:
N = (2^H) - 1 // +1 to both sides
N + 1 = 2^H // apply log2 monotone function to both sides
log2(N+1) = log2(2^H)
= H * log2(2)
= H
The direct relation between L and N (which is the answer to the question asked) is:
L = 2^(H - 1) // replace H = log2(N + 1)
= 2^(log2(N + 1) - 1)
= 2^(log2(N + 1) - log2(2))
= 2^(log2( (N + 1) / 2 ))
= (N + 1) / 2
For Big O analysis, the constants are discarded, so the Binary Search Tree lookup time complexity (i.e. H with respect to the input size N) is O(log2(N)). Also, keeping in mind the formula for changing the logarithm base:
log2(N) = log10(N) / log10(2)
and discarding the constant factor 1/log10(2), where instead of 10 one can have an arbitrary logarithm base, the time complexity is simply O(log(N)) regardless of the chosen logarithm base constant.
Assuming that it's a full binary tree, the number of nodes in the leaf will always be equal to (n/2)+1.
For the minimum number of nodes, the total number of nodes could be 1 (satisfying the condition that it should be a balanced tree).
I got the answers from my professor.
1) Maximum number of nodes at the last level: ⌈n/2⌉
If there is a balanced binary search tree with 7 nodes, then the answer would be ⌈7/2⌉ = 4 and for a tree with 15 nodes, the answer would be ⌈15/2⌉ = 8.
But what is troubling is the fact that this formula gives the right answer only when the last level of a balanced tree is completely filled from left to right.
For example, a balanced binary search tree with 5 nodes, the above formula gives an answer of 3 which is not true because a tree with 5 nodes can contain a maximum nodes of 4 nodes at the last level. So I am guessing he meant full balanced binary search tree.
2) Minimum number of nodes at the last level: 1
The maximum number of nodes at level L in a binary tree is 2^L (if you assume that the vertex is level 0). This is easy to see because at each level you spawn 2 children from each previous leaf. The fact that it is balanced/search tree is irrelevant. So you have to find the biggest L such that 2^L < n and subtract it from n. Which in math language is:
The minimum number of nodes depends on the way you balance your tree. There can be height-balanced trees, weight-balanced trees and I assume other balanced trees. Even with height balanced trees you can define what do you mean by a balanced tree. Because technically a tree of 2^N nodes that has a hight of N + 2 is still a balanced tree.
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.
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.