Reading week is over and we are back to class.
We talked about binary trees this week, which is basically a special type of tree which has at most two children. As such it'd be inefficient to organize the children of binary tree nodes into lists, and using two attributes for the "left child" and the "right child" for each node seems like a good idea.
Binary trees, with their special property, seem useful for representing and evaluating binary expressions, as we can describe them using such trees with each leaf as a value to be operated upon, and two leaves' common parent as the operator to perform on the left and right leaves. This way, by recursively going down the tree, joining the operators and values, we would be able to easily obtain the multilayer binary expression represented by the tree and evaluate it.
This concept is also helpful for searching for values, as an ordered binary tree (where the left child of a node is always less than the value of the node itself which is in turns less than the right child of the node) can direct the search to one path, so that we don't have to examine the nodes that are, by the orderedness of the tree, clearly not the value we are looking for. This means we can halve the search scope every step we go down the tree, so it takes at most lg n steps to search a tree with n nodes (meaning the worst case performance is O(lg n)), which seems much more efficient than a linear search where the worst case performance is O(n) steps.
Assignment 2 is going okay. I finished the tippy game and am working on minimax. I've found myself scrapping all my work and restarting more than once during this assignment though. I'm not sure if that's a good sign or not.
We are supposed to give a summary for recursion, object-oriented programming, or abstract data types for this week. I've already done the first two, but I think I'll add a few more words to the recursion one, as it seems that the TA didn't like it--I'm honestly not sure what the TA is looking for though. I'll just give a general summary of how it works.
What we've been working on for recursion is basically defining the function's behavior on a base case where everything else can be reduced to such a case, and by calling the function itself within the code for the function, reduce other more complicated cases to the base case. I once heard someone saying recursion can be explained as "an onion is an onion skin with an onion inside". We don't particularly care about how many layers of skin the onion has (well, as long as it's finite), as recursion does the work for us, we only need to define exactly how the function peels the onion skin, how the function operates on the core of the onion, and how the function reconstruct the layers of onion skin back into an onion, or into whatever we want it to be.
For example, a function f that operates on objects in lists can be applied to objects in nested lists using something as shown below:
def g(l):
if isinstance(l, list): # when the object operated upon is a list
return [g(i) for i in l] # reduce to a list of one less layer
else: # base case
return f(i) # f is the function directly applied onto the objects in the list
Here, the function g is an application of recursion, which reduces nested lists to nested lists with less layers and eventually to non-list objects which it passes to f. The reconstruction of the onion here is just putting the onion skins (list brackets) back up. So the outcome is the original list with each non-list object in it being operated upon by f.
We can do something else with the reduced case though, say if we change g into:
def g(l):
if isinstance(l, list):
return max([g(i) for i in l])
else:
return f(i)
the outcome would be the maximum value of the function f applied to each non-list objects in the list. Of course, here f has to be some function that outputs a string or a numerical value.
I've talked about applications of recursion, and as the binary tree example discussed above is also an case of the applications of recursion, I suppose there's no need to restate how useful it is.
No comments:
Post a Comment