Average number of comparisons for binary search 1

Data Structures
CSCI 132, Fall 2016
Lecture 24
Analyzing Search II
1
Comparison tree for binary
search 1
2
Average number of comparisons
for binary search 1
The number of comparisons = external path length
External path length = Sum of the number of branches
traversed in going from root to leaf once for each leaf.
For n = 10, external path length = ?
Answer: ((4*5) + (6*4))*2 = 88
Half the leaves are for successful searches.
Half the leaves are for unsuccessful searches.
Average number of comparisons for successful searches is?
Answer: 0.5(88)/10 = 4.4
Average number of comparisons for unsuccessful searches is?
Answer: 4.4
3
Comparison tree for binary
search 2
4
Average number of comparisons
for binary search 2
For successful searches--average number of comparisons is related to the
internal path length.
Internal path length = sum of the number of branches traversed from root to
vertex for each non-leaf vertex.
Internal path length for n = 10 is ?
Answer: (0+1+2+2+3+1+2+3+2+3) = 19
Number of vertices traversed on each path is one more than path length.
Number of comparisons is 2 for each non-terminating vertex and 1 for each
terminating vertex.
Total comparisons = ?
Answer: 2*(19 + 10) - 10 = 48
Average number of comparisons = ?
Answer: 48/10 = 4.8 (worse than binary search 1)
5
Average number of comparisons
for binary search 2
For unsuccessful searches--average number of comparisons is related to
external path length.
For binary search 2 tree, external path length = ?
Answer: (5*3) + (6*4) = 39
Since there are 2 comparisons per vertex, and 11 possible unsuccessful
terminations, then:
Average number of comparisons = ?
Answer: 2*39/11 = 7.1
6
In general, for lists of length n:
Successful
Binary Search 1
lg n + 1
Binary Search 2
2 lg n - 3
Unsuccessful
lg n + 1
2 lg n
7
Graphing the comparisons
8
2-trees
2-trees: Each non-leaf vertex has at most 2 children.
Level
Number of
Vertices
0
20
1
21
2
22
Number of vertices at level t is at most 2t
If there are k vertices at level t, then t >= log2(k) = lg(k)
Notation: log2(x) = lg(x), log10(x) = log(x), loge (x) = ln(x)
9