GATE 2003
107 questions
MCQ 1071-mark 107
Questions 61–65 of 107
Then ,
where is
Answer: (B)
The sum takes each key in turn as the root. If the root is the -th smallest of the keys, then the smaller keys must go to the left sub-tree and the remaining larger keys must go to the right sub-tree.
The left sub-tree can be any BST on its keys, which gives ways. The right sub-tree can be any BST on its keys, which gives ways. The two choices are independent, so they multiply, and a change in just one sub-tree gives a different tree. So:
Check: the root takes 1 node, so , which gives . The other options, such as , would make the node count more than .
Answer: (B)
1. Delection of the smallest element.
2. Insertion of an element if it is not already present in the set.
Which of the following data structures can be used for this purpose ?
Answer: (B)
A balanced search tree has height .
Removing the smallest element takes time.
Checking whether an element is present, and then inserting it:
A (min) heap is an almost complete binary tree, and its height is .
Removing the smallest element takes time (take out the root, put the last element in its place, then balance).
Checking whether an element is present, and then inserting it: the check alone takes . The insertion and balancing take . So the total is
The answer is B.
(Even if it is a max heap, the answer stays the same. Only the time to delete the minimum becomes larger: )
Answer: (D)
So the 7th smallest lies in the top 7 levels, which hold at most elements. Examine these (for example by a level-order traversal) and pick the 7th smallest. This is a fixed amount of work, whatever the value of , so the time is .
(If only the usual heap operations were allowed, 7 Extract-min operations would cost .)
Answer: (D)
What is the result of inserting G in the above tree ?
Answer: (B)
The root is full, so split it: moves up and becomes the new root, with children and . The children of are and , and those of are and .
, so go to . , so go to . It is full, so split it: moves up into the parent, which becomes , and the leaf becomes and .
, so G goes into , giving .
The result is root , with children and , and the leaves , , , , . This is tree B.
Tree A has G inside an internal node with leaf . That comes from adding G to first and then splitting at G, which is not this procedure. Tree C keeps a root with 3 keys, but the full root must be split before going down.
Answer: (B)
What would be the worst case time complexity of the insertion Sort algorithm, if the inputs are restricted to permutations of 1....n with at most n inversions?
Answer: (D)
First, see how the question defines an Inversion.
In a permutation of n distinct integers, an inversion is a pair where and .
- Note the difference between a swap and an inversion.
- Swapping is done by the programmer, so it is an explicit step. An Inversion is already present in the input, so it is implicit.
Ex :- Take the input =>
The inversions here are: , , , , , , and . So an inversion belongs to the input. It is not an operation (swap) done by us.
The time complexity of Insertion Sort is , where is the total number of inversions.
Ex :- Take the input =>
How many comparisons are needed to put in its right place?
- First, is compared with . The result is True, as it is smaller than .
- Next, it is compared with , and again the result is True.
- The same happens with . All of them give True.
So comparisons were inversions, and more comparison is needed, where the while loop fails.
To put in its right place, comparisons are needed.
So the total comparisons in Insertion Sort are: number of elements where the while loop fails + total number of inversions in the input
- Elements where the while loop fails: Take the input . The first element stays as it is. Each other element has one failed while-loop comparison. So the total is comparisons.
- Total number of inversions in the input: Best Case : and Worst Case :
Total time complexity of insertion sort:
The question says there are at most inversions, so the time complexity is :- .
Correct Answer: