2014年2月22日星期六

Recursion

These weeks, I have learned a lot about recursion function.A recursion function is a function inside calling itself repeatedly until a desired value is achieved.
I found recursion is so abstract that it is the most difficult concept for me to master.

The Turtle and the nested number list are two examples of recursive function. To understand how they work, one way is to assume the function works correctly when we call it for an order 0 level, then we just need to focus on order 1 level. If they work properly, we take it to the next level. It means we can trace the recursive function every recursion it runs.This method is really helpful for me to understand how recursive functions work. Moreover, the basic structure of recursive function is usually if statement ( if: ... else:), and the function is repeated in else code unless it reach a point or value which satisfy the requirement in if code.

In my Assignment 1, recursive function is the key step to reduce steps that moves cheeses from source to destination. In the lecture, I knew the code for tower of hanoi, and this code is the basic code for move cheese function. I choosed i according to the given function, and moved n-i, i and n-i cheeses using tower of hanoi separately. Finally, I calculate the total steps which is much less than n^2 -1.

This week, we were introduced a new data structure called Tree. A common kind of tree is binary tree, like a family tree, its top node is called parent, and the nodes it refers to are its children.Tree are defined recursively like other recursive functions. The process of assembling a tree is similar to the process of assembling a linked list. There are three traversal methods pre-order traversal, in-order traversal and post-order traversal.First print the root, then print the entire left subtree, and then print the entire right subtree. This way of traversing a tree is called a preorder, conversly,in-order is to print the left tree, root and then the right tree.Post-order is from left tree, right tree to the roots.

I acquired a lot of knowledge about the recursive function, and I am getting used to using it.    
            
  

没有评论:

发表评论