Monday, January 26, 2015

Changes 1/26/15 Heaps

Mr. Elkner gave me a python implementation of a heap to type up and a couple of questions to go along with it.  It was a good exercise because it showed how a regular array can be used to represent values in a binary tree.  EIMACS had talked a little bit about that but getting to actually work with the code was really helpful.  I think I have a good understanding of how the heap is actually built.  The method #heap_down, which swaps elements to ensure they obey the contract of a max_heap was a bit hard to understand at first, but I understand it much better after going through the code line by line to try to understand it.

Changes 1/23/15

Helped Aki with the Bottle Zoo webapp, and did more binary tree stuff.  I started writing a binary search tree implementation in Ruby.

Thursday, January 22, 2015

Changes 1/22/15 - EIMACS trees test

Took EIMACS test 24, which was on binary trees and heaps.  I didn't actually understand binary trees until halfway through the test, though.  I thought that they had to be balanced, but they actually don't.  EIMACS did a really bad job explaining this!  I'm definitely going to do some additional reading on heaps and binary search trees.

Wednesday, January 21, 2015

Changes 1/21/15 - More EIMACS Binary Search Trees

I read more about binary search trees on EIMACS, like how they are sorted as the initial values are appended. It showed a little bit of the behavior of Java's TreeSet class, like the fact that #toString() uses an inorder traversal to output values. Then it started teaching me about heaps, which are structured like BSTs but with a different sorting method. There are two types: a min-heap and a max-heap. Min-heap means that the value at the root of each tree is less than the value of each of its branches, whereas a max-heap means that the root of each tree is greater than the values of each of its branches.
This is a min-heap: each node's value is greater than the value of its parent.

EIMACS really didn't to a good job of explaining complete trees, but I think that in order for a heap to behave correctly it needs to be complete.  This means that the nodes fill from left to right such that there is never a node with a right branch but not a left one, while also keeping greater or equal amounts of nodes on the left as compared to the right.

Heaps can also be represented as arrays, with each level being added successively.  EIMACS also inserts a null value at position zero to make some properties easier to describe.

The above heap would be written as {null, 1, 2, 3, 17, 19, 36, 7, 25, 100}, and would have the following properties:
The index of a given node's child elements will be at double that node's index and, if a right node exists, double that node's index + 1.
The inverse is also true: a given node's parent element can be found by taking (int)(node index / 2).

This seems like it is definitely useful!  EIMACS gives priority queues as an example where heapsorts are often used.

An algorithm that organizes nodes to create a min-heap does the following:
Each value is added as a new leaf to the bottom row or starts a new row if the current bottom is full. If the new value is found to be greater than or equal to the parent value, then the insertion is complete. Otherwise, the values of the new node and the parent node are swapped.

Friday, January 16, 2015

Changes 1/16/15 - Recursion and binary search trees

At NSF last night, Kevin and I talked about recursion.  He emailed me after my blog post about recursion in binary trees and mentioned that recursion is also evident in linked lists, since they are a chain of the same type of object linked to each other.
Unfortunately I didn't take a picture of his diagram, but this image sort of shows the same thing: each node links to another node, or null.  I'm still feeling a little shaky on his explanation, because I thought recursion was more of a strategy for an algorithm rather than inherently in a data structure.

Sam also wrote up and explained a cool C file that emulates the car, cdr, and cons LISP functions in C, so I have a better understanding of what those actually are doing now.


This program outputs
6 7 8 3 2 1

I also got further in the EIMACS binary search tree curriculum. They still haven't explained what makes a balanced search tree balanced, so I'm a little confused as to the significance. I'm pretty sure it's because that way you can guarantee that the sorted tree has the elements where you think they are, because otherwise it could be arbitrary (I know that's vague, I'll try to expand on that on Monday but I'm running out of time.)

Thursday, January 15, 2015

Changes 1/15/15 - EIMACS and Open closed principle

I finished up the portion of EIMACS about traversing binary trees and started searching algorithms.  Binary search trees are trees where the value of the left node is less than the root value, while the value of the right node is greater than the root value.

Kevin also asked me to look at the Open/closed principle of object oriented programming, which states that software entities should be open for extension, but closed for modification.  Basically, inheritance allows a programmer to add their own code to a base class without modifying that base class.  That seems pretty simple, but I'm sure I'm not thinking of all the implications that brings.  Also, it's interesting that Ruby doesn't seem to adhere to this principle because you can modify essentially any classes you want, even if they are base classes.

Wednesday, January 14, 2015

Changes 1/14/15 - UMD Contest 2014 problem

Today, Sam and I worked on 2014 UMD contest problem number 4, which is called Minion Walk.  It involves checking if there is a path from the top left of a grid of squares to the bottom right using a flood algorithm.  The algorithm checks if the space it is currently in is empty, then makes a recursive call to each adjacent space, eventually traversing all possible spaces.  It was actually pretty easy for Sam and I, since Sam already had experience with this sort of algorithm.

Kevin also emailed me and asked me to look into the Open Closed Principle of object oriented programming, which I didn't get a chance to do today.  I'll be sure to look into that tomorrow.