In computer programming, recursion is the act of calling upon a function itself. Recursion, in python, may be a difficult concept to grasp at first as it may be replaced by iteration, but it is quite useful as it provides a concise and organized solution to certain problems.
Recursion is simple to use in basic functions such as a function that returns the sum of values in lists but it is also very useful for binary trees. For example, let's examine a code from class;
From personal experience, I found recursion a reasonable concept to understand when tracing code and completing basic programming examples. I found recursion difficult once I had to use it in code, specifically when I had to implement a function called parenthesize, which parenthesizes the expression rooted at "b", where "b" is a binary tree. The parenthesize function is shown below.
While I found parenthesize difficult at first, I found that using the following steps helped:
1. First, find a simple example representing an expression that does not require recursion (This will probably be the "if" statement).
2. Next, find an example (not as simple as in the first step) that may be solved by using recursion (This will probably the "else" statement).
Combining these statements into proper python coding will, most likely, give the desired result.
By using these steps, and thinking through the recursion in the second step, I was able to solve the parenthesize problem.
Overall, recursion is a necessary skill for program, that may be difficult at times, but useful.
Please feel free to leave a comment!
Until next time, practice recursively!
Recursion is simple to use in basic functions such as a function that returns the sum of values in lists but it is also very useful for binary trees. For example, let's examine a code from class;
def contains(t, v):
''' (Tree, v) -> bool
Return whether Tree t contains v.
>>> t = Tree(17)
>>> contains(t, 17)
True
>>> t = descendents_from_list(Tree(19), [1, 2, 3, 4, 5, 6, 7], 3)
>>> contains(t, 5)
True
>>> contains(t, 18)
False
'''
if t.value == v:
return True
else:
return True in [contains(c, v) for c in t.children]
The solution to this code for "if", takes the "base case", which is an expression that does not require solution thus making it very simple. The "else" part of the code is the difficult aspect as it requires recursion. In this example, since the function takes two arguments, the recursive function must also take two arguments in order to work properly. While, this seems like a simple code to implement, it is much easier to understand the code when it is given than when one must write the recursive code. Practice is key!
From personal experience, I found recursion a reasonable concept to understand when tracing code and completing basic programming examples. I found recursion difficult once I had to use it in code, specifically when I had to implement a function called parenthesize, which parenthesizes the expression rooted at "b", where "b" is a binary tree. The parenthesize function is shown below.
def parenthesize(b):
''' (BTNode) -> str
Parenthesize the expression rooted at b, so that float data is not parenthesized,
but each pair of expressions joined by an operator are parenthesized.
Assume: -- b is a binary tree
-- interior nodes contain data in {'+', '-', '*', '/'}
-- interior nodes always have two children
-- leaves contain float data
>>> b = BTNode(3.0)
>>> print(parenthesize(b))
3.0
>>> b = BTNode('+', BTNode('*', BTNode(3.0), BTNode(4.0)), BTNode(7.0))
>>> print(parenthesize(b))
((3.0*4.0)+7.0)
'''
While I found parenthesize difficult at first, I found that using the following steps helped:
1. First, find a simple example representing an expression that does not require recursion (This will probably be the "if" statement).
2. Next, find an example (not as simple as in the first step) that may be solved by using recursion (This will probably the "else" statement).
Combining these statements into proper python coding will, most likely, give the desired result.
By using these steps, and thinking through the recursion in the second step, I was able to solve the parenthesize problem.
Overall, recursion is a necessary skill for program, that may be difficult at times, but useful.
Please feel free to leave a comment!
Until next time, practice recursively!
No comments:
Post a Comment