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 xs 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 xs to a stack - simply append to a list
    xs of the stack are of the form (x, frequency, parent, additional_code_bit)
    (vi) Repeat the above four steps till the heap has only the root x left
    (vii) Return the stack along with the top root x of the heap
    '''
    # Your code
    modheap.initialize_heap(True, arity_exp, cmp_freq)
    for every_elem in freq_table:
        tup = (freq_table[every_elem], every_elem)
        modheap.add(tup)
    stack = []
    while(len(modheap.DATA) >= 2):
        elem1 = modheap.pop()
        elem2 = modheap.pop()
        tup1 = (elem1[1], elem1[0], elem1[1]+elem2[1], '1')
        tup2 = (elem2[1], elem2[0], elem1[1]+elem2[1], '0')
        stack.append(tup1)
        stack.append(tup2)
        composite_elem = (elem1[0]+elem2[0], elem1[1]+elem2[1])
        modheap.add(composite_elem)
    lastelem = modheap.DATA[0]
    last = (lastelem[0], lastelem[1], None, '')
    return stack, last
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
    '''
    
    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 ofdef 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 = []
    freq_tuples = freq_table.items()
    modheap.initialize_heap (True, arity_exp, cmp_freq)
    for elem in freq_tuples:
        modheap.add(elem)
        
    while(len(modheap.DATA)>2):
        pop1 = modheap.pop()
        pop2 = modheap.pop()
        
        stack.append((pop1[0], pop1[1], pop2[0]+pop1[0], 1))
        stack.append((pop2[0], pop2[1], pop2[0]+pop1[0], 0))
        modheap.add((pop2[0]+pop1[0], pop1[1]+pop2[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))
    top_root_elem = (modheap.DATA[1][0] + modheap.DATA[0][0], modheap.DATA[0][1] + modheap.DATA[1][1], None, '')
    return stack, top_root_elem
コード例 #5
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
    '''
    # 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 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 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
    '''
    
    modheap.initialize_heap(True, arity_exp, cmp_freq)
    for elem in freq_table:
        modheap.add((elem, freq_table[elem]))
    stack = []
    
    while (modheap.size()>1):
        
        (char1, freq1), (char2, freq2) = modheap.pop(), modheap.pop()
        new_char,  new_char_freq = char1+char2, freq1+freq2
        modheap.add((new_char, new_char_freq))
        stack.append((char1, freq1, new_char,'0'))
        stack.append((char2, freq2, new_char,'1'))
    return stack, 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(True , arity_exp , cmp_freq )
    tupheap = []
    stack = []
    for char in freq_table:
        #tupheap=modheap.add((x,freq_table[x]))
        tupheap = modheap.add((char , freq_table[char]))
    i = len(tupheap)
    while(i!=1):
        pop1 = modheap.pop()
        pop2 = modheap.pop()
        com_sym = pop1[0] + pop2[0]
        com_count = pop1[1] + pop2[1]
        tupheap = modheap.add((com_sym , com_count))
        stack.append((pop1[0] , pop1[1] , com_sym , '0'))
        stack.append((pop2[0] , pop2[1] , com_sym , '1'))
        i = i - 1
    
    #print stack
    print tupheap
    return stack , tupheap[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 thsorted_list = [] at 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, cmp_freq)
    
    for elem in freq_table :
        modheap.add((elem, freq_table[elem]))
               
    code_stack = []
    pop_1 = ()
    pop_2 = ()
    
    while modheap.size() > 1: 
        pop_1 = modheap.pop()
        pop_2 = modheap.pop()
                
        comb_ele  = pop_1[0] + pop_2[0]
        comb_freq = pop_1[1] + pop_2[1]
        
        modheap.add((comb_ele, comb_freq))
        
        code_stack.append((pop_1[0], pop_1[1], comb_ele, "0"))
        code_stack.append((pop_2[0], pop_2[1], comb_ele, "1"))
        
    return code_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]
コード例 #13
0
 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))
コード例 #14
0
 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 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])
コード例 #16
0
 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 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
コード例 #18
0
 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())
コード例 #19
0
 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())
コード例 #20
0
 def test_min_heap_sort(self, lst, exp2):
     '''
     Test min-heap sort
     '''
     modheap.initialize_heap(True, exp2, CMP_FUNCTION)
     for elem in lst:
         modheap.add(elem)
     lst.sort()
     sorted_list = []
     while (modheap.size() > 0):
         sorted_list.append(modheap.pop())
     self.assertEqual(lst, sorted_list)
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):
#def build_huffman_tree():
    '''
    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
    '''
    
    global TOP_PARENT
    
    stack = []
    modheap.initialize_heap(True , arity_exp)
    
    for i in freq_table:
        modheap.add((freq_table[i] , i))
    
    len_chash = len(modheap.DATA)

    while(len_chash > 1):
        
        pop1 = modheap.pop()
        pop2 = modheap.pop()
        
        comp_char = pop1[1] + pop2[1]
        comp_freq = pop1[0] + pop2[0]
        
        modheap.add((comp_freq , comp_char))
        
        stack.append((pop1[1] , pop1[0] , comp_char , '0'))
        stack.append((pop2[1] , pop2[0] , comp_char , '1'))
        
        len_chash = len_chash - 1 


    TOP_PARENT = modheap.DATA[0]
    return stack , TOP_PARENT
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
    # 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 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 make_heap(lst):
    modheap.initialize_heap(True, 1, COMPARE_FN)
    modheap.import_list(lst)
    modheap.heapify()
    return modheap.DATA