| Name | : | Ashutosh Raghuwanshi | |
| Enrollment Number | : | 031795362 (MCA-1 Batch-B) | |
| Course Code | : | CS-04 | |
| Course Title | : | Data Structures through 'C' and 'PASCAL' | |
| Assignment Number | : | MCA(1)-04/Project/03 |
| Question 1: |
Let T be a binary tree with height h and n nodes. Show thatlog(n+1)-1<=h<=(n-1)For which values of n and h can the above lower upper bounds of h be attained with equality? |
NOTE - In the above question the portion that has been cut is misprinted in the given assignment. We will also prove that the missprinted portion is wrong by proving the rest correct.
Before going staright to the equations, let me define the binary tree presizely but perfactly. Binary tree is eather empty or consist of a node called root together with at most two subtrees called the left subtree and the right subtree of the root. According to this definition a binary tree may have less then or equals to two nodes attached to it's root. Let's make table of the maximum nodes possible at a perticular height of the binary tree.
| Height (h) | (N) |
|---|---|
| 0 | 1 (1) |
| 1 | 3 (1+2) |
| 2 | 7 (1+2+4) |
| 3 | 15 (1+2+4+8) |
| ... | ... |
| h | N (20+21+22+23+...+2h) |
Or we can say that for a tree of height h the maximum number of nodes:
| i = h | 2i | Equation - (1) | ||
| Σ | ||||
| i = 0 |
This is the sumation of a Geomatric Progration and we know that sumation of T terms of a GP is:
|
|
|
After comparing (1 + 2 + 22 + 23 +...+ 2h)(a + ar + ar2 + ar3 +...+ arT-1)
| i = h | 2i | = |
2 - 1 |
Equation - (2) | |||
| Σ | |||||||
| i = 0 |
Now let a binary tree of height h and n nodes. Since we know that n will always be less then or equal to the maximum number of nodes possible (N h) in a binary tree of height h. So from Equation - (2) we can say:
n ≤ 2(h+1)-1
|
Equation - (3) |
Now, when we know that log2(n+1)-1(n-1) because any path of n nodes always has n-1 branches. So, now we have:
h ≤ (n-1) |
Equation - (4) |
And now, from Equation - (3) and Equation - (4) we have:
log2(n+1)-1 ≤ h ≤ (n-1)log2(n+1)-1 <= h <= (n-1) |
Proved |
To get the values of h and n at equality condition we have to assume this:
log2(n+1)-1 = h = (n-1)⇒ log2(n+1)-1 = (n-1)⇒ log2(n+1) = n⇒ (n+1) = 2n⇒ n = 1 |
Equation - (5) |
Putting Equation - (5) in the initial condition of equality:
log2(1+1)-1 = h = (1-1)⇒ 0 = h = 0 |
Equation - (6) |
From Equation - (5) and (6) we can say that at the condition of equality value of h will be zero and value of n will be one.
This theorem tells us that maximum height possible for a binary tree of 'n' nodes is '(n-1)', while it's minimum possible value is 'log2(n+1)-1'. In this way we can minimize or maximize the height as per our requirement.