Friday, 1 November 2013

Binary Search Trees


This week in lecture we learned about binary search trees. Basically, they are binary trees whose left sub trees have values less than the node, and whose right sub trees have values greater than the node. I think that binary search trees are more efficient than binary trees because the tree is partially sorted. For example, in exercise 6, which is due tonight, we are required to create a function max_node, which returns the node with the greatest value. Instead of creating a function that iterates through the entire binary search tree, I can simply leave out the left sub trees, since I know that they all have values less than the root. So if I had to create a function min_node that found the smallest node value, I could disregard the right sub trees, since they all have values greater than the root.

On another note, I finally finished e6! I found implementing __contains__ and max_node without recursion to be quite difficult. My original code consisted of using __str__() on the given BST, then checking if the item was in the given string. I later found out a way to write my code without recursions. However, since the Auto-marker was not running normally, I was not able to test my code as often as I normally do. Hopefully I do well!

No comments:

Post a Comment