Sunday, October 13, 2013
Week Five
October 13, 2013
----------------------->
Topics: [Recursion and Trees]
When I saw the diagram of the trees in the slides as I walked into the lecture room, I was just itching for a smile. Doing a double major of Computer Science and Linguistics, I was well aware of the notion of the hierarchical tree and the concept of nodes and paths (or "branches" as we linguistics say), though in a more grammatical concept. While in CS I learned this week that there are the three different traversal types - pre-order, in-order, and post-order, the material was review in terms of being introduced to the tree structure. So when the directions of the trees were being shown on the slides I was able to follow them pretty easily. Left subtree, root, right; Left subtree, right, node, etc.
The only lingering questions which I thought was that how would recursion work in this way necessarily? For I was under the impression that recursion called inside functions were used in order to create a wanted value or outcome for an inner part of the given parameter. In terms of the given tree of numbered nodes, would the recursion be the repeated two's, in this case?
Other Notes:
This week I would say I came to better understood recursion through the labs that were required to write recursive functions. Recursive functions need a base case, and they generally need to pass an argument of a parameter that is not the full "object" itself. Otherwise, a maximum recursive depth error would occur.
ex. reverse_string(s: str).
......
return reverse_string(s)
vs.
for n in s:
return reverse_string(n) + ....
And another note is pertaining to the "one line" return statements. Punctuation of return statements are key - [] would return the code, whereas () would cause the code to generate a "generator object"
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line ?, in __main__.reverse_string
Failed example:
reverse_string('clock')
Expected:
'kcolc'
Got:
<generator object <genexpr> at 0x000000000310F870>
In terms of recursion concept, I think I have it fully grasped. All that is left is making sure I am able to properly put the concept into coding usage :3
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment