Question 1 |

What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree?

O(n) for all | |

O(Logn) for all | |

O(Logn) for search and insert, and O(n) for delete | |

O(Logn) for search, and O(n) for insert and delete |

**Binary Search Trees**

**Discuss it**

Question 1 Explanation:

In skewed Binary Search Tree (BST), all three operations can take O(n). See the following example BST and operations.

10 / 20 / 30 / 40 Search 40. Delete 40 Insert 50.

Question 2 |

In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation?

Inorder Successor is always a leaf node | |

Inorder successor is always either a leaf node or a node with empty left child | |

Inorder successor may be an ancestor of the node | |

Inorder successor is always either a leaf node or a node with empty right child |

**Binary Search Trees**

**Discuss it**

Question 2 Explanation:

Let X be the node to be deleted in a tree with root as 'root'. There are three cases for deletion
1) X is a leaf node: We change left or right pointer of parent to NULL (depending upon whether X is left or right child of its parent) and we delete X
2) One child of X is empty: We copy values of non-empty child to X and delete the non-empty child
3) Both children of X are non-empty: In this case, we find inorder successor of X. Let the inorder successor be Y. We copy the contents of Y to X, and delete Y.
Sp we need inorder successor only when both left and right child of X are not empty. In this case, the inorder successor Y can never be an ancestor of X. In this case, the inorder successor is the leftmost node in right subtree of X. Since it is leftmost node, the left child of Y must be empty.

Question 3 |

We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree? (GATE CS 2011)

0 | |

1 | |

n! | |

(1/(n+1)).2nCn |

**Binary Search Trees**

**Discuss it**

Question 3 Explanation:

There is only one way. The minimum value has to go to the leftmost node and the maximum value to the rightmost node. Recursively, we can define for other nodes.

Question 4 |

How many distinct binary search trees can be created out of 4 distinct keys?

4 | |

14 | |

24 | |

42 |

**Binary Search Trees**

**Discuss it**

Question 4 Explanation:

See question 2 of http://www.geeksforgeeks.org/data-structures-and-algorithms-set-23/ for explanation. The link also has a generalized solution.

Question 5 |

Which of the following traversal outputs the data in sorted order in a BST?

Preorder | |

Inorder | |

Postorder | |

Level order |

**Binary Search Trees**

**Discuss it**

Question 5 Explanation:

Inorder traversal of a BST outputs data in sorted order. Read here for details.

Question 6 |

Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?

7 5 1 0 3 2 4 6 8 9 | |

0 2 4 3 1 6 5 9 8 7 | |

0 1 2 3 4 5 6 7 8 9 | |

9 8 6 4 2 3 0 1 5 7 |

**Binary Search Trees**

**Discuss it**

Question 6 Explanation:

In-order traversal of a BST gives elements in increasing order. So answer c is correct without any doubt.

Question 7 |

The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)? (GATE CS 2004)

2 | |

3 | |

4 | |

6 |

**Binary Search Trees**

**Discuss it**

Question 7 Explanation:

Constructed binary search tree will be..

10 / \ 1 15 \ / \ 3 12 16 \ 5

Question 8 |

The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

10, 20, 15, 23, 25, 35, 42, 39, 30 | |

15, 10, 25, 23, 20, 42, 35, 39, 30 | |

15, 20, 10, 23, 25, 42, 35, 39, 30 | |

15, 10, 23, 25, 20, 35, 42, 39, 30 |

**Binary Search Trees**

**Discuss it**

Question 8 Explanation:

The following is the constructed tree

30 / \ 20 39 / \ / \ 10 25 35 42 \ / 15 23

Question 9 |

Consider the following Binary Search Tree

10 / \ 5 20 / / \ 4 15 30 / 11If we randomly search one of the keys present in above BST, what would be the expected number of comparisons?

2.75 | |

2.25 | |

2.57 | |

3.25 |

**Binary Search Trees**

**Discuss it**

Question 9 Explanation:

Expected number of comparisons = (1*1 + 2*2 + 3*3 + 4*1)/7 = 18/7 = 2.57

Question 10 |

Which of the following traversals is sufficient to construct BST from given traversals
1) Inorder
2) Preorder
3) Postorder

Any one of the given three traversals is sufficient | |

Either 2 or 3 is sufficient | |

2 and 3 | |

1 and 3 |

**Binary Search Trees**

**Discuss it**

Question 10 Explanation:

When we know either preorder or postorder traversal, we can construct the BST. Note that we can always sort the given traversal and get the inorder traversal. Inorder traversal of BST is always sorted.

Question 11 |

Consider the following code snippet in C. The function print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.

// A BST node struct node { int data; struct node *left, *right; }; int count = 0; void print(struct node *root, int k) { if (root != NULL && count <= k) { print(root->right, k); count++; if (count == k) printf("%d ", root->data); print(root->left, k); } }What is the output of print(root, 3) where root represent root of the following BST.

15 / \ 10 20 / \ / \ 8 12 16 25

10 | |

16 | |

20 | |

20 10 |

**Binary Search Trees**

**Discuss it**

Question 11 Explanation:

The code mainly finds out k'th largest element in BST, see K’th Largest Element in BST for details.

Question 12 |

Consider the same code as given in above question. What does the function print() do in general?
The function print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.

// A BST node struct node { int data; struct node *left, *right; }; int count = 0; void print(struct node *root, int k) { if (root != NULL && count <= k) { print(root->right, k); count++; if (count == k) printf("%d ", root->data); print(root->left, k); } }

Prints the kth smallest element in BST | |

Prints the kth largest element in BST | |

Prints the leftmost node at level k from root | |

Prints the rightmost node at level k from root |

**Binary Search Trees**

**Discuss it**

Question 12 Explanation:

The function basically does reverse inorder traversal of the given Binary Search Tree. The reverse inorder traversal produces data in reverse sorted order. Whenever a nod is visited, count is incremented by 1 and data of a node is printed only when count becomes k.

Question 13 |

You are given the postorder traversal, P, of a binary search tree on the n elements 1, 2, ..., n. You have to determine the unique binary search tree that has P as its postorder traversal. What is the time complexity of the most efficient algorithm for doing this?

O(Logn) | |

O(n) | |

O(nLogn) | |

none of the above, as the tree cannot be uniquely determined. |

**Binary Search Trees**

**GATE CS 2008**

**Discuss it**

Question 13 Explanation:

One important thing to note is, it is Binary Search Tree, not Binary Tree. In BST, inorder traversal can always be obtained by sorting all keys.
See method 2 of http://www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/ for details.
Same technique can be used for postorder traversal.

Question 14 |

Suppose we have a balanced binary search tree T holding n numbers. We are given two numbers L and H and wish to sum up all the numbers in T that lie between L and H. Suppose there are m such numbers in T. If the tightest upper bound on the time to
compute the sum is O(n

^{a}log^{b}n + m^{c}log^{d}n), the value of a + 10b + 100c + 1000d is ____.60 | |

110 | |

210 | |

50 |

**Binary Search Trees**

**GATE-CS-2014-(Set-3)**

**Discuss it**

Question 14 Explanation:

int getSum(node *root, int L, int H) { // Base Case if (root == NULL) return 0; if (root->key < L) return getSum(root->right, L, H); if (root->key > H) return getSum(root->left, L, H) if (root->key >= L && root->key <=H) return getSum(root->left, L, H) + root->key + getSum(root->right, L, H); }The above always takes O(m + Logn) time. Note that the code first traverses across height to find the node which lies in range. Once such a node is found, it recurs for left and right children. Two recursive calls are made only if the node is in range. So for every node that is in range, we make at most one extra call (here extra call means calling for a node that is not in range).

Question 15 |

n-k+1 | |

n-k | |

n-k-1 | |

n-k-2 |

**Binary Search Trees**

**GATE-CS-2003**

**Discuss it**

Question 15 Explanation:

The idea is to make a key root, put (k-1) keys in one subtree and remaining n-k keys in other subtree.
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −

- The left sub-tree of a node has a key less than or equal to its parent node's key.
- The right sub-tree of a node has a key greater than to its parent node's key.

__If n=1__We have only one possibility, therefore only 1 BST.__If n=2__We have 2 possibilities , when smaller number is root and bigger number is the right child or second when the bigger number is root and smaller number as left child.__If n=3__We have 5 possibilities. Keeping each number first as root and then arranging the remaining 2 numbers as in case of n=2.__If n=4__We have 14 possibilities. Taking each number as root and arranging smaal numbers as left subtree and larger numbers as right subtree. Thus we can conclude that with n distinct numbers, if we take ‘k’ as root then all the numbers smaller than k will left subtree and numbers larger than k will be right subtree where the the right subtree and left subtree will again be constructed recursively like the root. Therefore, This solution is contributed by**Parul Sharma.**Question 16 |

What are the worst-case complexities of insertion and deletion of a key in a binary search tree?

Θ(logn) for both insertion and deletion | |

Θ(n) for both insertion and deletion | |

Θ(n) for insertion and Θ(logn) for deletion | |

Θ(logn) for insertion and Θ(n) for deletion |

**Binary Search Trees**

**GATE-CS-2015 (Set 1)**

**Discuss it**

Question 16 Explanation:

The time taken by search, insert and delete on a BST is always proportional to height of BST. Height may become O(n) in worst case.

Question 17 |

While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search tree (BST) in the sequence shown, the element in the lowest level is

65 | |

67 | |

69 | |

83 |

**Binary Search Trees**

**GATE-CS-2015 (Set 3)**

**Discuss it**

Question 17 Explanation:

Here is The Insertion Algorithm For a Binary Search Tree :

Insert(Root,key) { if(Root is NULL) Create a Node with value as key and return Else if(Root.key <= key) Insert(Root.left,key) Else Insert(Root.right,key) }Creating the BST one by one using the above algorithm in the below given image : This solution is contributed by

**Pranjul Ahuja.**Question 18 |

The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _____________
Note: The height of a tree with a single node is 0.
[This question was originally a Fill-in-the-Blanks question]

2 | |

4 | |

64 | |

32 |

**Binary Search Trees**

**GATE-CS-2016 (Set 2)**

**Discuss it**

Question 18 Explanation:

To get height 6, we need to put either 1 or 7 at root.
So count can be written as T(n) = 2*T(n-1) with T(1) = 1

7 / [1..6] 1 \ [2..7]Therefore count is 2

^{6}= 64 Another Explanation: Consider these cases, 1 2 3 4 5 6 7 1 2 3 4 5 7 6 1 7 6 5 4 3 2 1 7 6 5 4 2 3 7 6 5 4 3 2 1 7 6 5 4 3 1 2 7 1 2 3 4 5 6 7 1 2 3 4 6 5 For height 6, we have 2 choices. Either we select the root as 1 or 7. Suppose we select 7. Now, we have 6 nodes and remaining height = 5. So, now we have 2 ways to select root for this sub-tree also. Now, we keep on repeating the same procedure till remaining height = 1 For this last case also, we have 2 ways. Therefore, total number of ways = 2^{6}= 64Question 19 |

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 55. Which of the following sequences CANNOT be the sequence of nodes examined?

{10, 75, 64, 43, 60, 57, 55} | |

{90, 12, 68, 34, 62, 45, 55} | |

{9, 85, 47, 68, 43, 57, 55} | |

{79, 14, 72, 56, 16, 53, 55} |

**Binary Search Trees**

**GATE IT 2006**

**Discuss it**

Question 19 Explanation:

In BST on right side of parent number should be greater than it, but in C after 47, 43 appears that is wrong.

Question 20 |

When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70 80, 90 are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value 60?

35 | |

64 | |

128 | |

5040 |

**Binary Search Trees**

**Tree Traversals**

**Gate IT 2007**

**Discuss it**

Question 20 Explanation:

There are two set of values, smaller than 60 and greater than 60. Smaller values 10, 20, 40 and 50 are visited, means they are visited in order. Similarly, 90, 80 and 70 are visited in order.

= 7!/(4!3!)

= 35

= 7!/(4!3!)

= 35

Question 21 |

A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys.

Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search?

I. 81, 537, 102, 439, 285, 376, 305

II. 52, 97, 121, 195, 242, 381, 472

III. 142, 248, 520, 386, 345, 270, 307

IV. 550, 149, 507, 395, 463, 402, 270

Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search?

II and III only | |

I and III only | |

III and IV only | |

III only |

**Binary Search Trees**

**Gate IT 2008**

**Discuss it**

Question 21 Explanation:

Key to be searched 273:

- I) 81, 537, 102, 439, 285, 376, 305 is not correct We cannot go to 376 from 285 as 273 is smaller than 285.
- II) 52, 97, 121, 195, 242, 381, 472 is not correct. We cannot go to 472 from 381 as 273 is smaller than 381.
- III) 142, 248, 520, 386, 345, 270, 307 is correct
- 550, 149, 507, 395, 463, 402, 270 is not correct. We cannot go to 463 from 395 in search of 273

Question 22 |

A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys.

I. 81, 537, 102, 439, 285, 376, 305

II. 52, 97, 121, 195, 242, 381, 472

III. 142, 248, 520, 386, 345, 270, 307

IV. 550, 149, 507, 395, 463, 402, 270

Which of the following statements is TRUE?

I. 81, 537, 102, 439, 285, 376, 305

II. 52, 97, 121, 195, 242, 381, 472

III. 142, 248, 520, 386, 345, 270, 307

IV. 550, 149, 507, 395, 463, 402, 270

Which of the following statements is TRUE?

I, II and IV are inorder sequences of three different BSTs | |

I is a preorder sequence of some BST with 439 as the root | |

II is an inorder sequence of some BST where 121 is the root and 52 is a leaf | |

IV is a postorder sequence of some BST with 149 as the root |

**Binary Search Trees**

**Gate IT 2008**

**Discuss it**

Question 22 Explanation:

A: I and IV are not in ascending order

B: If 439 is root, It should be 1st element in preorder

D: IV is a postorder sequence of some BST with 149 as the root, => False

B: If 439 is root, It should be 1st element in preorder

D: IV is a postorder sequence of some BST with 149 as the root, => False

Question 23 |

How many distinct BSTs can be constructed with 3 distinct keys?

4 | |

5 | |

6 | |

9 |

**Binary Search Trees**

**Gate IT 2008**

**Discuss it**

Question 23 Explanation:

^{2n}C

_{n}/(n+1) =

^{6}C

_{3}/4 = 5

There are 23 questions to complete.