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 ''' code_stack = [] modheap.initialize_heap(True, arity_exp, cmp_freq) modheap.DATA = freq_table.items() modheap.heapify() while(len(modheap.DATA)>2): element1 = modheap.pop() element2 = modheap.pop() father = ( element1[0]+element2[0], element1[1]+element2[1] ) modheap.add(father) code_stack.append((element1[0], element1[1], father[0], '0' )) code_stack.append((element2[0], element2[1], father[0], '1' )) code_stack.append((modheap.DATA[0][0], modheap.DATA[0][1], "root", '0')) code_stack.append((modheap.DATA[1][0], modheap.DATA[1][1], "root", '1')) return code_stack , ''
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 ''' stack=[] modheap.initialize_heap(True,arity_exp,cmp_freq) while(len(modheap.DATA)>1): tup1=modheap.pop() tup2=modheap.pop() combine_char=tup1[0]+tup2[0] combine_freq=tup1[1]+tup2[1] modheap.DATA.append((combine_char,combine_freq)) modheap.heapify() stack.append((tup1[0],tup1[1],combine_char,'1')) stack.append((tup2[0],tup2[1],combine_char,'0')) return stack,modheap.DATA[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 s\tack along with the top root element of the heap ''' # Your code freq_list = [] for letter, number in freq_table.items(): freq_list.append( (letter, number) ) modheap.initialize_heap( True, arity_exp, cmp_freq ) modheap.DATA = freq_list modheap.heapify() stack = [] while(modheap.size()>1): element1 = modheap.pop() element2 = modheap.pop() parent = element1[0] + element2[0] combined_freq = element1[1] + element2[1] modheap.add( (parent, combined_freq) ) stack.append((element1[0], element1[1], parent, '0')) stack.append((element2[0], element2[1], parent, '1')) return stack, modheap.DATA[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 modheap.initialize_heap(True, arity_exp) for i in freq_table: modheap.add((freq_table[i], i)) modheap.heapify() pop_stack = [] length = modheap.size() while length > 1 : element1 = modheap.pop() element2 = modheap.pop() composite = (element1[0]+element2[0], element1[1]+element2[1]) modheap.add(composite) pop_stack.append((element1[1], element1[0], composite[1], '1')) pop_stack.append((element2[1], element2[0], composite[1], '0')) length = length-1 return pop_stack, modheap.DATA[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 stack=[] lst=freq_table.items() modheap.initialize_heap(True ,arity_exp , cmp_freq) modheap.import_list(lst) modheap.heapify() while modheap.size()>1: item1=modheap.pop() item2=modheap.pop() character_concat=(item1[0]+item2[0],item1[1]+item2[1]) modheap.DATA.append(character_concat) modheap.heapify() stack.append((item1[0],item1[1],character_concat[0],'1')) stack.append((item2[0],item2[1],character_concat[0],'0')) return 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 ''' modheap.initialize_heap(True, arity_exp) for i in freq_table: modheap.add((freq_table[i], i)) modheap.heapify() pop_stack = [] length = modheap.size() while length > 1 : element1 = modheap.pop() element2 = modheap.pop() composite = (element1[0]+element2[0], element1[1]+element2[1]) modheap.add(composite) pop_stack.append((element1[1], element1[0], composite[1], '1')) pop_stack.append((element2[1], element2[0], composite[1], '0')) length = length-1 return pop_stack, modheap.DATA[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 ''' modheap.initialize_heap(True,arity_exp,cmp_freq) modheap.DATA=freq_table.values() list0=freq_table.items() stack=[] i=0 while(len(modheap.DATA)>1): n=0 m=0 modheap.heapify() print modheap.DATA a=modheap.pop() b=modheap.pop() for letter,frequency in list0: if(frequency==a): a1=letter k1=n else: n=n+1 if(frequency==b): b1=letter k2=m else: m=m+1 if(k2<k1): del list0[k1] del list0[k2] if(k2>k1): del list0[k2] del list0[k1] list0.append((a1+b1,a+b)) stack=stack+[(a1,a,0)]+[(b1,b,1)] dict2=dict(list0) modheap.DATA=dict2.values() modheap.DATA=stack pi=modheap.get_parent_index(i) data=[] while(i<=len(stack)-1): if(pi!=None): data=data+[stack[i][:-1]+(stack[pi][0],)+(stack[i][-1],)] if(pi==None): data=data+[stack[i][:-1]+('None',)+(stack[i][-1],)] i=i+1 pi=modheap.get_parent_index(i) return data
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 ''' modheap.initialize_heap(is_min = True, arity_exp = 1, compare_fn = None) final_list = [(y,x) for (x,y) in freq_table.items()] stack = [] modheap.DATA = final_list modheap.heapify() while(len(final_list) > 1): a = modheap.pop() modheap.heapify() b = modheap.pop() modheap.heapify() new = (a[0]+b[0], a[1]+b[1]) stack.append((a[1], a[0], a[1] + b[1], '0')) stack.append((b[1], b[0], a[1] + b[1], '1')) final_list.append(new) return (stack,final_list[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 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 ''' # Your code 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()>2): peer1 = modheap.pop() peer2 = modheap.pop() stack.append((peer1[0], peer1[1], peer2[0]+peer1[0], '1')) stack.append((peer2[0], peer2[1], peer2[0]+peer1[0], '0')) modheap.add((peer2[0]+peer1[0], peer1[1]+peer2[1])) stack.append((modheap.DATA[0][0], modheap.DATA[0][1], "root", '1')) stack.append((modheap.DATA[1][0], modheap.DATA[1][1], "root", '0')) print stack return stack
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 # 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