MCS 275 Spring 2024
Emily Dumas
Reminders and announcements:
Involves solutions to a maze with obstacles (a labyrinth)
Passing obstacles requires equipment, player has mutable inventory
You write several functions that answer questions (solution exists? need this item? etc.)
I provide specs, labyrinth data structure, and sample labyrinths.
Back to dendritic matters...
Tree-related examples are 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 search tree (BST) is a binary tree with keys that can be compared, such that for each node:
We'll require keys to be distinct. Equal keys can be handled in multiple ways.
This "just" is a binary tree with keys.
Node
class represents a node of a binary tree (and the entire subtree with that node as root)
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.