Wednesday, 2 March 2016

Week 7 - Abstract Data Types

This week, I thought it would be nice to dedicate a post to the abstract data types I have learned so far. Some of them, like stacks and queues, I have already discussed about and some, like trees, are new. Nevertheless, this post will be a reflection summarizing the structure of all the data types I have encountered in CS148, starting from the most recent.

Trees

This data type has a recursive structure since each of the sub-units of a tree are trees. A tree can be visualized as the upside down version of a tree we see in nature, that is, as a tree that starts with the root, splits into branches and ends with the leaves (fruits and flowers in the tree are not considered). The root or starting point of the ADT, tree, is the root node. The branches, which are like sub-trees, are called children. Finally, the parts of the tree which have no sub-units or no children, just like the leaves of a tree in nature, are called leaves.  Each unit of the tree, like roots, children and leaves, can be called nodes. A node is called an internal node if it has children and is called a leaf otherwise. The root is a leaf if it has no children and it is an internal node if it has at least one child. Each node in a tree is referred to exactly once. Just like in linked lists, the root node can be viewed as the whole tree or as the main node of the tree (or the tree’s starting node). Each tree may or may not have a value and may or may not have children. When the root is viewed as the starting node, it can be thought of like a linked list except where each node can have references to more than one nodes. This concept is best described with a diagram and hence, I have summarized this discussion in the diagram below.





Linked Lists

Linked lists are a list of nodes, where each node refers to exactly one other node or none. Each node has a value and may or may not have a reference to another node. They can be thought of as a nested list where a node has a reference to another node which in turn has a reference to another node, etc. In this approach, each node represents itself and all the other nodes after it. The second and more common approach is to think of them as a node pointing to another node. In this approach, each node only represents itself and what it refers to. In the second point of view, the linked list is implemented with a wrapper class which keeps track of the front and back nodes and the size of the list. The application of a linked list is the same as a list except linked list proves more efficient in most cases.

Stacks and Queues

Stacks can be thought of as a stack of objects. Each object gets added to the top (or end) of the list and the object in the top (or end) is removed first. Queues can be thought of as a line (or queue) of objects. In Queues, objects get added to the end of the list and objects are removed from the front of the list. Both of them can be implemented with the built-in list or a linked list but linked lists are more efficient in the case of Queues because when an object is removed from a list, the rest of the objects need to be moved over by one. This is not required in linked lists because in a linked list, one just needs to change the references of front and maybe back.

These are all the data types we have learned so far. Looking forward to learning more.


No comments:

Post a Comment