# Binary Search Trees

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.

There are 38 questions to complete.