- 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
BinaryTreeNodeclass 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
BinarySearchTreeclass usingBinaryTreeNodeobjects 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 whetheritemis present in the treesearch(item)- return an item in the tree matching the givenitem, orNoneif not foundinsert(item)- insert the givenitemin order into the tree_find_node(item)- return the node containingitemin the tree, orNoneif not found (hint: implement this first)_find_parent_node(item)- return the parent of the node containingitem(or the parent of whereitemwould be if inserted) in the tree, orNoneif the tree is empty or has only a root node
- Run
python binarytree.pyto testBinarySearchTreeclass instance methods on a small example - Run
pytest binarytree_test.pyto run the binary tree unit tests and fix any failures - Write additional unit tests for the
BinaryTreeNodeandBinarySearchTreeclasses- Add to existing test cases to ensure the
sizeproperty is correct - Include test cases for the
heightinstance 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
BinarySearchTreeclass instance method:delete(item)- removeitemfrom the tree, if present, or else raiseValueError(hint: break this down into cases based on how many children the node containingitemhas 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)