So after looking back at all of my previous posts, I figured that it would be most appropriate to revisit my very first post, Why Geeks Write. As I stated in that post, coming into CSC148, I saw SLOGs as a massive waste of time because doing it did not teach us any new computer science content. I personally enjoyed writing creatively in my own time, but I did not appreciate SLOGs because it felt 'work' and was too technical for my tastes. Surprisingly, I got really into writing that very first post and it ended up changing my perspective on writing about computer science to a more positive one. That post made a very big impact on me throughout the rest of the course. I thought about it while writing every single one of my other blog posts, and always put effort and genuine thoughts into my writing like I would with my creative writing, rather than trying to get it done as quickly as possible and making up things that I think the TA marking this wants to see in order to get marks.
When I wrote that first post, I had no actual experience, and could only see the theory as to why writing about these technical things would be useful. However, as the term progressed and I continued putting effort into my posts, I actually felt the benefits. Writing summaries of the topics we covered each week in lecture helped me gain a better idea of how well I knew the content, and actually helped me get to know the content better because I had to review and make sense of things in order to write comprehensible summaries in the first place. These summaries also acted as notes that I looked over as preparation for tests. As well as this, writing down my thoughts and opinions each week helped me reflect on things I was good at, and things I needed to work on. I sometimes even wrote down little things that I had to do (such as in my 3rd post, Tracing Recursion) and it served as a reminder to actually go do these things. Sometimes when you tell yourself you need to do something, you lack the motivation to actually do something, especially after you leave it for a period of time. But when I put it down in a post, and I know it's "out there" for other people to see, it feels more binding, and provided me with just enough of a boost to actually do the things. It was also just fun and pleasant in general to look back at what I wrote, remember what I felt back then, and see how much I have grown since then. I saw writing as an outlet to express myself and let my creativity flow, and thanks to that first blog post and this SLOG in general, writing now means even more to me.
I looked at some of my classmates' SLOGs, and was surprised to find that a lot of blogs were not being updated at all. It disappoints me a little seeing that not everybody sees the wonders of writing. However, I did find a few blogs that regularly updated and had some pretty good content. It was interesting to see what people found challenging or easy and how they perceived the content of the course. One particularly memorable thing I came across on a blog (http://juliansongcsc148slog.blogspot.ca/) said that computer science is so much more than just coding. This perfectly put into words what I had slowly manifesting the back of my mind as my studies progressed, and I completely agree with it. Computer science is so much more than just 'this code does this', 'typing this will do this'. Careful, logical consideration, planning, and even some amount of creativity is required in order to put together algorithms, the efficiency/running time of code also needs to be taken into account, there is math involved, and being able to discuss and communicate ideas to fellow computer scientists is also essential. There is so much more to computer science than I previously thought, and I look forward to discovering even more as I continue my studies.
Monday, March 30, 2015
Tuesday, March 17, 2015
Binary Search Trees
In lecture we went back to look at binary search trees, which we briefly looked at before when working with binary trees. A binary search tree is a binary tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node.
When we worked on binary trees previously, the insert function to add nodes into the tree was always provided, and we never really talked about it. In lecture we finally got to take a closer look at this, as well as deleting nodes. Although there was no lab because of the strike, there was still a lab exercise, which provided the opportunity to experiment with, and further familiarize ourselves with the insert and delete functions.
I found the function delete a little tricky at first, but after playing around with it a little, I got it.
What this code does is basically removing a specific node from the tree if the tree contains the specific node, and then returns the resulting tree. If the node we are trying to remove is an internal node, we still want to keep it's children/the subtree under it, so we cannot simply locate the node and make it None. Instead, we replace the data of the node with that of its largest child in the left subtree, and then delete that child. This child is larger than all of the other nodes in the subtree, but is smaller than the node it is replacing. This way, the structure of a binary search tree - left node data is smaller than its parent's and right node data is larger than its parent's. An alternative would be to find the smallest node in the right subtree to replace the specific node we are trying to remove.
We can do this by replacing the code under 'else:' with:
and use a helper function _find_min instead of _find_max
We were also prompted to think about the efficiency of functions on a binary search tree. For a regular binary tree with nodes randomly placed, to find a certain node in the worst case, we would have to traverse through all of the tree's nodes. However, with how the nodes are organised in a binary search tree, the worst case requires time proportional to the height of the tree, which would be a lot less than a regular tree with nodes that had more than one child. Overall, I found binary search trees to be fairly fascinating to think about and really fun to play around with.
I found the function delete a little tricky at first, but after playing around with it a little, I got it.
def delete(node, data):
''' (BTNode, data) -> BTNode
Delete node with data if it exists and return the resulting tree.
'''
return_node = node
if node is None:
pass
elif node.data > data:
node.left = delete(node.left, data)
elif node.data < data:
node.right = delete(node.right, data)
elif node.left is None:
return_node = node.right
elif node.right is None:
return_node = node.left
else:
node.data = _find_max(node.left).data
node.left = delete(node.left, node.data)
return return_node
def _find_max(node: BTNode) -> BTNode:
'''Find and return maximal node, assume node is not None'''
return _find_max(node.right) if node.right else node
What this code does is basically removing a specific node from the tree if the tree contains the specific node, and then returns the resulting tree. If the node we are trying to remove is an internal node, we still want to keep it's children/the subtree under it, so we cannot simply locate the node and make it None. Instead, we replace the data of the node with that of its largest child in the left subtree, and then delete that child. This child is larger than all of the other nodes in the subtree, but is smaller than the node it is replacing. This way, the structure of a binary search tree - left node data is smaller than its parent's and right node data is larger than its parent's. An alternative would be to find the smallest node in the right subtree to replace the specific node we are trying to remove.
We can do this by replacing the code under 'else:' with:
node.data = _find_min(node.right).data
node.right = delete(node.right, node.data)
and use a helper function _find_min instead of _find_max
def _find_min(node: BTNode) -> BTNode:
'''Find and return minimal node, assume node is not None'''
return _find_min(node.left) if node.left else node
We were also prompted to think about the efficiency of functions on a binary search tree. For a regular binary tree with nodes randomly placed, to find a certain node in the worst case, we would have to traverse through all of the tree's nodes. However, with how the nodes are organised in a binary search tree, the worst case requires time proportional to the height of the tree, which would be a lot less than a regular tree with nodes that had more than one child. Overall, I found binary search trees to be fairly fascinating to think about and really fun to play around with.
We had our second test during our Wednesday lecture time slot. The test was a lot easier than I thought it would be. I don't think there was anything in particular that stood out, it was basically types of things that we had already done in either lecture or labs.
Sunday, March 8, 2015
Linked Lists
A linked list structure can be thought of as an item and a ordered sub-list. It can be implemented on python with the use of 2 classes. The first class represents a node, which can be thought of as an item in the list. This class keeps track of the node's own value which can be any object type, and also has a 'next' attribute that points to the next node. A second class stores information about the entire list. It keeps track of the very first node (front), the last node (back) and the number of nodes in the list. It can be visualized as shown below.
There are many methods or functions that can work with linked lists. It is possible to check whether the list contains a certain value, to retrieve a value with its index, add a node anywhere in the list, delete nodes anywhere in the list etc. The majority of these things require moving along the list and examining each item. This can be done by creating a current_node variable and setting it to the front node of the list, then moving current_node along the nodes in the list.
current_node = self.front
while <condition>:
<action>
current_node = current_node.nxt
*self refers to the class that keeps track of the entire list
I found the concept really easy to grasp, and it is relatively easy to work with linked lists as long as I draw out examples as I am writing methods/functions that work on linked lists. I didn't find linked lists to be unpleasant, but I didn't find it very interesting either, but I'm sure there will be more fun new things coming up for us to cover in the course.
Sunday, March 1, 2015
Recursion
Recursion is a way of coding where a function calls itself in its body. It basically keeps solving smaller instances of the same problem until a final solution is reached. Recursion can be quite complicated and confusing to trace, especially if there are large amounts of information that the code needs to process and so it is extremely easy to make mistakes that lead to massive errors in coding. A good way to approach coding with recursion is to identify the base case scenario(s) that do not require recursion, code this first, and then move on to add in recursive calls. It is useful to keep in mind that if the base is never met in the calls, an infinite loop will occur. Recursion can allow code to do things that it wouldn't be able to do otherwise, but in many cases, it is possible to work without recursion. In these cases, recursion can still be beneficial because it can make the code a lot shorter and easier to read or follow.
I talked about object oriented programming (OOP) and abstract data types (ADTs) in last week's post, and mentioned tracing recursion the week before that. This week, we got to combine these things and work on binary trees. A binary tree is a series of nodes, where each node can have a maximum of 2 children. Expression trees and binary search trees are examples of binary trees. An expression tree is a tree where each internal node is an operation and each leaf is a number, and working through the tree produces a mathematical expression. A binary search tree is a tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node. We worked on many functions that could be used on these binary trees, such as functions to add parentheses and create an expression out of an expression tree that can be evaluated, a function to evaluate an expression tree, a function that lists all the nodes with values in between two given values, functions to find the height, longest path, number of leaves etc. At first, I found these really hard to figure out, but as I did more, I realized that as long as I stick to writing down the code for base cases that do not require recursion first, and then work on the recursion code, it wasn't actually all that difficult. Working on these things also helped me become more familiar and comfortable working with ADTs.
I talked about object oriented programming (OOP) and abstract data types (ADTs) in last week's post, and mentioned tracing recursion the week before that. This week, we got to combine these things and work on binary trees. A binary tree is a series of nodes, where each node can have a maximum of 2 children. Expression trees and binary search trees are examples of binary trees. An expression tree is a tree where each internal node is an operation and each leaf is a number, and working through the tree produces a mathematical expression. A binary search tree is a tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node. We worked on many functions that could be used on these binary trees, such as functions to add parentheses and create an expression out of an expression tree that can be evaluated, a function to evaluate an expression tree, a function that lists all the nodes with values in between two given values, functions to find the height, longest path, number of leaves etc. At first, I found these really hard to figure out, but as I did more, I realized that as long as I stick to writing down the code for base cases that do not require recursion first, and then work on the recursion code, it wasn't actually all that difficult. Working on these things also helped me become more familiar and comfortable working with ADTs.
Subscribe to:
Posts (Atom)
