- Tree data structure, terminology
- Binary search tree, operations
- Review Make School's trees slides
- Watch Make School's trees video lecture
- Read Interview Cake's logarithms and binary search article and binary tree properties article
- Watch HackerRank's trees and binary search tree video (up to 3:00)
- Watch Harvards's family trees and binary search tree video and stack frames video
- Play with VisuAlgo's interactive binary search tree visualization
- Implement
BinaryTreeNode
class with the following properties and instance methods using binary tree starter code:data
- the node's data elementleft
- the node's left child, orNone
(if it does not exist)right
- the node's right child, orNone
(if it does not exist)is_leaf
- check if the node is a leaf (an external node that has no children)is_branch
- check if the node is a branch (an internal node that has at least one child)height
- return the node's height (the number of edges on the longest downward path from the node to a descendant leaf node)
- Implement
BinarySearchTree
class usingBinaryTreeNode
objects with the following properties and instance methods using binary tree starter code:root
- the tree's root node, orNone
(if the tree is empty)size
- property that tracks the number of nodes in constant timeis_empty
- check if the tree is empty (has no nodes)height
- return the tree's height (the number of edges on the longest downward path from the tree's root node to a descendant leaf node)contains(item)
- return a boolean indicating whetheritem
is present in the treesearch(item)
- return an item in the tree matching the givenitem
, orNone
if not foundinsert(item)
- insert the givenitem
in order into the tree_find_node(item)
- return the node containingitem
in the tree, orNone
if not found (hint: implement this first)_find_parent_node(item)
- return the parent of the node containingitem
(or the parent of whereitem
would be if inserted) in the tree, orNone
if the tree is empty or has only a root node
- Run
python binarytree.py
to testBinarySearchTree
class instance methods on a small example - Run
pytest binarytree_test.py
to run the binary tree unit tests and fix any failures - Write additional unit tests for the
BinaryTreeNode
andBinarySearchTree
classes- Add to existing test cases to ensure the
size
property is correct - Include test cases for the
height
instance method on both classes
- Add to existing test cases to ensure the
- Annotate class instance methods with complexity analysis of running time
- Implement this additional
BinarySearchTree
class instance method:delete(item)
- removeitem
from the tree, if present, or else raiseValueError
(hint: break this down into cases based on how many children the node containingitem
has and implement helper methods for subtasks of the more complex cases)
- Implement binary search tree with singly linked list nodes (having only one link to another node) instead of binary tree nodes (having two links to other nodes)