I must say that BST is really a great invention derived from a regular binary tree. It's been very beautifully constructed since every value goes into a proper position, which saves a great amount of time when we want to find an item or insert one. So a BST, i.e a Binary Search Tree, is a ordered tree such that the left subtree of any node contains only keys that are less than the node's key and at the same time the right subtree of any node contains only keys that are greater than the node's key. Thus, there should't be duplicated nodes. What's more, the left and right subtree of a node must be BST as well. So you can see that everything in a BSTree is already sorted, which is always good since it's neat and clear and we can do lots of stuff with this great property. Also we derived three kinds of traversal out of a BST: in-order, pre-order and post-order. Each of them gives us a "sorted" list representing a BST. Using recursive, codes become neat and clear since when you run it by hand, a sort of pattern revealed itself, that's way I love these traversals.
The BSTNode, just like LLNode, represents the nodes in a BST. We can implements classes in different ways, which was exactly what we were working with in the lab and it was kinda frustrating, I figured I had too little practice on recursive functions. Things don't seem hard at first sight but I just can't working around comfortable with them. I guess I have to work harder and not try to remember patterns, as I always did.
No comments:
Post a Comment