In last few weeks, I have learn a lot of things about recursion. Recursion means "defining something in terms of itself" usually at some smaller scale, perhaps multiple times, to achieve your objective. For example, we might say"A human being is someone whose mother is a human being" or "a family tree starts with a couple who have children, each with their own family subtrees.In general, recursion is a process of calling a function that is already executing. Firstly, we should include a base case which is a branch of the conditional statement in a recursive function that does not give rise to further recursive calls. So the base case is not recursive. We also should care the infinite recursion. When this happened, the function call itself recursively without ever reaching any base case. Eventually, infinite recursion causes a maximum recursion error in python.
In the lecture, professor show us two kinds of recursive data structure. One is nested list. Nested list is a list whose elements are either number or nested number lists. We have define some function for the nested list like summing the number is the nested list. For this function, we first need to think of base case, the do recursion for each nested list in nested list. So we sum each element is each nested list and then add them together. I also learn other methods about nested list like find the maximum depth. The second kind of recursive structure is trees. Trees are made up of nodes. A common kind of tree is a binary tree, in which each node contains a reference to two other nodes. The nodes is possible None. I think this is a advanced structure of nested list. The tree is a ADT and we use recursion under the object-oriented programming. We learn how to use recursion to find the height and the number of leafs of the tree, and represent the tree in different order by the recursion.
For the recursion, the professor give us a big assignment about towel of hanoi. A three stools hanoi is a very famous problem. But we need to solve the problem with four stools. That is a big challenge for me at first. After the hint from professor about the three stool, I get the idea of the recursion of four stools. At that time, I get a deep understand of how to define a recursion function.
In short, I think recursion is very helpful for some very complex programming. And the recursion is also use in mathematics. And I will spend more time on learning recursion.
I still remember we found lots of paper about Tower of Hanoi when doing A1.
回复删除