CSSE 230
Data Structures and Algorithm Analysis

Homework 4 - 40 points

To Be Turned In

  1. (20 points) Calculate the exact average-case time complexity of the binary search algorithm shown in Weiss, Figure 5.11. Only consider successful searches (where the item we are looking for is actually in the array) and assume all elements are equally likely to be the one we are searching for. 

    1. Complete this for N=7 elements in the array.
    2. Complete this for N=15 elements in the array.
    3. Complete this for N = 2k - 1 for some integer k ≥ 0, generalizing from what you found in (a) . 

    Hint: For each i, count how many of the array elements will be found after binary search examines exactly i array elements. (For example, the middle element is the only one to be found after examining one array element, and there are two elements that can be found afterexaming two array elements.) Then sum over all and divide to find the average. For the generalization, you'll use a summation.
    Potentially useful fact: Depending on how you work this problem, you may find the following useful:  Of course, you must explain how the formula is useful.  (Hint: Weiss 5.6.2 about binary search gives the big-oh answer for this problem, so you can use that as a sanity check.)

  2. (20 points) Checkout the TreePractice project from your SVN repository. Complete them, per the given specifications, and commit your solutions back to the repositories. These are good practice for exams. Recall the recursive pattern from day 7. One hint: on some problems, you will want to communicate information down the tree as you recurse, so you will find it helpful to add a parameter to your recursive method. 

For Your Consideration

These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.