edited by
14,132 views
5 votes
5 votes

The number of nodes of height $h$ in any $n$-element heap is ________.

  1. $h$
  2. $2^{h}$
  3. ceil $\left[\frac{n}{2^{h}}\right]$
  4. ceil $\left[\frac{n}{2^{h+1}}\right]$

Answer is given as D, But I think it should be C. Because, even if you take height=1 then possible nodes are 3 and 2. 

edited by

5 Answers

Best answer
2 votes
2 votes
Answer shd be C.

We can prove it by taking random example.

if we take n=9 and required h=3(e.g.) then according to option d it will produce answer as (9/16=1(ceil))..and thats not correct..

If we consider option C--

(9/8=2) which is correct.
selected by
4 votes
4 votes

First, let's observe that the number of leaves in a heap is ⌈n/2⌉. Let's prove it by induction on h.

Base: h=0. The number of leaves is ⌈n/2⌉=⌈n/20+1⌉.

Step: Let's assume it holds for nodes of height h−1. Let's take a tree and remove all it's leaves. We get a new tree with n−⌈n/2⌉=⌊n/2⌋elements. Note that the nodes with height h in the old tree have height h−1 in the new one.

We will calculate the number of such nodes in the new tree. By the inductive assumption we have that T, the number of nodes with height h−1 in the new tree, is:

T=⌈⌊n/2⌋/2h−1+1⌉<⌈(n/2)/2h⌉=⌈n2h+1⌉

As mentioned, this is also the number of nodes with height h in the old tree.

Please upvote if you understand it. :D 

1 votes
1 votes

Answer should be D

Consider the above Heap where height of each nodes are follows

Height of the node with data 4 is 3

Height of the node with data 2,6 is 1

Height of the node with data 1,3,5,7 is 0

 

Number of nodes present at height 'h' in heap/ Complete binary tree= ceil( n/2^(h+1) ) 

 

In above Heap, find number of nodes at height 1

n=7, h=1

N=ceil(7/2^2)

=ceil(7/4)

=2

node with data 2 and 6 are present at height 1 so it's correct.

Ref: http://www.iitg.ac.in/psm/indexing_ma252/y12/LectureNoteMA252Jan23.pdf

Related questions

4 votes
4 votes
1 answer
1
2 votes
2 votes
0 answers
2
bhuv asked Jul 5, 2017
491 views
Number of node of height h in a binary tree is given as ceiling (n/2^(h+1)) from where this formula come ? I've only found proof by induction or solving for a particular ...
2 votes
2 votes
1 answer
3
vijaycs asked May 25, 2016
12,806 views
0 votes
0 votes
2 answers
4
sripo asked Dec 25, 2018
4,785 views
In a 3-array tree if internal nodes have exactly 3 children,the number of leaf nodes will be __ ?Does it vary for binary tree?What do you mean by internal nodes? Non roo...