As mentioned before, because a binary tree only has two children, it is very efficient to search for an item in the tree, if the tree is sorted. This is much like linear search, in which both cases need the data to be sorted. I learned that a binary search tree is a sorted tree, in which all the values less than the base node would be placed to its left, and all the values greater than the base node would be placed to its right. This means that to search for an item, we do not need to compare each node to the value we are searching for (in which case it would require a longer processing time), but rather, one comparison can throw away half of the tree values off (for that they are disregarded from further searching). It is a much faster and efficient way to search for values.
In the beginning, I thought that linked list is the same as nested list. However, it turned out that they are not quite the same. Linked list is also a class object, which has front, back and size. However, I'm still quite confused of the connection between class LLNode and class LinkedList. It was specially complex when involved with recursion. For example, the __getitem__ method was quite challenging to understand, because I did not know what does recursion help to increase index number. But after I asked my friends, I found out that self.size (which is positive) is added to index to keep on neutralize it to pull it towards positive. I was also confused about the else portion, because I don't understand why cur_node (which refers to self.front) has value and nxt, as these variables belong to LLNode. I hope to see more examples of linked lists to continue improve.
Sunday, March 8, 2015
impressions on week 7
This week's focus was mainly on linked lists. I learned about binary search trees and got introduced to linked lists. Unlike trees, binary trees only have 2 children. Therefore the variable self.children cannot be a list anymore, but instead, it is split to self.left and self.right. This is a lot more convenient for searching algorithms to be implemented. However, it also brought difficulty in writing code because self.children can be an empty list, which can allow programmers to still use list methods on it. But if self.left or self.right is None, then it is harder to write code without errors.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment