Binary Search Trees, Sets, and Maps

This lecture covers Binary Search Trees (BSTs), which are a special kind of binary tree that maintains a sorting invariant on its elements. It combines the advantages of a sorted array (i.e. fast lookup with binary search) with the flexibility of a linked list (i.e. efficient insert/remove operations anywhere in the data structure).

BSTs are the foundation of "industry-strength" implementations of several application-oriented data structures, including sets and maps. We'll particularly focus on maps, since we haven't discussed them previously and they are a key part of project 5 in EECS 280.


1: Intro to Binary Search Trees

In essence, a binary search tree is a regular binary tree where each node's left subtree contains lower values and right subtree contains higher values. Let's take a look at some specifics as well as the reason this leads to efficient performance.




2: Recursive Functions on BSTs
2.1 Not Started

We can work with binary search trees using a similar recursive approach to regular binary trees, except that we can make an informed decision about which subtree to explore based on the sorting invariant.


2.1 Exercise: bst_contains()

Implement the bst_contains() function, introduced at the end of the video above.


You're welcome to check your solution with this walkthrough video.




3: BST Implementation on Project 6

It's also worth a bit of time to take a look at how a binary search tree could be realized as a C++ class, including some of the specifics for the implementation you'll work with in EECS 280 project 6. (Sorry - I recorded this video last year, when it was project 5.)




4: Building a Set on a BST

Now, let's turn to some useful data structures implemented via a binary search tree. First, we'll consider a BST-based set, which will outperform the other array-based implementations we've seen previously. (Please ignore the references to the classifier application where you use a map… you've already done that this term in project 4.)




5: Building a Map on a BST

In this video, we'll introduce the idea of a map as an associative container where we can store and retrieve values according to a particular key. Then we'll look at the fundamental approach we could use to implement a map with a BST.




6: Map Implementation

Finally, a few practical tips and tricks for Map.h from project 6.

Let's take a tour of each component in the Map class, including the BST member variable (i.e. the "has-a" pattern), the template parameters, and a custom comparator to compare key-value pairs in the BST based on the keys only.


Here's also an overview of what each of the three main Map functions (find, insert, and operator[]) should do. You'll use each in various places thoughout the project 6 driver.


You've reached the end of this lecture! Your work on any exercises will be saved if you re-open this page in the same web browser.

Participation Credit
Make sure to sign in to the page, complete each of the exercises, and double check the participation indicator at the top left of this page to ensure you've earned credit.