This type of tree holds a distinct advantage in search for specified values. Say for example, in reference to the picture, we are attempting to search for 18. in this case, once we look at the root 5 we can immediately come to the conclusion that 18 must be in the right subtree (as left<root<right). Essentially, we have cut our searching time in half, as we can neglect to look through the left subtree.
In terms of efficiency, binary search is type O(lg n), while a more linear search through all elements yields O(n). Over large sets of data, lgn < n, so we conclude it is more efficient.
Lab08
This weeks lab was personally, one of the more challenging ones. The (main)focus of this lab was to implement the method count_less in a BST class. We had to implement it recursive in 3 different coding styles, and then iteratively. For me, I tend to prefer a single style of coding and stick with it. So while one recursive solution came easily, the others took a little more thought. I can definitely see the advantage of knowing multiple ways of approaching a problem though, as compared to my more single-minded approach.
Exercise03
This exercise was very useful for me, as a way of better understanding trees. After doing parts a and b, I feel like a have a much stronger grasp on tree sorting, tree representations, and of course, I got practice on recursion. The surprising thing I found out of this was how often I seem to neglect base cases/simple cases. Especially in part B, while my code worked flawlessly for large data sets, it buckled under simple examples of None. Of course, I was able to fix it by the next auto test, but it opened my eyes as to not just testing for advanced circumstance, but also the simple (which I had previously assumed would obviously work).
No comments:
Post a Comment