18,581 views
8 votes
8 votes

Consider the following Binary Search Tree


               10
             /    \
            5      20
           /      /  \           
          4     15    30
               /  
              11      

If we randomly search one of the keys present in above BST, what would be the expected number of comparisons?

(A) 2.75
(B) 2.25
(C) 2.57
(D) 3.25

2 Answers

Best answer
17 votes
17 votes

Expected number of comparisons will be the sum of the product of probability of an item being the searched value and the no. of comparisons for the same. We are given that search key is chosen randomly from one of the keys present in the tree. So, the probability for each item being the searched key = $\frac{1}{n}$, where $n$ is the total no. of keys in the tree. 

In a BST, if an item matches at level $h$, we would have done $h$ comparisons. 

So, $\text{Expected no. of comparisons} = \sum_{i = 1}^n h_i \times \frac{1}{n} \\ \text{where }h_i \text{ is the level of node }i \\ =\sum_{i=1}^7 h_i \times \frac{1}{7} \\ = \frac{1}{7} + \frac{2}{7} + \frac{2}{7} + \frac{3}{7} + \frac{3}{7} + \frac{3}{7} + \frac{4}{7} \\ = \frac{18}{7} = 2.57$.

selected by
1 votes
1 votes
Answer (C)

Expected number of comparisons will be average comparisons, as element to be  searched is in the list( it is already given ) so

For level 0  node we will require 1 comparison------------→total comparisons at level 0  $1*1=1$

For a single level 1  node we will require 2 comparison.--->total comparisons at level 1 $2*2=4$

For a single level 2  node we will require 3 comparison----->total comparisons at level 2 $3*3=9$

For a single level 2  node we will require 3 comparison----->total comparisons at level 3 $4*1=4$

total comparisons $= 1 + 4 + 9 + 4=18$

 

average comparisons $= 18/7 = 2.57$

Option (C)
edited by

Related questions

1 votes
1 votes
1 answer
1
s_dr_13 asked Mar 10, 2019
2,711 views
Consider the following binary tree with root at level 0. What is the internal path length for the above tree?31142932
0 votes
0 votes
2 answers
2
sripo asked Dec 25, 2018
4,808 views
In a 3-array tree if internal nodes have exactly 3 children,the number of leaf nodes will be __ ?Does it vary for binary tree?What do you mean by internal nodes? Non roo...
5 votes
5 votes
5 answers
3
0 votes
0 votes
5 answers
4
radha gogia asked Sep 30, 2015
1,672 views
If I am given an array $X$ of $n$ distinct integers which is interpreted as a complete binary tree, so if the parent is at index $i$, then it's left child would be at ind...