BST + Recursion = Happy
This week's lab was the very first lab in which my partner and I had to stay till the very end and still didn't finish. Recursion makes life so much easier! I remember in the first week when Danny was jokingly saying that by the end of the semester, we would all want to always use recursion on every problem. Well after this lab, I have to say that I have been converted to recursionism (well, to some extent). Implementing the count_less() function iteratively actually required a significant amount of work whereas the recursion solution was easy as 3.1415... (a little math humor :D)
I'm also starting to grow fond of BTSs and just trees in general. Off the top of my head, with binary search trees we can:
- Search in O(lgN) time (well, if it's fairly balanced)
- Find the minimum
- Find the maximum
- Find the inorder successor
- Find the inorder predecessor
- Do Huffman encoding by using a BST to get prefix-free binary strings
- And so many more things (as we've been seeing in class, exercises, labs, and assignment 2)...
- There are different ways to balance a binary search tree:
- AVL trees
- Red & Black trees
- And there are many more kinds of trees, each tailored for a specific purpose:
- Radix trees: useful for constructing dictionaries with keys that can be expressed as strings
- There are prefix trees, suffix trees, and there are even trees to sub in for hash tables!
