3
$\begingroup$

This is a GRE practice question.

BST n=8

If a node in the binary search tree above is to be located by binary tree search, what is the expected number of comparisons required to locate one of the items (nodes) in the tree chosen at random?

(A) 1.75

(B) 2

(C) 2.75

(D) 3

(E) 3.25

My answer was 3 because $n=8$ and $\lg(n)$ comparisons should be made, and $\lg(8) = 3$. But the correct answer is 2.75. Can someone explain the correct answer? Thanks!

$\endgroup$
  • 3
    $\begingroup$ Chosen at random according to what distribution? *sigh* It makes me sad when you have a whole country where getting an advanced degree depends on passing an exam that's set by an organization that doesn't know the difference between "at random" and "uniformly at random". $\endgroup$ – David Richerby Aug 13 '15 at 8:10
  • $\begingroup$ @DavidRicherby Also, they can't draw trees. The image clearly shows a graph with a 4-cycle of node $E$, $C$, $H$ and $DF$. $\endgroup$ – Raphael Aug 13 '15 at 11:27
  • $\begingroup$ Re-reading this, let me note that the approach is wrong because it a) confuses average and worst-case and b) assumes a (tight) $\lg n$ bound that does not hold. $\endgroup$ – Raphael Aug 13 '15 at 11:29
  • $\begingroup$ @DavidRicherby Of course. I was trying to make a joke. $\endgroup$ – Raphael Aug 13 '15 at 11:37
8
$\begingroup$

Recall, how expected value is defined. You count the for every element $X$ in the tree the number of comparisons it takes to locate it, say $C(X)$. Then $$E[\text{# of comparisons}]=\sum_{X\in\{A,\ldots,H\}} p_X \cdot C(X),$$ where $p_x$ denotes the probability that $X$ is chosen, which is the same for all $X$, namely $1/8$. In other words, you compute the average over the $C(X)$s.

$\endgroup$
  • $\begingroup$ Working out the math for A.Schulz's answer (4*2+3*3+(2*2)+1)/8=2.75. Ie takes 1 comparison to find E, 2 comparisons to find C and H, etc.. $\endgroup$ – Philip Menke Oct 22 '14 at 1:42
0
$\begingroup$

Find comparisons for every element and total them ie; 22 comparisions for 8 elements so for 1 element 22/8=2.75

$\endgroup$
  • 1
    $\begingroup$ That only works for the uniform distribution, and adds nothing over the current accepted answer. $\endgroup$ – Raphael Aug 13 '15 at 7:15
  • $\begingroup$ @Raphael The accepted answer does not actually give the result, but kind of explain, much too abstractedly for my (pedagogical) taste, how to compute it. The answer is there, by in someone else's comment which does not count. Actually, I was bothered by the question because I do not know what counts as a comparison. I still do not really know. Is it ignorance on my part, or lack of precision in question and answers? $\endgroup$ – babou Aug 13 '15 at 10:30
  • $\begingroup$ Your answer is credible. But you do not give enough details on why you do it that way. An answer that is too brief does not help readers to really understand. And if you were just applying the formula of the previous answer, you should say so explicitly. $\endgroup$ – babou Aug 13 '15 at 10:35
  • $\begingroup$ @babou Every community member can propose an edit to the accepted answer, extending it by the concrete example from the comment. $\endgroup$ – Raphael Aug 13 '15 at 11:26
-2
$\begingroup$

1 node at level 1 {E} 2 node at level 2 {C H} 3 node at level 3 {B D F } 2 node at level 4 {A G} 1*1+2*2+3*3+2*4=22 22/8=2.75(ANS)

$\endgroup$
  • 3
    $\begingroup$ The question asks for an explanation, not a calculation: anyone who understands the concepts should be able to do the calculation on their own. We already know that the answer is 2.75 because that's stated in the question. $\endgroup$ – David Richerby Jun 16 '15 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.