Thursday, 12 March 2015

Week 7: Abstract Data Types

A couple years ago, prior to studying CS, I took a statistics course which contained a particularly outspoken CS major. During one class I heard him say to an acquaintance how in CS he had encountered some extremely abstract material. This comment really caught my attention. Studying abstract topics is something I really enjoy, but at that time I had no knowledge of CS or of programming in general so I wasn't sure what he meant. Now that I have some experience with abstract data types I have a much better understanding of what he was getting at.

There are an indefinite number of ways of storing data and accessing data. Many of these are extremely useful and I find it amazing that it's possible to instantiate their structure by using a programming language. These ADTs end up being very versatile tools in a programmer's arsenal, even ones as simple as a queue.

For lab #5 (trees) we were asked to implement a method which returns a list of all values contained in the nodes of a given tree. At the time I felt that the most natural way to traverse the nodes was by using what I later found out is called a breadth-first search (http://en.wikipedia.org/wiki/Breadth-first_search). I knew that I could use recursion to easily implement the method but this would return a list with the values in a different order and for some reason I had my heart set on a breadth-first approach. Recursion didn't seem that suitable so I spent some time considering how I might solve this problem. Before too long I realized that I can add the root to a queue and while the queue isn't empty, repeat the process of dequeue-ing whatever is at the front, adding its value to the list to be returned and then enqueue-ing all of its children. This algorithm ended up producing a list of values in the order I wanted.

If I had no knowledge of the queue ADT I either wouldn't have been able to solve this problem or my solution would have been significantly more complicated. Different situations call for data to be structured in different ways. In this case, having knowledge of a first in, first out approach to organizing and accessing data is what lead me to a clean, straightforward solution to this problem.

2 comments:

  1. I always thought stack and queue are sort of the first basic abstract data structure in a series of learning other data structures such as tree, binary tree, etc. I thought it was neat that you were able to use stack and queue to solve a tree problem (kind of like ADT within another ADT, eh?). Also, it's interesting that you were able to find the connection between CSC and Statistics; considering a Statistics course is a prerequisite for CSC.

    ReplyDelete
  2. Thanks for your feedback. I really like the idea of combining ADTs to get a new or improved ADT. I found the linked list implementation of a queue that we were shown in lecture to be particularly interesting.

    ReplyDelete