Пример #1
0
 def test_linked_list6(self):
     lst = None
     for i in range(3):
         lst = linked_list.insert(lst, i, i)
     self.assertEqual(linked_list.remove(lst, 0), Node(1, Node(2, None)))
     lst = linked_list.remove(lst, 0)
     self.assertEqual(linked_list.remove(lst, 1), Node(2, None))
     lst = linked_list.remove(lst, 1)
     self.assertEqual(linked_list.remove(lst, 2), None)
Пример #2
0
def build_Hufftree(alist):
    llist = convert_llist(alist)
    while (linked_list.length(llist) > 1):
        node1,llist=linked_list.remove(llist, 0)
        node2,llist=linked_list.remove(llist, 0)
        n=Node(0,(node1.freq+node2.freq),node1,node2)
        n.char=(node1.char<node2.char) and node1.char or node2.char
        llist=linked_list.insert_sorted(llist, n, comes_before)
    if(llist==None):
        return None
    else:
        return linked_list.get(llist, 0)
Пример #3
0
def join_leaves(sorted_list):
    #whilethere is more than one node in the list
    while linked_list.length(sorted_list) > 1:
        #remove two nodes from beginning of list (lowest frequencies)
        (leaf1, sorted_list) = linked_list.remove(sorted_list, 0)
        (leaf2, sorted_list) = linked_list.remove(sorted_list, 0)
        #create new Node with these two leaves as children, lesser of two on left(leaf1)
        node_frequency = leaf1.frequency + leaf2.frequency
        if leaf1.character < leaf2.character:
            character = leaf1.character
        else:
            character = leaf2.character
        new_node = Node(node_frequency, character, leaf1, leaf2)
        sorted_list = linked_list.insert_sorted(sorted_list, new_node, comes_before)
    return sorted_list
Пример #4
0
def build_tree(list):
    sorted = build_sorted_leaves(list)  # TODO: re-nest build_sorted_leaves?
    while ll.length(sorted) > 1:  # TODO: inefficient
        tup = ll.remove(sorted, 0)
        first = tup[0]
        sorted = tup[1]
        tup = ll.remove(sorted, 0)
        second = tup[0]
        sorted = tup[1]
        if first.char < second.char:
            char = first.char
        else:
            char = second.char
        node = Node(char, first.freq + second.freq, first, second)
        sorted = ll.insert_sorted(sorted, node, comes_before)
    return node
Пример #5
0
def build_tree(list, func):
    sorted_list1 = sorted_leaf_list(list)
    sorted_list = array_to_linked(sorted_list1)
    while linked_list.length(sorted_list) > 1:
        leaf_node, sorted_list = linked_list.remove(sorted_list, 0)
        leaf_node1, sorted_list = linked_list.remove(sorted_list, 0)

        if int(leaf_node.val) < int(leaf_node1.val):
            x = Node(leaf_node.val, leaf_node.freq + leaf_node1.freq,
                     leaf_node, leaf_node1)
        else:
            x = Node(leaf_node1.val, leaf_node.freq + leaf_node1.freq,
                     leaf_node, leaf_node1)
        sorted_list = linked_list.insert_sorted(sorted_list, x, func)
    ptree = linked_list.get(sorted_list, 0)
    return ptree
Пример #6
0
def remove(ht, key):
    index = hash(key) % ht.size
    if ht.table[index] == None:
        raise LookupError
    if linked_list.length(ht.table[index]) > 1:
        for item_index in range(linked_list.length(ht.table[index])):
            itemref = linked_list.get(ht.table[index], item_index)
            if itemref[0] == key:
                ht.table[index] = linked_list.remove(ht.table[index],
                                                     item_index)[1]
                ht.items -= 1
                return ht
        raise LookupError
    ht.table[index] = None
    ht.items -= 1
    return ht
Пример #7
0
 def test_remove(self):
     self.assertEqual(remove(Node(1, Node(2, None)), 10),
                      Node(1, (Node(2, None))))
     self.assertEqual(remove(None, 20), None)
     self.assertEqual(remove(Node(1, Node(2, Node(3, None))), 2),
                      Node(1, Node(3, None)))