Sunday, 1 March 2015

Abstract Data Types (ADT)

In this SLOG, I'll be talking about two structures. Trees and Stacks.

A tree (the data structure) looks like an upside down tree (the biological one). It starts off with the root and then it branches off. The last nodes that don't contain any other nodes are called leaves. There is only one way (path) to get to a specific node and paths cannot go in a circle. Also, two different nodes cannot have a path to the same node. This means that a node's children are exclusively its own children. These structures are levels of organization like XML and html. Recursive functions are also important in the case of trees because there are so many levels, it takes recursion to go through the organization. A tree can have lots of children but there is a catch. All trees have arity. This means the maximum number of children a node can have. This is important because if a tree doesn't have arity, the root would have all the children and it would just be a tree with a height of 2.

Stacks on the other hand are kind of like lists except you can only take off elements one at a time. If you imagine a stack of books, you know what the book at the top is but not the books underneath it. You can put a new book on top or even take the top one off but you can't put a book in the middle of the stack or take one from the middle because then the stack falls apart. The ADT Stack works in the same way. You can pop off the top element, you can push an element on to the top of the stack but you cannot take anything or put anything in the middle of the stack. The only way to see what is in the stack would be to keep popping off elements until the stack is empty. Stacks also have the method is_empty which returns a boolean to answer if the stack is empty or not. This helps when you pop off because while the stack is not empty, you keep popping elements. This is to make sure there is no error or infinity loop. You need to make sure that no more elements are being popped when the stack is empty.

Both of these structures are very helpful. We saw a similar structure to the stack in lab 4. We learned about enqueue and dequeue. They work in a similar way except we pop off the beginning of the list and push to the end. Like how any fast food order would take place. People who were there first would be served first and new people will be lined up in the back. This is different from stacks as people who were pushed lasted would be poped first.

I didn't have too much trouble with these ADTs. The lab really helped me understand them. They are valuable ways to store data. Which data type one picks really depends on how they want their data to be accessed.

No comments:

Post a Comment