Sunday, 1 March 2015

SLOG #5 RECURSION, AND ABSTRACT DATA TYPES

Time flies; half of the semester has past. I think I am ready to give a summary to what I have learnt in CSC148 for the past 6 weeks. Object-Oriented Programming, recursion, and abstract data types (ongoing) are the main topics of this course so far. Since I have already mentioned OOP in a previous slog, and since I now feel quite comfortable with implementing classes, I would talk (more) about  recursion and abstract data types in this slog.
Recursion:
The defining properties of recursive functions are the following:
  1. The function is well defined at a base case or cases 
  2. When the function is called at a point (usually a number) that is different from the base case, the function refers to the value of itself at the previous number. This process continues until the function hits the base case.
It is not hard to see that the function is well defined on all values starting from the base case (usually natural numbers), since for each number it gives the computer an algorithm or "roadmap' to instruct it what to do. One advantage of recursion is that the programer only needs to do two things: define the function at a base value, and tell the function to refer to its previous value when given a number that is not the base value. Sometimes this feature of recursion can be significantly more convenient (although it may take the computer a very long time to trace back to the base case). For example, with recursion, it is very easy to define factorial, or to evaluate the nth term of Fibonacci sequence. On the contrary, using closed form formulas for the above problems might be much faster, but they are quite complicated: the closed form of factorial involves gamma function, where we need to numerically evaluate an improper integral; and the formula for Fibonacci sequence has exponential terms; both of which will return a float instead of int. Hence when input size is large, floats from closed formulas might be far from the real values.

Another advantage of recursion will lead to my second topic today: Abstract Data Types. Among the ADTs we are learning are stacks. queues, and trees. I view ADTs as mathematical representations/models for certain objects that we might be interested in. For example, the picture below shows how a binary tree represents an arithmetic expression:
The structure of trees makes recursion an important tool when we want to implement methods/functions on this data type.
Last week, we began to study binary tree. As its name suggests, nodes in binary trees have as most two children. Unlike general trees which store their children in a list, binary tree nodes store their children separately as two instance variables. The leaves of a general trees have child empty list, while the leaves of binary trees have child None. This distinction makes the implementation of recursive functions on binary trees different: for general trees, we start with a base case where the tree has height 1, but for binary trees, we start with a base case where the binary tree is None. This prevent us from calling incorrect methods on NoneType. The exercise in this week's lab helped me understand binary tree more clearly.
As to assignment 2, I find implementing tippy game state not very hard, since I have been practicing more on OOP, and since the starter code provide a paradigm that I can refer to. We are required to implement a recursive function in strategy minimax. Given the practice in lab #5, this did not cause me much trouble.
In addition, I found the slog of this fellow CSC148 student from last year quite helpful and inspiring:
Please do leave a comment if you are reading this post.

No comments:

Post a Comment