2015年3月9日星期一

Link List

    Link list likes tree what we learn before.  However there are quite different between them.  Link list only have one node of one root which is unlike tree, having 2 or 3 nodes.
    Firstly, we need a class, which likes a tree class, to record a value that before of after a value.
    Secondly, we can create a link list class.  In the __init__ function, we create two variable to record a link list in the following method.
    
    Following, it is the method that use to append a value before a value.
    Lastly, it is the method that use to append a value after a value.

def append(lnk, value):
        ''' (LinkedList, object) -> NoneType

        Insert a new node with value at back of lnk.

        >>> lnk = LinkedList()
        >>> lnk.append(5)
        >>> lnk.size
        1
        >>> print(lnk.front)
        5 ->|
        >>> lnk.append(6)
        >>> lnk.size
        2
        >>> print(lnk.front)
        5 -> 6 ->|
        '''
        new_node = LLNode(value)
        if lnk.back:
            lnk.back.nxt = new_node
            lnk.back = new_node
        else:
            lnk.back = lnk.front = new_node
        lnk.size += 1

2015年3月1日星期日

Tree

    Tree is kind of basic data structure which is used for storing data.  Each tree only has one root and each non-root nodes has exactly one parent.  There are directed edges between some pairs of nodes.  In addition the number of edges in a sequence of nodes, called path, is the length of the this path.  In the following, there are some codes of tree class.
    First of all, it is the initialized function of tree class.
    Secondly, there are some method about tree class.  

    It is the method about counting leaves(nodes with no children).  In this method, we use a recursion to guarantee to get the basic nodes of the tree.


    It is the method about counting internal nodes (node with one or more children).  In this method, we use a recursion to find out all the nodes which does not have children from the root to the basis.