Week 10 Day 1 - BSTs Part II
Today we went over the competency exams for classes and linked lists, and 20 minutes in started talking again about BSTs.
First, we talked about destructors. Destructing a tree is pretty easy. Call delete on the root node, and then have the destructor on the root call delete on its left and right child. If the pointer is null, C++ will ignore the delete, so you don't even need to check for null before deleting it.
Second, I wrote a find() function that would return true or false based on if the key we're searching for is in the data structure. Find (and most BST functions) require the less-than operator to be defined, but in our example here I'm using the spaceship operator to generate all the different comparison operators (other than ==, which was odd). Using the default spaceship operator (= default) works even better, BTW.
Finally, we started in on deleting from a BST, which is the most annoying part since there's a bunch of special cases that have to be handled.