Monday, 16 February 2015

SLOG #4 STACKS, QUEUES, AND TREES

Although the prescribed topic of this week's slog is a summary of Object-Oriented Programming concepts, I will mainly talk about the material I learnt from last week and this week's labs since I have mentioned OOP in a previous slog.

I found what I have learnt in the past two labs particularly helpful, partly because Diane only mentioned the abstract data types (ADTs): stacks, queues, and trees briefly in class. In the labs, we were required to read through the codes of these data structures and write methods or functions for them, which gave me a more solid understanding on how they work and how we can make use of them. We also learnt some applications of these ADTs. For example, we can use a queue to represent a queue in a restaurant, or we can use a tree to trace the order of a sequence of binary operations.

The materials in lab #4 is not very difficult, but it becomes quite tricky when I tried to implement recursive functions on trees in lab #5. It is easy to think of a base case(when the problem can be solved in one single step). In code, this is usually the "if" part. However, I got lost when I started trying to write something in the "else" part. Fortunately, the lab handout gave me some hints; it asked me look at a larger tree. Assuming that the function works on its subtrees, what should I do to make the function also work on this larger tree? This is very similar to the induction step in an induction. In induction, you prove the base case, assume the statement holds for all numbers smaller than a number k, and prove the statement also holds for k, using the above facts. In induction, we know our function works for the base case, we assume the function works for any level of input smaller than a certain number k(in this is it is the height of a tree), and we construct the function such that it will work for level of input k. Thinking in this way, the problems in the lab were greatly simplified; all I needed to do was to think of base case, and design function so that it works for trees with height two. Thanks to recursion, the subtrees collapsed to the leaves that are directly connected to the root!

Reading week is coming, so is assignment 2. Hopefully I will be able to have a draft for A2 by the end of this week.

No comments:

Post a Comment