2015年3月8日星期日

Impressions of week #7

This week we focus on the BTNodes and Linked lists. These are special classes.  In Professor Danny's ppt, he mentioned that most of the work of special methods is done by the nodes "__repr__", "__str__" and "__eq__".Once these are done for nodes, it's easy to do them for the entire list.
1. BTNodes
 Binary tree code has two children which are in order, the left one and the right one. 
eg: 
b = BTNode(8, BTNode(4, BTNode(2, None, None), BTNode(6, None, None)), BTNode(12, None, BTNode(14, None, None)))
print(b)
        14
    12
8
        6
    4
        2
So when we write some recursions linked with BTNodes, we need to pay much attention to the children. What we usually do is operating the left tree and operating the right tree and combining them. And when it comes to list_between(node, start, end), we do not have to compare all the data in the node because data in left subtree are less than node.data and data in right subtree are more than node.data .
2. LLNode
Linked lists are lists made up of an item (value) and a sub-list (rest). And the objects (nodes) have a value and a reference to other similar objects. 
eg
>>> n = LLNode(5, LLNode(7))
>>> print(n)
5 -> 7 ->|
In order to make a reference to the node and move it along the node, what we usually do is using the codes below:

cur_node = self.front
while <some condition here...>:
    # do something here...
    cur_node = cur_node.nxt
The Linked list nodes can be quite confusing and we shall always draw pictures to make the situations much more clear. And what can't be ignored is that once we change the LinkedList, we need to change last node, former last node, back, size and possibly front.


没有评论:

发表评论