Tuesday, March 31, 2015

Revisiting ADTs

For this post, I am going to be revisiting the concept of ADTs, Abstract Data Types. In my past slog entry about Abstract Data Types, I said that they consisted of stacks and subclasses. I also mentioned that I found it difficult to implement stacks and the concept of inheritance. Furthermore, I commented on Assignment 1, which was due earlier that week and which I had a slightly difficult time completing. However, after rereading the entry and revisiting those comments, I've found that I did not clearly or completely discuss this concept. Additionally, I also realized that the topics placed under ADTs was narrow. Through later weeks in this course, I've learned more ADTs that can be added along with stacks and subclasses, for instance trees and linked lists.

An abstract data type (ADT) is a model of data types, which are defined by behavior rather than the representation of data. This behavior is from the perspective of the user of the data, not the implementer. This is usually in terms of values, operations and the behavior of those operations. An ADT is  a "class of objects whose logical behavior is defined by a set of values and a set of operations." As mentioned in the previous entry, stacks and queues are abstract data types. Now, weeks later I can add more to this short list. Trees, linked lists, and BSTs are other abstract data types. While individually a tree and a stack are vastly different, and each has its own advantages and disadvantages for when to implement them, they both fall under the term of ADTs. This may take a while to fully understand, but once it did, the concept really clicked. The applications and uses of ADTs are very diverse and vast.

Looking through my classmates slogs, its clear to see that they also have been able to fully understand the concept of ADTs better. Being able to understand and identify trees, lists, stacks and queues as Abstract Data Types is very important, especially for object-oriented programming. Or as one class mate put it: "Object Oriented Programming is focused on using custom classes with various special methods and properties included, some of which serve as Abstract Data Types (like lists, stacks, queues, trees)." . Another classmate, when discussing stacks and trees, mentions that they are also abstract data types. Being able to connect the concepts we learn now with previous concepts learned earlier in the course, is crucial, especially when implementing them. This also makes it easier to learn, when you group similar concepts together, and having a broad term to use when talking about vastly different objects, (such as a tree and a stack) makes it easier to get a point or an idea across, as well as making it able for a programmer to get his thoughts across in a short, concise manner (in as little words as possible).

Sunday, March 22, 2015

Week 9 Impressions: Mutating BSTs

Last week, (week 9) we had our second and final test for the semester. The test covered topics from weeks 5-8, and concepts such as tree definitions (travelling through trees using postorder, inorder and preorder traversals), recursive functions, binary trees and binary search trees, as well as linked lists. We were also allowed an aid sheet. In general, I didn't find the test to be too difficult, some concepts, especially the newer ones, such as linked lists were harder to understand. Since the test occurred during lecture time, we did not go over much in class. In fact we had less than an hour for the lecture last week.

Earlier in the semester we learned about Binary trees and Binary Search trees, a way of  organizing data in trees such that they are easier and more efficient to search through. In class last week, we wrote a function called insert that inserted data starting at node, and returned the root. We examined the code (def insert) and talked about a new function for deleting data instead of inserting data. In the function for inserting data, we wrote a recursive function. Taking in a node and data, the function should insert the data at the node and return the root. Starting by assigning the node to a new variable, called return_node, we then wrote an if statement for the simplest example, where there is no node (if not node:), if that is the case, then the variable return_node should be assigned the new data. If the new data is less than the data at the node, then the data is inserted in the left node, else if it is greater than the data at node, then it is inserted to the right. This is a property of binary search trees (greater values are on the right and smaller values on the left of the node). Finally if there is nothing else to do, (else statement), return_node should be returned. This code is easy to understand, especially after breaking it down like I did above. By going through the code line by line and "reading" the if's, elif's and else statements, you can understand what the code does, even if there is no docstring description.

Finally, in class we went over, briefly a function for deleting data from a binary search tree (BST) rooted at node. In order to write a function for deletion of data, we had to first understand and write a general algorithm for delete. This is both to understand what we want the function to do and also to make writing it more simple and easier. Our algorithm for delete, is to first see if node is None, if True then return that node. Next, we want to check if the data is less than the data at the node (like we did in the insert function). If it is we delete it from the left child and return this node. Third, we check if the data is more than the node.data, and delete it if it is from the right child, returning this node. The fourth case we can have, is that the node has fewer than two children. If that is the case, then one must be None, and must therefore return the other one. Finally if the node has two non-None children (i.e there is data for both the right and left), then the data must be replaced with that of its largest child in the left subtree. That child is then deleted and the new node returned. Looking at the code, its clear that this is what happens, and that by writing out an algorithm first, it was easier to write the code.

Thursday, March 12, 2015

Week 8 Impressions: Linked Lists

Last week the TA's went on strike. Therefore, we didn't have any labs during the week. The lab handout was still available, but there were no lab quizzes, and no one had to show up to their weekly scheduled labs. This meant, that while there were still questions and functions to write and implement, there was no TA to ask for help, when stuck and needing guidance. Beyond this inconvenience, the lecture was understandable and I didn't have any more trouble solving the lab problems. The discussion board was a good place to go, especially this week, when I had trouble with the lab. Also the solutions were posted for the lab, so I had the answers and when stuck I could work back from the solution and understand how they got to that code.

In class, we learned about linked lists. We also learned of two different, but very useful ways to think about linked list structures. The first way to think of them is as lists made up of an item (value) and a sub-list (rest). The second way of thinking about linked lists is as objects (nodes) with a value and a reference to other similar objects. This sounds complex but what it really translates into is a list within a list, or a tree with a branching factor of one, meaning one node points to another, which points to another and so on. Linked lists also need a "wrapper" class. This class represents the linked list as a whole and keeps track of information about the entire list, including its front, back and size. This class initializes these variables as the most basic and simple list (an empty linked list) with size 0, and the front and back both initialized to None.

Furthermore, we learned how to "walk" across a linked list, as in how to write code that can check the nodes of a linked list. In that code, we made a reference (to at least one) other node and then moved the code along the list. We then added and modified to the code so that is checks every possible node in a linked list ( _contains_ ). Afterwards, we wrote code for an append method, which does several things: change the last node, the former last node, the back, the size (adding one as an item is appended to the list), as well as possibly the front. A question concerning linked lists was on Test 2, which we had this week. Understanding and learning how to implement functions for linked lists was something I found slightly difficult for this week. However, by working through the problems in the lab handout and by playing around with the class code, I was able to gain a more thorough understanding of the concept. Programming really is a hands on subject; to learn more efficiently and effectively all you have to do is code more.    

Sunday, March 8, 2015

Week 7 Impressions: Binary Trees

During week 7 for this course, we continued learning about Trees, this time Binary Trees. Binary trees are a specific type of trees in which each node can have at most two children (left and right). Binary trees can be have ordering conditions. This is very useful, since with an ordering condition, a binary tree is more efficient and easier to search through. If it is known that the higher values are on the right of a node and lower values are on the right, then searching through the tree is not only easier, it's also more efficient.  Also, using a binary tree, we can create a code so that the program searches through the tree without having to stop at and search through unnecessary nodes. For example, if we are searching for 8 in a binary tree and self.data (or the value at the first node) is 4, then we know that the code does not have to stop at the node on the left, since it would be less than 4, and 8 is obviously greater than 4. Using a binary tree in this manner, makes searching through large amounts of data more quick and efficient. Additionally there are more than one way of searching through a binary tree: inorder, preorder, and postorder.

Inorder searching is a way of searching through binary trees, where it visits the left subtree inorder, then visits the node itself, then visits the right subtree inorder. This code is recursive. Preorder, on the other hand, is different, since the node itself is visited first, then the left subtree in preorder, then the right subtree in preorder. Finally, post-order searching is when the left subtree is visited first in postorder, then the right subtree in postorder, then the node itself. In all cases, the left subtree is always visited before the right subtree. 

Furthermore, in last week's lab, we created recursive code for a binary tree. This was difficult at first, especially writing the recursive part of the code. In order to create recursive code, you must first understand the most basic and simplest example, which does not require recursive code. This part usually the easiest since it does not require any recursion and is just a simple 'if' statement. Afterwards, you must think of the most simplest example that does require a little recursion. After, write the recursive code for this simple recursive example. This part is much more difficult, and takes some practice and understanding. However, afterwards it becomes much more easy to write and implement recursive code. For one part of the lab last week, we had to write code to search through a binary tree without stopping at unnecessary nodes. As mentioned above, this meant we had to use the properties and ordering conditions of a binary tree already explained. This code was also recursive and was easier to understand and visualize than to write and implement. However, once the most basic example was thought of and code written for, the rest flowed naturally.  

Sunday, March 1, 2015

Week 7: Recap on OOP

For this week, please grade my summary on Object Oriented Programming: found here.

To recap, what I've written on Object Oriented Programming:
OOP (Object-oriented programming) is simply a model for programming. It is centered on objects (obviously) as well as data. This differs from other models for programming languages, which typically are centered around actions and logic. Objects are abstract data types (ADTs). Furthermore, objects contain data, called attributes and code, called methods. Data classes, such as those used in Assignment 1 and in labs, are important in defining subclasses, as well as for creating special types of classes, like Stacks, Trees, and Queues. Inheritance, which is important for coding subclasses, is a more efficient way of programming (than to manually program each class). Through subclasses, those classes that share similar functions and methods, can also share code. The programmer would not have to write code twice (or copy and paste code, which can cause errors to occur). Therefore, Inheritance is more time efficient as well as a more accurate way of coding (less errors will occur). 

Additionally, through object-oriented programming, programmers can create trees, which are extremely useful. Through a binary search tree, in particular, a programmer can search through thousands of options more quickly and efficiently. Since the binary search tree is set up in such a way that data is comparable. The data at the nodes to the left are less than data.node and those to the right are greater. Thus a programmer can write code to search the tree without having to search all the nodes in the tree, since it would know that is does not have to stop at the left or right node, according to the value it is searching for and the value at the data.node. Also, as when used in Assignment 1, object-oriented programming, can be used to make a general class with more specific subclasses, which can be used for other games (such as Subtract Square, Tippy, or Tic-Tac-Toe). Using object-oriented programming is very useful and has numerous advantages.