recategorized by
958 views
1 votes
1 votes

The number of ways in which the numbers 1, 2, 3, 4, 5 can be inserted into
a Binary Heap such that resulted binary heap is Max Heap.
Please  give the ans and what is the formula for n distinct elements?

recategorized by

2 Answers

Best answer
8 votes
8 votes

Given inputs that can be inserted in the heap$H= \left \{ 1,2,3,4,5 \right \}$ and the resultant heap must be a max heap.

$\Rightarrow 5 must$ be root(No Choice) and we are left with 4 element.

$\Rightarrow$we have now two sets one is Left Subtree (let it be 'L') and other is Right Subtree(let it be 'R')

$\Rightarrow \left | L \right |=3 and \left | R \right |=1$  (Reason-:Heap is complete/almost complete binary tree)

The two sets look like$\Rightarrow$

S_N_O Set L Set R
1 $\left \{ 1,2,3 \right \}$ $\left \{ 4 \right \}$
2 $\left \{ 2,3,4 \right \}$ $\left \{ 1 \right \}$
3 $\left \{ 1,3,4 \right \}$ $\left \{ 2 \right \}$
4 $\left \{ 1,2,4 \right \}$ $\left \{ 3 \right \}$

For S_N_O 1$\Rightarrow$ 3 must be root and '1' and '2' is left or right child (2 choices for S_N_O 1) and same for all the S_N_O .. so total possiblity=2+2+2+2=8 

selected by
–1 votes
–1 votes
Ans should be 4.

Related questions

6 votes
6 votes
0 answers
2