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

Held: Tuesday, April 11, 2006

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

Related Pages:

Notes:

• Are there questions on the exam? Please keep them short today.
• Talk at 4 p.m. EC.

Overview:

• Binary search trees.
• BSTs and dictionaries.

## Search Trees

• Another strategy for building dictionaries 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
/        \
Kuiper       Stone
/     \         /    \
Chamberland  MooreE  Shuman  Walker
/   \      \       \
French MooreT  Silkin  Wolf
\
Kornelson
```
• 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.

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 Tue May 9 08:31:48 2006.
The source to the document was last modified on Thu Jan 12 14:58:07 2006.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Outlines/outline.38.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu