In the lab, one of the main concepts to understand this week was defining a function inside another function which does most of the work you want your outer function to do. We used this for BST and BSTNodes. We were asked to write a method count_less for BST, inside which we had to define a helper function. The purpose of the helper function was to do the recursion that BST couldn't do. The helper function had the same function as BST, the only difference was that it took in a BSTNode as a parameter. That way this function could go through the nodes in BST recursively and count the nodes less than a given value. BST couldn't do this recursion because it is a method for a tree not a node, so the helper function did all the work and count_less just called the helper function to return its value. This is an important concept that I think we will see in future labs and programming.
In class we learned about Big-Oh, however just a few weeks before I had learned about it in CSC165. It is a good way of measuring a function's run time, but an even better way is Big-Theta (which I just recently learned about), so I hope we cover that in 148 before the end of the course. The advantage of Big-Theta is that it gives a function that provides both a lower and upper bound (based on your choice of constant) for the runtime.
E3 was really fun to do. I find these exercises (and assignments) are just the right amount of difficulty: they are challenging but not impossible, which makes them interesting. I found the hint in the handout for the second function to be misleading and it made me go in a direction that was way more complicated than needed (all I'll say is that too many tuples were involved). However when I tried not to follow the handout's hint and do it my own way, I found there was a much more straightforward and intuitive way to write the functions. I wrote a helper function to determine the depth of a child, and then my main function would recursively check which child was deeper and then go find the sum there. Although it was different from their suggestion it still passed all the tests!
Good luck with A2 part 2 everyone!
No comments:
Post a Comment