recategorized by
1,742 views
1 1 vote

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?

2 Answers

Best answer
8 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 –1 vote
Ans should be 4.
Position:
Show:

Related questions

9 9 votes
1 1 answer
213
213 views
GO Classes asked Jul 22
213 views
Heaps are usually implemented using arrays.If an element is present at a known array index in a heap of size $N$, what is the time complexity to remove that element and r...
2 2 votes
1 1 answer
162
162 views
GO Classes asked Jul 21
162 views
A max-heap is stored using $0$-based indexing as:$$[60, 30, 45, 15, 5, 10, 20]$$During the first iteration of heap sort:Swap the root with the last element. Reduce the he...
4 4 votes
2 2 answers
157
157 views
GO Classes asked Jul 21
157 views
A min-heap is stored using $1$-based indexing as:$[2, 13, 7, 17, 14, 22, 8, 21]$After one $\texttt{DeleteMin}$ operation, what is the final heap array?$[7, 13, 8, 17, 14,...
4 4 votes
2 2 answers
171
171 views
GO Classes asked Jul 21
171 views
The following max-heap is stored using $1$-based indexing:$[57, 53, 42, 48, 25, 34, 29, 18, 30, 25]$Insert $55$ into this max-heap. What is the final heap array?$[57, 55,...