Sunday, 1 March 2015

Abstract data types (week#7)

From wikipedia, 'An abstract data type (a.k.a. ADT) is a mathematical model for a certain class of data structure of one or more programming languages that have similar semantics.' It focuses on semantics, which is the meaning of the object, and operations on the data instead of how to implement it in detail. It doesn't care about which programming language to use.

There are two benefits of ADT, from our course notes:
1. We can think about the algorithms and recipes freely without including all the details of how our objects are implemented.
2. Details of how objects and their components are stored and accessed vary  between programming languages, whereas a very good algorithm can be translated into any programming language.

So far, we have learned 3 ADTs. They are stack, queue and tree. I'll give a brief summaries of them next.

1. stack:

A stack is used to store data. There are 3 basic operations on stack:

pop: remove and return the top item on the stack(if it's not empty)
push: add an item on the top of the stack
is_empty: check whether the stack is empty

As can be seen in the 3 operations, it pops from the top and adds on the top as well, which is called 'last-in-first-out' (a.k.a. LIFO).




2. queue:

Very similar to stack, queue is also used for storing data, however, the basic operations of queue is different from stack.

enqueue: add an item at the back of the queue
dequeue: remove the item that is in the front of the queue
is_empty: check if there are items in queue

Queue ADT follows the rule 'first-in-first-out' (FIFO), which contradicts to the stacks'.



3. tree:

As the use of stack and queue, tree also stores data, but it stores data in a hierarchical order. 

(From the lecture slides)
A tree is a set of nodes with directed edges between some pair of nodes.
An edge connects a parent node to a child node.
Nodes may contain values.
The beginning node without a parent is the root.
A node without children is called leaf.



2 comments: