MCS 275 Spring 2023
Emily Dumas
Reminders and announcements:
Tree-related examples will go in the new directory datastructures in the course sample code repository.
Learn about search and insert operations on binary search trees.
Implement in Python.
Explore 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_node)
.
Challenge: Read the source of treevis
and
figure out how it works!
Now let's build a subclass of Node
to represent a BST.
Desired features:
Given x
, find and return a node with key x
. Return None
if no
such node exists.
Given a key, add a node to the tree with that key, maintaining the BST property.
Let's use this to build a class to store a collection of integers that supports fast insertion and membership testing.
IntegerSet
has many possible implementations (e.g. a list, a tree, ...), and a user of
the class doesn't need to know about which one it uses.