# Data Structures and Algorithms | Set 16

• Difficulty Level : Easy
• Last Updated : 27 Mar, 2017

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

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

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) Θ(n2 log n)

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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :