Recursion:
The defining properties of recursive functions are the following:
- The function is well defined at a base case or cases
- 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.
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