Friday, April 17, 2015

The remaining Weeks 6 to 12 and last thoughts

It has been a while and we will be combining all of what we've learnt so far, we will be talking about the different contents learnt throughout the weeks as well as listing all ADTs (Abstract Data Types) we've used along with a final impression of the course.

Week 6-7
Week 6 has been my introduction to trees, although I did not entirely comprehend the concept of a tree due the limited time after the test we had, with time I have finally been able to grasp the idea behind it.
A tree is an abstract data type that has two important parts: the root and the branches. It would be rather referred to as "value" and "children" in this case. Let us explain what each of these are in the case of the regular Tree class.
Each instance of a tree is called a node, a node is the entity that has both value and children.
The "value" is the data contained within the node, it can be any type of data.
Children, in the case of the regular tree refers to a list containing all sub-nodes or sub-trees attached to the said tree.

Creating a tree is really easy in Python.
Suppose the Tree class had already been imported from the tree module in the following way:

>>> from tree import Tree
>>> sometree = Tree()
In that case we would have created a blank tree, is has no node data and no children.
We  could use of the methods available in the Tree class but for now we will modify it using regular python commands or known methods.
(If nothing is bound, the default values for both are None for sometree.value and [] (the empty list for sometree.children)

>>>  sometree.value = 6
The node's content become the int 6.
>>> sometree.children.append(Tree(7, Tree(3)))
>>> sometree.children.append(Tree(1))
With this, we've added two children to the tree, a tree with node value (int 1) and no children, and another tree with a node value of 7 and a tree having three as a node and no children as a child.
We could even add a third children, fourth, fifth and so on... the list can be expanded to the user's liking, so long as it stores data in a way that is as efficient as possible.
We could have also used some of the methods to count the number of nodes (4 in this case), the branching factor (2 branches), the listing of all nodes, among others.
Most of these methods require the use of recursion, which we will talk about next week.

Week 7-8-9
We've been introduced to trees themselves, while it is easy to create one, using one is a completely different story. During these few weeks we will learn about recursion, call tracing and binary trees.

First, let us talk about recursion. It's a very simple principle that involves calling a function within itself. As our goal in computer science is to solve problems, recursion is generally the solution when it comes to functions or methods requiring recursive calls.

Suppose we were to do such on a function that allows to calculate the factorial of a given n number:

def factorial(n):
    if n == 1:
           return 1
    else:
            return n * factorial(n-1)

Note how we call factorial within itself. In order to comprehend the work of this, we can do a function call tracing, meaning we will be tracing each and every  of the steps taking place here.

>>> factorial(4)
Let us work through the steps of it.
We know 4 isn't 1, so we return 4 * factorial(3) (4-1=3)
We do not have a complete answer since python still has to call factorial(3). Let us a complete tracing.

>>> factorial(4)
24

Tracing:
return 4 * factorial(3) # 4 not equal to 1, thus the result
return 4 * 3 * factorial(2)  # 3 not equal to 1, thus the result
return 4 * 3 * 2 * factorial (1)  # 2 not equal to 1, thus the result
return 4 * 3 * 2 * 1  # 1 equal to 1, thus 1 is returned at the very end
return 24

Recursive calls at the return statement allow us to treat some data that needs to pass through the same function over and over again with ease and that, thanks to the conditions (if, else statements).
Recursive calls can also be used in methods within classes, we will address this when we talk about traversal orders.

But for now, let us mention binary trees.
Binary Trees are Trees (an ADT with inherited attributes) that have one particularly. Aside from having their nodes containing data, they may have no child, one child or two children.

Though, aside from number of children, what differs between this type of tree and the binary tree?
Binary trees can be used for efficient binary searching, which means a left one's content is always less than a right one. This allows for easy traversal, we will mention this in the next portion.

Week 10-11-12
A final abstract data type was introduced to us in the final weeks, link lists and two topics were boarded: traversal order, efficiency. We will first talk about link lists.

Unlike trees, link lists do not really have any branches when it comes to storing data, it is just one branch, a continuous line.
What link lists have data in their value but that they point to the next node which itself may have a value and a linked node.
We've also learned here that the order in which instructions are inputed when making changes to the link lists is important, as storing the current node before replacing the next one is different than doing the operation the other way around.

Recursion is also used with link lists, in here we learned about traversal order for binary trees. We have four types: inorder (from left to right up to down), postorder (from the bottom nodes to the upper nodes, starting from end nodes), preorder, levelorder.

Efficency refers to how efficient the program is, in other words, giving us a result with a minial amount of memory and time used on the computer's side. For that, we not only take into the consideration the amount of steps a certain algorithm goes through, but also do as estimate how to the minimal and maximal amount of steps an algorithm can perform with the use of Big Oh and Big Omega (See CSC165 Slog.)

ADTs and final thoughts
Stacks, Trees, Binary Trees, Link List are all the abstract data types.
So far, this course has been enlightening when it comes to solving problems in programming as simple algorithms can only go so far.
The amount of abstract data type we were taught about also opened a lot of possibilities when it comes to storing, treating and searching data types. It also taught me or rather further reinforced my skills on making algorithms simplier and less memory consuming and the basics of creating a full program (game, arrangement software.)

As for this SLOG, I can't really say if it affected my style of writing notes, information down as I always do things the same way. Vaguely talk about subjects and interpret information so that common folk and computer scientists alike understand.