MCS 275 Spring 2021
Emily Dumas
Course bulletins:
I've created a new directory trees in the course sample code repository.
Live coding examples from the next couple of lectures will be added there.
Learn about search and insert operations on binary search trees.
Explore an application to a fast data structure for storing a set of integers.
A binary tree in which:
This "just" is a binary tree with keys.
Let's build a class to represent nodes of a binary tree that also store keys.
I provide a module treevis
in the sample code repository that can "pretty print" a tree with the function treeprint(root)
.
Challenge: Read the source of treevis
and figure out how it works!
Now let's build a class that builds and manages a BST made of Node
objects.
Desired features:
Given x
, find and return a node with key x
. Return None
if no such node exists.
Take a Node
object and add it to the tree in a place that maintains the BST property.
The node must have its key
set, but not its parent and children. Insert will handle those.
Let's use this to build a class to store a collection of integers that supports fast insertion and membership testing.
To use BST
, you need to know about and work with Node
objects.
In contrast, IntegerSet
has an interface based directly on the values to be stored. It hides the fact that its implementation uses a BST.