def build_sorted_leaves(list): sorted = ll.empty_list() for i in range(0, 250): if list.values[i] != None: sorted = ll.insert_sorted(sorted, Leaf(i, list.values[i]), comes_before) return sorted
def make_character_sorted_list(occurrence_list): sorted_list = linked_list.empty_list() for i in range(array_list.length(occurrence_list)): if occurrence_list[i] != None: leaf = make_leaf(chr(i), occurrence_list[i]) sorted_list = linked_list.insert_sorted(sorted_list, leaf, comes_before) return sorted_list
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
def build_leaf(x, func): #convert array into linked list new = linked_list.empty_list() for index in range(x.capacity): if (array_list.get(x, index)) != None: leaf_node = Leaf(chr(index), array_list.get(x, index)) new = linked_list.insert_sorted(new, leaf_node, func) return new