Esempio n. 1
0
def build_huffman(OccuranceList):
    if OccuranceList == array_list.empty_list():
        return None
    huffman_linked_list = linked_list.empty_list()
    for index, occurance in enumerate(OccuranceList.array):
        if occurance != 0:
            temp_leaf = Leaf(
                chr(index),
                occurance)  #figure out how to go from into to asciib
            huffman_linked_list = linked_list.insert_sorted(
                huffman_linked_list, temp_leaf, comes_before)
    while linked_list.length(huffman_linked_list) != 1:
        temp_leaf_1 = linked_list.get(huffman_linked_list, 0)
        temp_leaf_2 = linked_list.get(huffman_linked_list, 1)
        huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0)
        huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0)
        total_freq = temp_leaf_1.freq + temp_leaf_2.freq
        if comes_before(temp_leaf_1, temp_leaf_2):
            if ord(temp_leaf_1.char) < ord(temp_leaf_2.char):
                temp_node = Node(temp_leaf_1.char, total_freq, temp_leaf_1,
                                 temp_leaf_2)
            else:
                temp_node = Node(temp_leaf_2.char, total_freq, temp_leaf_1,
                                 temp_leaf_2)
        huffman_linked_list = linked_list.insert_sorted(
            huffman_linked_list, temp_node, comes_before)
    return huffman_linked_list.first
Esempio n. 2
0
 def test_linked_list3(self):
     lst = None
     for i in range(3):
         lst = linked_list.insert(lst, i, 0)
     self.assertEqual(linked_list.get(lst, 0), 2)
     self.assertEqual(linked_list.get(lst, 1), 1)
     self.assertEqual(linked_list.get(lst, 2), 0)
     self.assertRaises(IndexError, linked_list.get, lst, 3)
     self.assertRaises(IndexError, linked_list.get, lst, -1)
Esempio n. 3
0
def insert(ht, key, item):
    index = hash(key) % ht.size
    if ht.table[index] != None:
        if insert_helper(ht.table[index], key, item):
            pass
        else:
            ht.collisions += 1
            ht.items += 1
            ht.table[index] = linked_list.add(
                ht.table[index], linked_list.length(ht.table[index]),
                (key, item))
    else:
        ht.table[index] = linked_list.add(ht.table[index],
                                          linked_list.length(ht.table[index]),
                                          (key, item))
        ht.items += 1
    if ht.items / ht.size > 1.5:
        new_table = HashTable(ht.size * 2, ht.items, [None] * (ht.size * 2), 0)
        for item in ht.table:
            if item != None:
                if linked_list.length(item) > 1:
                    for item_index in range(linked_list.length(item)):
                        new_index = hash(linked_list.get(
                            item, item_index)[0]) % new_table.size
                        if new_table.table[new_index] != None:
                            new_table.collisions += 1
                            new_table.table[new_index] = linked_list.add(
                                new_table.table[new_index],
                                linked_list.length(new_table.table[new_index]),
                                linked_list.get(item, item_index))
                        else:
                            new_table.table[new_index] = linked_list.Pair(
                                linked_list.get(item, item_index), None)
                else:
                    new_index = hash(item.first[0]) % new_table.size
                    #                    if new_table.table[new_index] != None:
                    #                        if insert_helper(new_table.table[new_index], item.first[0], item.first[1]):
                    #                            pass
                    #                        else:
                    #                            collisions += 1
                    #                            new_table.table[new_index] = linked_list.Pair(item.first, None)
                    #                    else:
                    new_table.table[new_index] = linked_list.Pair(
                        item.first, None)
        ht = new_table
    return ht
Esempio n. 4
0
 def peek(self):
     """Returns the value at the top of the Stack
     Args:
         self (StackArray): The Stack
     Returns:
         (int): value on top of stack
     """
     if self.top is None:
         raise IndexError('')
     return linked_list.get(self.top, self.num_items - 1)
Esempio n. 5
0
def get(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:
                return itemref[1]
        raise LookupError
    return ht.table[index].first[1]
Esempio n. 6
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)
Esempio n. 7
0
 def peek(self):
     """method that tells us what the item at the top of the stack is
     Args:
         No args
     Returns:
         item (any type) : the item (data) at the top of the stack, without
                           removing it
     Raises:
         IndexError : when the stack is empty
     """
     if self.is_empty():
         raise IndexError
     return llist.get(self.top, 0)
Esempio n. 8
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
Esempio n. 9
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
Esempio n. 10
0
 def peek(self):
     """Write signature and purpose
     """
     if self.is_empty():
         raise IndexError
     return linked_list.get(self.top, self.num_items - 1)
Esempio n. 11
0
 def test_get(self):
     self.assertEqual(get(Node(1, Node(2, None)), 1), 2)
     self.assertEqual(get(Node(20, Node(12, None)), 0), 20)
     self.assertRaises(IndexError, get, Node(1, Node(2, None)), 7)