# Data Structures and Algorithms | Set 16

Following questions have been asked in GATE CS 2009 exam.

**1. Consider a binary max-heap implemented using an array. Which one of the following array represents a binary max-heap?
**

(A) 25,12,16,13,10,8,14

(B) 25,14,13,16,10,8,12

(C) 25,14,16,13,10,8,12

(D) 25,14,12,13,10,8,16

Answer (C)

A tree is max-heap if data at every node in the tree is greater than or equal to it’s children’ s data.

In array representation of heap tree, a node at index i has its left child at index 2i + 1 and right child at index 2i + 2.

25 / \ / \ 14 16 / \ / \ / \ / \ 13 10 8 12

**2. What is the content of the array after two delete operations on the correct answer to the previous question?
**

(A) 14,13,12,10,8

(B) 14,12,13,8,10

(C) 14,13,8,12,10

(D) 14,13,12,8,10

Answer(D)

For Heap trees, deletion of a node includes following two operations.

1) Replace the root with last element on the last level.

2) Starting from root, heapify the complete tree from top to bottom..

Let us delete the two nodes one by one:

1) Deletion of 25:

Replace 25 with 12

12 / \ / \ 14 16 / \ / / \ / 13 10 8

Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.

16 / \ / \ 14 12 / \ / / \ / 13 10 8

2) Deletion of 16:

Replace 16 with 8

8 / \ / \ 14 12 / \ / \ 13 10

Heapify from root to bottom.

14 / \ / \ 8 12 / \ / \ 13 10

14 / \ / \ 13 12 / \ / \ 8 10

**3. In quick sort, for sorting n elements, the (n/4)th smallest element is selected as pivot using an O(n) time algorithm. What is the worst case time complexity of the quick sort?
**

(A) Θ(n)

(B) Θ(n Log n)

(C) Θ(n^2)

(D) Θ(n

^{2}log n)

Answer(B)

The recursion expression becomes:

T(n) = T(n/4) + T(3n/4) + cn

After solving the above recursion, we get Θ(nLogn).

4. What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0.

(A) 2

(B) 3

(C) 4

(D) 5

Answer(B)

AVL trees are binary trees with the following restrictions.

1) the height difference of the children is at most 1.

2) both children are AVL trees

a / \ / \ b c / \ / / \ / d e g / / h

References:

http://en.wikipedia.org/wiki/AVL_tree

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

## Recommended Posts:

- Data Structures and Algorithms | Set 28
- Data Structures and Algorithms | Set 36
- Data Structures and Algorithms | Set 34
- Data Structures and Algorithms | Set 29
- Data Structures and Algorithms | Set 31
- Data Structures and Algorithms | Set 30
- Data Structures and Algorithms | Set 32
- Data Structures and Algorithms | Set 33
- Data Structures and Algorithms | Set 35
- Data Structures and Algorithms | Set 37
- Data Structures and Algorithms | Set 38
- Data Structures and Algorithms | Set 4
- Data Structures and Algorithms | Set 10
- Data Structures and Algorithms | Set 11
- Data Structures and Algorithms | Set 27