Monday, March 31, 2014

What is Big Oh?


The term big oh is a commonly used when discussing the runtime of a function. For example, if I have a function that takes 10 steps when given an input size of 10, 20 steps for an input size of 20, and so on; then, when given a large arbitrary input size of n, it would take n steps. This function would be denoted as O(n) because the function grows linearly. Therefore, big oh is denotes the runtime of a function. However, the runtime can be different for different inputs. In CSC108, we learned about bubble sort. Here is a code for bubble sort we used in CSC108:

def bubble_sort(L):
    """ Sort the items of L from smallest to largest.  """
    end = len(L) - 1
    while end != 0:
       for i in range(end):
            if L[i] > L[i + 1]:
                L[i], L[i + 1] = L[i + 1], L[i]
        end = end - 1


The algorithm of bubble sort is based on a simple technique, where two consecutive numbers are compared with each other and swapped iff the second number is smaller than the first. This is done for each number in the list, indicating that the runtime of an unsorted list would be O(n2). However, the runtime can be totally different if the list was already sorted (the runtime would be O(1)), which would be the best case scenario. Often the best case scenario doesn’t tell us much, so we look at the worst case scenario, which is when the list is reversed sorted in the bubble sort example. The O(n2) is the worst case runtime for bubble sort, which is useful to know to get an idea about the function’s efficiency (the topic of my next assigned slog post).

Saturday, March 29, 2014

Binary Search Tree Example

To continue with the use of the properties of Binary Search Trees. The best example would be a lab exercise that was initially quite challenging but finally clarified many misconceptions about BSTs and left me with in-depth understanding (or at least, that's how I feel)!
For lab 8, we had to write a method called count_less, where given a BST the function returns a sum of all the numbers less than the value at the root. We had to write this method using three different class implementations but the one I present here is the one I struggled with the most.
def count_less(self: '_BSTNode', item: object) -> int:
        """
        Return the number of items in the BST rooted at this node that are
        strictly less than item.
        """
        if self.item:
            if self.left is None and self.right is None:
                if self.item < item:
                    return 1
                else:
                    return 0
            elif self.left is None:
                if self.item < item:
                    return 1
                else:
                    return self.right.count_less(item)
            elif self.right is None:          
                if self.item < item:
                    return self.left.count_less(item) + 1
                else:
                    return self.left.count_less(item)
            else:
                if self.item < item:
                    return self.left.count_less(item) + 1
                else:
                    return self.left.count_less(item) + self.right.count_less(item)
        else:
            return 0
Yes, I know that that my code is really long because of repetition but I am presenting this version instead of a compact one for the sake of clarity as I describe how the property of BST is used when coding this method.

First of, assuming that the class BSTNode is implemented properly, we check if the root of the tree has a value and if it does then we check if it has no children, only left child, only right child or both children. The case where we have no children, the implementation is straight forward. Also, for the case where we have no left child, we are bound to check the right child given the value of the item is more than the value of the root, and vice versa for the case where we have no right child. However, it is interesting to take a look at the else clause because this is where we see the implementation of a BST property. In the else clause, given the value of the item is less than the value of the root, we only do recursion on the left subtree because that is the only place where there could be more values that are less than the value of the item to suffice the purpose of this function.

Thursday, March 27, 2014

Binary Search Trees

My last post was definitely not about Binary Search Trees. It was about Binary Trees in general (although I managed to mistakenly write Binary Search Trees). So, I will now explain how a binary search tree is different from a normal binary tree. A normal binary tree is a tree where the root can have two children, left child and right child, just like the example presented in the previous post. There are no restrictions as to what the left and the right child could be. However, in Binary Search Trees (BST), the left child’s value must be smaller than the root while the right child’s value must be greater than the root. Considering that the left and the right child themselves are BST, they would follow the same rule, i.e. their left child’s value must be smaller than itself while the right child’s value must be greater than itself.

Here is an example of a Binary Search Tree:

In this example, we see that 4 is less than 5, hence it is the node of the left subtree; while 7 is greater than 5, hence it is the node of the right subtree. This pattern follows for the left and right subtrees themselves.
It is a common misconception to think that the left subtree of the right child can be smaller than the node. This is because when we add 4 for instance, it *first* checks if 4 is less than the value at the root or not, which it is. Then, *second* it goes and compares 4 to the value of the root of the left subtree. Thus, a value less than the root can never be found in the right subtree. This property often used when searching for a specific number as it number's value and the root's value can be compared to narrow the search, thus doing less work (one of the primary goals of computer scientists)!

Wednesday, March 5, 2014

Binary Trees and Linked Lists


A binary search tree is essentially a list with a value, representing the node or the parent, which points to its left child and right child. The left child and the right child could be a value or a tree itself, which could have another tree within itself, and so on. It differentiates for an actual tree as its branches grow downwards and it can only have 2 branches since it is binary. When a branch does not further lead to other branches, it ends by pointing to None type objects. An example would be:
T = [5, [4, [3, None, None], [1, None, None]], [2, None, None]] 

A linked list, on the other hand, is basically the same kind of structure as at binary tree but the only difference is that it has only one child. It is also made of a list, which only points to one child instead of a left child and right child. Consequently, the visual representation of the following linked list example is:
L = [5, [4, [3, None]]