# Data Structures and Algorithms | Set 7

Following questions have been asked in GATE CS 2006 exam.

**1. In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006)**

(A) 0(n)

(B) O(logn)

(C) 0(loglogn)

(D) 0(1)

Answer (A)

In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf nodes for the minimum value. Worst case complexity will be O(n)

12 / \ / \ 8 7 / \ / \ / \ / \ 2 3 4 5

**2. A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. the root is stored at X[1]. For a node stored at X[i], the left child, if any, is stored in X[2i] and the right child, if any, in X[2i+1]. To be able to store any binary tree on n vertices the minimum size of X should be. (GATE CS 2006)**

(A) log2n

(B) n

(C) 2n + 1

(D) 2^n — 1

Answer (D)

For a right skewed binary tree, number of nodes will be 2^n – 1. For example, in below binary tree, node ‘A’ will be stored at index 1, ‘B’ at index 3, ‘C’ at index 7 and ‘D’ at index 15.

A \ \ B \ \ C \ \ D

**3. Which one of the following in place sorting algorithms needs the minimum number of swaps? (GATE CS 2006)**

(A) Quick sort

(B) Insertion sort

(C) Selection sort

(D) Heap sort

Answer (C)

For selection sort, number of swaps required is minimum ( Θ(n) ).

**4. An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array (GATE CS 2006)**

(A) Solves it in linear time using a left to right pass of the array

(B) Solves it in linear time using a right to left pass of the array

(C) Solves it using divide and conquer in time 8(nlogn)

(D) Solves it in time 8(n2)

Answer (B)

Please see this post for explanation.

**5. Consider a weighted complete graph G on the vertex set {v1, v2, ..vn} such that the weight of the edge (vi, vj) is 2|i-j|. The weight of a minimum spanning tree of G is: (GATE CS 2006)**

(A) n — 1

(B) 2n — 2

(C) nC2

(D) 2

Answer (B)

Minimum spanning tree of such a graph is

v1 \ v2 \ v3 \ v4 . . . vn

Weight of the minimum spanning tree

= 2|2 – 1| + 2|3 – 2| + 2|4 – 3| + 2|5 – 4| …. + 2| n – (n-1) |

= 2n – 2

**Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.**

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 29
- Data Structures and Algorithms | Set 28
- Data Structures and Algorithms | Set 37
- Data Structures and Algorithms | Set 27
- Data Structures and Algorithms | Set 38
- Data Structures and Algorithms | Set 34
- Data Structures and Algorithms | Set 35
- Data Structures and Algorithms | Set 33
- Data Structures and Algorithms | Set 32
- Data Structures and Algorithms | Set 30
- Data Structures and Algorithms | Set 31
- Data Structures and Algorithms | Set 26
- Data Structures and Algorithms | Set 1
- Data Structures and Algorithms | Set 17
- Data Structures and Algorithms | Set 15