Пример #1
0
def __parcours(L, i=0):
    res = ""
    if (L[i] == '1'):
        for j in range(8):
            res += L[i + j + 1]
        temp = chr(__todecimal(res))
        return (bintree.BinTree(temp, None, None), i + 9)
    else:
        (a, x) = __parcours(L, i + 1)
        (c, x2) = __parcours(L, x)
        return (bintree.BinTree(None, a, c), x2)
Пример #2
0
def buildHuffmantree(inputList):
    """
    Processes the frequency list into a Huffman tree according to the algorithm.
    """
    H = heap.Heap()
    l = len(inputList)
    for i in inputList:
        H.push((i[0], bintree.BinTree(i[1], None, None)))
    while (l > 1):
        B = H.pop()
        B1 = H.pop()
        H.push((B[0] + B1[0], bintree.BinTree(None, B[1], B1[1])))
        l = l - 1
    return (H.pop()[1])
Пример #3
0
def __list2bst(L, left, right):
    if left == right:
        return None
    else:
        mid = left + (right - left) // 2
        return bintree.BinTree(L[mid], __list2bst(L, left, mid),
                               __list2bst(L, mid + 1, right))
Пример #4
0
def __2minlist(inputList):
    """
    :param inputList: bintree list (result of __listtree(inputList))
    :return: input list but with a new bintree created from the 2 smallest frequency of the list
    """
    # first min
    mini1 = inputList[0]
    mini1freq = mini1.key[0]
    length = len(inputList)
    index = 0
    for i in range(length):
        if inputList[i].key[0] < mini1freq:
            mini1 = inputList[i]
            mini1freq = mini1.key[0]
            index = i
    (inputList[index], inputList[length - 1]) = (inputList[length - 1],
                                                 inputList[index])
    mini1 = inputList.pop()
    # second min
    mini2 = inputList[0]
    mini2freq = mini2.key[0]
    index = 0
    length = len(inputList)
    for i in range(length):
        if inputList[i].key[0] < mini2freq:
            mini2 = inputList[i]
            mini2freq = mini2.key[0]
            index = i
    (inputList[index], inputList[length - 1]) = (inputList[length - 1],
                                                 inputList[index])
    mini2 = inputList.pop()
    inputList.append(
        bintree.BinTree((mini1freq + mini2freq, None), mini1, mini2))
    return inputList
Пример #5
0
def hier2tree(L, i = 1):
    if i >= len(L) or L[i] == None:
        return None
    else:
        C = bintree.BinTree(L[i], None, None)
        C.left = hier2tree(L, 2*i)
        C.right = hier2tree(L, 2*i+1)
        return C
Пример #6
0
def copy(B):
    if B == None:
        return None
    else:
        C = bintree.BinTree(B.key, None, None)
        C.left = copy(B.left)
        C.right = copy(B.right)
        return C
Пример #7
0
def __list2bst(L, left, right):
    if left == right:
        return None
    else:
        mid = left + (right - left) // 2
        B = bintree.BinTree(L[mid], None, None)
        B.left = __list2bst(L, left, mid)
        B.right = __list2bst(L, mid + 1, right)
        return B
Пример #8
0
def leaf_insert(B, x):
    if B == None:
        return bintree.BinTree(x, None, None)
    else:
        if x <= B.key:
            B.left = leaf_insert(B.left, x)
        else:
            B.right = leaf_insert(B.right, x)
        return B
Пример #9
0
def __list2bst2(L, left, right):
    if left == right:
        return None
    else:
        mid = left + (right - left) // 2
        while mid < right - 1 and L[mid] == L[mid + 1]:
            mid += 1
        return bintree.BinTree(L[mid], __list2bst2(L, left, mid),
                               __list2bst2(L, mid + 1, right))
Пример #10
0
def __buildTree(L, i=0):
    if i >= len(L) or L[i] == None:
        return (None, i+1)
    else:
        B = bintree.BinTree(L[i], None, None)
        i = i + 1
        (B.left, i) = __buildTree(L, i)        
        (B.right, i) = __buildTree(L, i)
        return (B, i)
Пример #11
0
def __listtree(inputList):
    """
    :param inputList: list of Tuple(frequency, char)
    :return: a bintree list where each bintree is a leaf and have a Tuple(frequency, char) as key
    """
    for i in range(len(inputList)):
        inputList[i] = bintree.BinTree((inputList[i][0], inputList[i][1]),
                                       None, None)
    return inputList
Пример #12
0
def insertBST(x, B):
    if B == None:
        return bintree.BinTree(x, None, None)
    else:
        if x != B.key:
            if x < B.key:
                B.left = insertBST(x, B.left)
            else:
                B.right = insertBST(x, B.right)
        return B
Пример #13
0
def __buildTreeS(L):
    if L == []:
        return None
    else:
        e = L.pop()
        if e == None:
            return None
        else:
            B = bintree.BinTree(e, None, None)
            B.left = __buildTreeS(L)    
            B.right = __buildTreeS(L)
            return B
Пример #14
0
def __decodetree(charlist, structure):
    """
    :param dataIN: the encoded data
    :param B: the new tree
    :param index: the index to go through the data
    :return: the tree
    """
    if len(structure) <= 0:
        return None
    index_struct = len(structure) - 1
    if structure[index_struct] == '1':
        structure.pop()
        index = len(charlist) - 1
        key = charlist[index]
        charlist.pop()
        B = bintree.BinTree(key, None, None)
        return B
    elif structure[index_struct] == '0':
        structure.pop()
        B = bintree.BinTree(None, None, None)
        B.left = __decodetree(charlist, structure)
        B.right = __decodetree(charlist, structure)
        return B
    return None
Пример #15
0
def leaf_insert_iter(B, x):
    new = bintree.BinTree(x, None, None)
    P = None
    T = B
    while T != None:
        P = T
        if x <= T.key:
            T = T.left
        else:
            T = T.right

    if P == None:
        return new
    else:
        if x <= P.key:
            P.left = new
        else:
            P.right = new
        return B
Пример #16
0
def copy(B):
    if B == None:
        return None
    return bintree.BinTree(B.key, copy(B.left), copy(B.right))
Пример #17
0
def root_insertion(B, x):
    (L, R) = cut(B, x)
    return bintree.BinTree(x, L, R)