# Data Structures and Algorithms | Set 23

Following questions have been asked in GATE CS 2005 exam.  1. Which one of the following is a key factor for preferring B-trees to binary search trees for indexing database relations? (a) Database relations have a large number of records (b) Database relations are sorted on the primary key (c) B-trees require less memory than binary search trees (d) Data transfer form disks is in blocks. Answer (d) A disk block contains fairly large number of keys. Unlike BST where each node contains only one key, B-Tree is designed to contain large number of keys so that tree height is small. 2. How many distinct binary search trees can be created out of 4 distinct keys? (a) 5 (b) 14 (c) 24 (d) 42 Answer (b) Here is a systematic way to enumerate these BSTs. Consider all possible binary search trees with each element at the root. If there are n nodes, then for each choice of root node, there are n – 1 non-root nodes and these non-root nodes must be partitioned into those that are less than a chosen root and those that are greater than the chosen root. Let’s say node i is chosen to be the root. Then there are i – 1 nodes smaller than i and n – i nodes bigger than i. For each of these two sets of nodes, there is a certain number of possible subtrees. Let t(n) be the total number of BSTs with n nodes. The total number of BSTs with i at the root is t(i – 1) t(n – i). The two terms are multiplied together because the arrangements in the left and right subtrees are independent. That is, for each arrangement in the left tree and for each arrangement in the right tree, you get one BST with i at the root. Summing over i gives the total number of binary search trees with n nodes. The base case is t(0) = 1 and t(1) = 1, i.e. there is one empty BST and there is one BST with one node.   3. In a complete k-ary tree, every internal node has exactly k children. The number of leaves in such a tree with n internal nodes is: (a) nk (b) (n – 1) k+ 1 (c) n( k – 1) + 1 (d) n(k – 1) Answer (c) 4) Suppose T(n) = 2T(n/2) + n, T(0) = T(1) = 1 Which one of the following is false. a) T(n) = O(n^2) b) T(n) = Θ(nLogn) c) T(n) = Ω(n2) d) T(n) = O(nLogn) Answer (c) The given recurrence relation can be solved using Master Theorem. It lies in case 2 of Master Theorem. Or, if you remember recurrence relation of Merge Sort or best case Quick Sort, you can guess the value of T(n). T(n) = Θ(nLogn) By definition of Big O notation, we can say. Θ(nLogn) = O(nLogn) = O(n^2) Θ(nLogn) can be equal to Ω(n) or Ω(nLogn), but not Ω(n^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.