CS 66: Introduction to Computer Science II
Problem Solving with Algorithms and Data Structures using Python
Sections 7.2 - 7.6, 7.11 - 7.14: https://runestone.academy/runestone/books/published/pythonds/Trees/toctree.html
A tree data structure is a collection of nodes that express a hierarchical relationship.
You can think of it like a linked list, but each node may be linked to more than one other node.
Node: the circles - may contain a key and other information
Edge: arrow representing relationship between two nodes
Parent/Child: the edge goes out from the parent node and into the child node. (Mammal is the parent of Carnivora, Carnivora is the child of Mammal)
Root: node with no parent (Animalia)
Leaf: nodee with no children (Human, Chimpanzee, etc.)
A Binary Search Tree is a tree that allows a search operation to be done similar to a binary search.
Like hash tables, they are commonly used to implement the set and map abstract data types.
We will work through the implementation of a set - remember that a set is a collection of values with no duplicates and no implied ordering.
Set operations: add, search, remove
BST Nodes have at most 2 children, called left child and right child
Nodes in left subtree have value less than this node
Nodes in right subtree have value more than this node
This is true for every node!
Here's what our Linked List node looked like:
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
Design a node for a binary search tree
class BSTNode:
def __init__(self,initdata):
#fill in the rest
#add any methods that you think will be helpful
#we could create a new node like this
mynode = BSTNode(42)
BSTs allow for good use of recursion
We can create an add
method right inside the BST Node class
Here's the pseudocode
Implement the add
method in the BST Node class using this algorithm.
Discuss: Is it possible to create an empty tree with the class we've written?
#put this method into BSTNode class
def add(self,new_val):
#implement the code here
Discuss: Is it possible to create an empty tree with the class we've written?
Let's now create a class to represent a whole tree made up of BST Nodes
BSTNode
classBSTNode
methodsclass BSTSet:
def __init__(self):
self.__root = None #start with an empty tree!
def add(self,new_val):
if self.__root == None:
self.__root = BSTNode(new_val)
else:
self.__root.add(new_val)
myset = BSTSet()
myset.add(4)
myset.add(7)
myset.add(2)
myset.add(9)
Implement a search
method in the BSTNode
class - again use recursion.
Write the associated method in BSTSet
(suggestion: call it __contains__
in BSTSet
so that you can use it with the in
operator.
myset = BSTSet()
myset.add(4)
myset.add(7)
myset.add(2)
myset.add(9)
print( 7 in myset ) #should be True
print( 1 in myset ) #should be False
What would the tree look like in a worst-case search? So then what is the computational complexity of your search? What about add
?
After you a good guess, check out https://runestone.academy/ns/books/published/pythonds/Trees/SearchTreeAnalysis.html to see if you agree with the book.
Removing nodes is the most difficult part.
Three cases to consider: