Ah, BSTs. BSTs are great. They're already organized so there are less cases to code for (yes I'm lazy, don't judge me), and searching them has a runtime of O(logn), how nice.
What exactly are BSTs? They're basically binary trees from all the way back in week 6 with one more pre-condition - in each node, the value of the root is greater than the left child and less than the right child. Let's look at some visual examples of BSTs, again with my
The actual python implementation for BSTs is the same as normal binary trees, but we add the above stated pre-condition.
Note that the BST is actually a wrapper function of BSTNode so any methods made for BSTs need to be made as module level functions. (See the insert function example from lecture).


No comments:
Post a Comment