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
    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 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
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
    '''
    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
    '''
    # 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 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 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 , 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 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
    '''
    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 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
    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