edited by
7,150 views
28 votes
28 votes

Consider a max heap, represented by the array: $40, 30, 20, 10, 15, 16, 17, 8, 4$.

$$\begin{array}{|l|l|}\hline \text{Array index}  &  \text{1} & \text{2}  &  \text{3} & \text{4} & \text{5}  &  \text{6} & \text{7}  &  \text{8} & \text{9} \\\hline \text{Value}  &  \text{40} & \text{30}  &  \text{20} & \text{10} &\text{15}  &  \text{16} & \text{17}  &  \text{8} & \text{4} \\\hline \end{array}$$

Now consider that a value $35$ is inserted into this heap. After insertion, the new heap is

  1. $40, 30, 20, 10, 15, 16, 17, 8, 4, 35$
  2. $40, 35, 20, 10, 30, 16, 17, 8, 4, 15$
  3. $40, 30, 20, 10, 35, 16, 17, 8, 4, 15$
  4. $40, 35, 20, 10, 15, 16, 17, 8, 4, 30$
edited by

1 Answer

Best answer
44 votes
44 votes
Heap is complete binary tree. To insert a new element, we put it at the end of the tree and move up towards root till heap property is satisfied. Here, $35$ comes as child of $15$, with the path $40-30-15-35$. So, we swap $15, 35$ and then $30, 35$ to get the new path $40-35-30-15$. So, new heap will be $40 \ 35 \ 20 \ 10 \ 30 \ 16 \ 17 \ 8 \ 4 \ 15$.

Correct Answer: $B$
edited by
Answer:

Related questions

11.3k
views
4 answers
23 votes
makhdoom ghaya asked Feb 13, 2015
11,342 views
The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum number of nodes in a binary tree of height $5$ are$63$ and ... $64$ and $5$, respectively$32$ and $6$, respectively$31$ and $5$, respectively
10.7k
views
3 answers
47 votes
makhdoom ghaya asked Feb 13, 2015
10,735 views
What are the worst-case complexities of insertion and deletion of a key in a binary search tree?$\Theta(\log n)$ for both insertion and deletion$\Theta(n)$ for ... $\Theta(\log n)$ for insertion and $\Theta(n)$ for deletion
7.5k
views
3 answers
25 votes
makhdoom ghaya asked Feb 12, 2015
7,549 views
Which of the following is/are correct in order traversal sequence(s) of binary search tree(s)?$3, 5, 7, 8, 15, 19, 25$5, 8, 9, 12, 10, 15, 25$2, 7, 10, 8, 14, 16, 20$4, 6, 7, 9, 18, 20, 25$I and IV onlyII and III onlyII and IV onlyII only
9.4k
views
4 answers
33 votes
go_editor asked Feb 14, 2015
9,402 views
Consider the following array of elements.$\langle 89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100 \rangle$The minimum number of interchanges needed to convert it into a max-heap is$4$5$2$3$