def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' # Your code stack = [] temp_list = freq_table.items() modheap.initialize_heap(True, 1, cmp_freq) modheap.import_list(temp_list) modheap.heapify() while(len(temp_list) > 1): tup1 = modheap.pop() tup2 = modheap.pop() tup3 = tup1[0]+tup2[0], tup1[1]+tup2[1] stack.append((tup1[0], tup1[1], tup3[0], '1')) stack.append((tup2[0], tup2[1], tup3[0], '0')) modheap.add(tup3) temp_list = modheap.DATA return stack, stack[-1]
def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' modheap.initialize_heap(True, arity_exp, cmp_freq) modheap.import_list([(char, freq) for char, freq in freq_table.items()]) modheap.heapify() code_stack = [] while(modheap.size() > 1): tuple1 = modheap.pop() tuple2 = modheap.pop() parent_symbol = tuple1[0] + tuple2[0] parent_freq = tuple1[1] + tuple2[1] modheap.add((parent_symbol, parent_freq)) code_stack.append((tuple1[0], tuple1[1], parent_symbol, '1')) code_stack.append((tuple2[0], tuple2[1], parent_symbol, '0')) return code_stack, modheap.pop()[0]
def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' # Your code s=[] freq_list=freq_table.items() modheap.initialize_heap(True, arity_exp, cmp_freq) modheap.import_list( freq_list ) modheap.restore_heap(0) while(len(modheap.DATA)>2): t1=modheap.pop() t2=modheap.pop() cs=t1[0]+t2[0] cs=' '+cs modheap.DATA.append((cs, t1[1]+t2[1])) modheap.restore_subtree(0) s.append((t1[0], t1[1], cs, '0')) s.append((t2[0], t2[1], cs, '1')) s.append((modheap.DATA[0][0], modheap.DATA[0][1], 'rt', '0')) s.append((modheap.DATA[1][0], modheap.DATA[1][1], 'rt', '1')) return s,''
def test_noparent(self, ismin, heapsize, aexp, parent, child): ''' Test if the functions correctly recognize the root of the heap (with no parent) ''' modheap.initialize_heap(ismin, aexp, CMP_FUNCTION) modheap.import_list(self.nlist(heapsize)) self.assertEqual(parent, modheap.get_parent_index(child))
def test_nochild(self, ismin, heapsize, aexp, parent, child): ''' Test if the functions correctly recognize leaves of the heap (those with no children) ''' modheap.initialize_heap(ismin, aexp, CMP_FUNCTION) modheap.import_list(self.nlist(heapsize)) self.assertEqual(child, modheap.get_leftmostchild_index(parent))
def test_indices(self, ismin, heapsize, aexp, parent, child): ''' Test max elements of the heap ''' modheap.initialize_heap(ismin, aexp, CMP_FUNCTION) modheap.import_list(self.nlist(heapsize)) self.assertEqual(child, modheap.get_leftmostchild_index(parent)) self.assertEqual(parent, modheap.get_parent_index(child))
def test_min(self, lst): ''' Test min elements of the heap ''' modheap.initialize_heap(True, 2, CMP_FUNCTION) modheap.import_list(lst) modheap.heapify() lst.sort() self.assertEqual(lst[0], modheap.pop()) self.assertEqual(lst[1], modheap.pop())
def test_max(self, lst): ''' Test max elements of the heap ''' modheap.initialize_heap(False, 1, CMP_FUNCTION) modheap.import_list(lst) modheap.heapify() lst.sort() self.assertEqual(lst[-1], modheap.pop()) self.assertEqual(lst[-2], modheap.pop())
def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' # Your code modheap.initialize_heap(is_min=True, arity_exp=1, compare_fn=None) sample = [(y, x) for (x, y)in freq_table.items()] modheap.import_list(sample) modheap.heapify() sample = modheap.DATA stack = [] while(modheap.size() >2): tup1 = modheap.DATA.pop(0) modheap.heapify() tup2 = modheap.DATA.pop(0) tup3 = (tup1[0] +tup2[0], tup1[1] +tup2[1]) tup1 = (tup1[1], tup1[0], tup3[1], '0') tup2 = (tup2[1], tup2[0], tup3[1], '1') modheap.add(tup3) modheap.heapify() stack += [tup1, tup2] tup1 = modheap.DATA.pop(0) tup2 = modheap.DATA.pop(0) tup3 = (tup1[0] + tup2[0], tup1[1] + tup2[1]) tup1 = (tup1[1], tup1[0], tup3[1], '0') tup2 = (tup2[1], tup2[0], tup3[1], '1') stack += [tup1, tup2] modheap.heapify() return stack , tup3
def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' # Your code stack = [] for i in freq_table: stack.append([freq_table[i], i]) modheap.initialize_heap(True, arity_exp) modheap.import_list(stack) modheap.heapify() new_stack = [] while(modheap.size()>= 2): tup1 = modheap.pop() tup2 = modheap.pop() add = [tup1[0]+tup2[0], tup1[1]+tup2[1]] modheap.add(add) new_stack.append([tup1[1], tup1[0], add[1], ""]) new_stack.append([tup2[1], tup2[0], add[1], ""]) new_stack.extend(modheap.DATA) new_stack[-1] = [new_stack[-1][1]]+new_stack[-1]+[""] new_stack[-1][2] = "" for j in new_stack: if(len(j[0]) == 1): form_codes(j, new_stack) for i in new_stack: if(len(i[0]) == 1): codes[i[0]] = i[3] return new_stack[:-1], new_stack[-1]
def build_huffman_tree(freq_table, arity_exp): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' new_list = [] for i in freq_table: new_list.append((i, freq_table[i])) modheap.initialize_heap(True, arity_exp , cmp_freq) modheap.import_list(new_list) modheap.heapify() stack = [] while(modheap.size()>1): element1 = modheap.pop() element2 = modheap.pop() modheap.add((element2[0]+element1[0], element1[1]+element2[1])) stack.append((element1[0], element1[1], element2[0]+element1[0], '1')) stack.append((element2[0], element2[1], element2[0]+element1[0], '0')) #stack.append((modheap.DATA[0][0], modheap.DATA[0][1], "root", '1')) #stack.append((modheap.DATA[1][0], modheap.DATA[1][1], "root", '0')) return stack,modheap.DATA[0][0] print stack,modheap.DATA[0][0]
def build_huffman_tree( freq_table, arity_exp ): ''' Build the huffman tree for the input textfile and return a stack (maybe along with the character at the root node) Algo: (i) Start by making a heap out of freq_table using modheap (ii) Pop two elements out of the heap at a time (iii) Form a composite character that is the concatenation of the two and the combined frequency of the two (iv) Add the new composite character with its frequency to the heap (v) Add the two popped elements to a stack - simply append to a list Elements of the stack are of the form (element, frequency, parent, additional_code_bit) (vi) Repeat the above four steps till the heap has only the root element left (vii) Return the stack along with the top root element of the heap ''' # Your code # temp = [] stack = [] temp = [(y,x) for (x,y) in freq_table.items()] modheap.initialize_heap(True, arity_exp, cmp_freq) modheap.import_list( temp ) modheap.heapify() heap = modheap.DATA while modheap.size() > 1 : heap[0] , heap[len(heap)-1] = heap[len(heap) - 1] , heap[0] t1 = heap.pop() modheap.heapify() heap = modheap.DATA heap[0], heap[len(heap) - 1] = heap[len(heap) - 1], heap[0] t2 = heap.pop() composite = (t1[0] + t2[0], t1[1] + t2[1]) t1 = (t1[1],t1[0],composite[1],'0') t2 = (t2[1],t2[0],composite[1],'1') heap += [ composite ] modheap.heapify() heap = modheap.DATA stack += [ t1 , t2 ] return stack, heap[0]
def make_heap(lst): modheap.initialize_heap(True, 1, COMPARE_FN) modheap.import_list(lst) modheap.heapify() return modheap.DATA