8.14

Lecture 25: Binary search🔗

This assignment is due on Sunday, November 17 at 11:59pm. Submit it using Handin as assignment lecture25.

Searching is a very common task: we might want to know how many times a given word occurs in a book, or find someone’s phone number in a contact list or phonebook. In Problem set 7: Lists, you implemented searching through a list of frequencies. In Problem set 10: Prefix trees, you implemented searching through a tree of letters. A list and a tree represent two different ways to decompose a search problem. Searching through a list can be slower than searching through a tree, because in a list, the target can be buried much farther from the root.

Binary search is a general and efficient search algorithm. It requires the data to be sorted, like the headwords in a dictionary. To look for a word in a dictionary using binary search, first we flip to the middle of the dictionary, and compare the word in the middle to the word we want. If they happen to be the same, then we’re lucky and done. If they’re different, then depending on their relative order, we can always throw away half of the dictionary. For instance, if we’re looking for “bro” and the middle of the dictionary is “law”, then we can throw away the second half of the dictionary, and continue with the middle of the first half.

In this lecture, we will implement binary search. The key is to decompose the problem of searching through a sorted list into the smaller problems of searching through its two halves.

Exercise 1. Let’s first practice the decomposition by hand. Below is the beginning of a binary search decomposition:

Each box contains a sorted list to search through.
  • If the list has an odd number of elements (say 9), then split it into two equally long halves (4 each), and leave the middle element.

  • If the list has an even number of elements (say 4), then split it into two equally long halves (2 each), and remove the first element from the right half to become the middle element.

  • Stop only when the list is empty. Any non-empty box, such as or even just , must be decomposed further.

Your job is to finish this decomposition.
  1. Use this app to draw the decomposition. Make a rectangle for every problem.

  2. Click the “Copy to clipboard” button in the lower-left corner of the app.

  3. Paste the code as a comment, like this:
    ; Binary search:
    ; <bpmn:definitions ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... </bpmn:definitions>

Exercise 2.
; A SearchTree is one of:
; - (make-not-found)
; - (make-try SearchTree String SearchTree)
(define-struct not-found [])
(define-struct try [left middle right])
Define the SearchTree you just made as a constant. Call it neighbors, because it contains the names of Monroe County and its neighbors.

Exercise 3. Design a function search-tree-contains? that determines whether the given SearchTree contains the given string. For example, neighbors contains "Monroe" but not "Middlesex".