# Class 38: Dictionaries (2): Binary Search Trees

Back to Dictionaries (1). On to Project Discussion.

Held: Monday, 8 November 2004

Summary: Today we consider a tree-based implementation of dictionaries, the binary search tree.

Related Pages:

Notes:

• Cool CS talk Wednesday at 4:30 (snacks at 4:15). Extra credit for attending.
• No additional homework for tomorrow (project day).

Overview:

• An ADT for binary search.
• Implementing that ADT with arrays.
• Binary search trees.
• BSTs and dictionaries.

## A Data Structure for Binary Search

• To solve the problem of building a better structure for dictionaries, we might step back and reflect on the problem of binary search.
• After all, binary search is an algorithm we might apply to find elements.
• We've used binary search on arrays, but are there other structures we can use it on? We might answer this by considering the operations binary search requires (and, therefore, the abstract data type specification).
• What operations do we need to do binary search?
• Grab the middle element.
• Grab the smaller elements: everything less than the middle element.
• Grab the larger elements: everything greater than the middle element.
• We might build a data structure (or at least an interface) that directly supports those three operations.
• Rephrasing binary search in terms of these operations,
```  public boolean binarySearch(BinarySearchable stuff,
Object findMe,
Comparator order)
{
if (stuff.isEmpty())
return false;
int compareVal = order.compare(findMe, stuff.middle());
if (compareVal == 0)
return true;
else if (compareVal < 0)
return binarySearch(stuff.smaller(), findMe, compare);
else ; if (compareVal > 0)
return binarySearch(stuff.larger(), findMe, compare);
} // binarySearch(BinarySearchable, Object, Comparator)
```
• We already know how to implement such structures using sorted arrays. An advantage of sorted arrays is that all the operations are O(log2n) (as long as we're cautious about how we represent these structures).
• A disadvantage of sorted arrays is that it is expensive to add values.

## Search Trees

• Another strategy for building searchable structures is to use search trees. Search trees use the basic idea of binary search, but encapsulate it in a particular way.
• As in the case of heaps, search trees apply the divide-and-conquer approach to the design of data structures.
• Can we also represent such structures as linked objects, using nodes?
• Yes, we'll give each node two links: one to a tree of smaller things and one to a tree of larger things.
• For example, we might build the following structure for the first seven letters of the alphabet.
```      D
/   \
B     F
/ \   / \
A   C E   G
```
• Similarly, here's a structure for the names of some faculty members.
```              Rebelsky
/        \
Ferguson      Stone
/     \         /    \
Chamberland  Herman   Shuman  Walker
/         /   \      \       \
Adelberg   Gum   Jepsen  Silkin  Wolf
\       /     /    \
Bishop French Hill  MooreE
\      \
Kornelson  MooreT
```
• Such structures are called binary search trees
• Binary: there are no more than two child links per node.
• Search: you use them for looking for things.
• Trees: They look somewhat like upside-down trees .
• How do we use search trees? The same way we use binary search.
• To find an element in a search tree, you compare the object you're looking for to the current node. If the object you're looking for is smaller, you follow the left branch. If the object you're looking for is bigger, choose the right branch.
• We can use a similar process to insert an element.
• You look at the current node.
• If the node is empty, you build a new node.
• If the node contains the appropriate key, you replace it.
• If the value to be inserted is larger, you recurse on the right subtree.
• If the value to be inserted is smaller, you recurse on the left subtree.
• If we can keep the tree relatively balanced, this gives an O(log2n) algorithm.
• In order to implement `Dictionary` with binary search trees, we'll build a wrapper class. This class will permit us to
• Represent empty collections.
• Keep track of the `Comparator` used to determine large and small.

Back to Dictionaries (1). On to Project Discussion.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 8 10:37:24 2004.
The source to the document was last modified on Thu Aug 26 20:22:24 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/Outlines/outline.38.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu