This is the second time I write the
blog that is about Recursion. Throughout the continuous learning according to
this topic, I am more familiar with it. Besides knowing what it is, I learn how
to use it now.
A1 gives us the opportunity for
practicing of using recursion. We need to solve the challenging problem by
applying the structure of recursion. Also, it is very helpful to work with
classmates. We can help each other on working it.
In
addition, “Tree” is the good example. Form the structure of Tree, it is really
like a family. There is a root, which is also the original parent and its
children also have their children. In the methods of Tree, all returns are only
one line owing to comprehensive and they clearly show the abstract solution. For
example, count is the method for counting the nodes in the Tree. The function
is “return 1 + sum([count(n) for n in t.children]). It counts all the parents
and leafs. If the n is also the parents, it needs to call the function itself.
Also, we could use the structure of Tree to do the calculation: preorder,
inorder and postorder.
Recursion is very important. The main
benefits are these two: firstly, the program using recursion functions is
obviously clearer, simpler, shorter, and easier to understand. Secondly, it
directly reflects the algorithm. All in all, I prefer recursion to iteration.
No comments:
Post a Comment