Exemple #1
0
def read_tree (bitreader):
    '''Read a description of a Huffman tree from the given bit reader,
    and construct and return the tree. When this function returns, the
    bit reader should be ready to read the next bit immediately
    following the tree description.

    Huffman trees are stored in the following format:
      * TreeLeafEndMessage is represented by the two bits 00.
      * TreeLeaf is represented by the two bits 01, followed by 8 bits
          for the symbol at that leaf.
      * TreeBranch is represented by the single bit 1, followed by a
          description of the left subtree and then the right subtree.

    Args:
      bitreader: An instance of bitio.BitReader to read the tree from.

    Returns:
      A Huffman tree constructed according to the given description.
    '''
    #the function uses recursion to build from the bottom up
    first_bit = bitreader.readbit()
    if first_bit == 1:
        #create a tree branch
        return huffman.TreeBranch(read_tree(bitreader), read_tree(bitreader))
    else:
        second_bit = bitreader.readbit()
        if second_bit == 0:
            #reached end of tree
            return huffman.TreeLeafEndMessage()
        else:
            #create a tree leaf
            return huffman.TreeLeaf(bitreader.readbits(8))
Exemple #2
0
def read_tree(bitreader):
    # bit == 1, TreeBranch
    if bitreader.readbit() == 1:
        tree = huffman.TreeBranch(read_tree(bitreader), read_tree(bitreader))
        return tree
    # bit == 0, could be either TreeLeaf(value) or TreeLeafEndMessage()
    else:
        # bit == 1, 01 - TreeLeaf(value)
        if bitreader.readbit() == 1:
            # read 8 bits for the symbol at this leaf
            value = bitreader.readbits(8)
            leaf = huffman.TreeLeaf(value)
            return leaf
        # bit == 0, 00 - TreeLeafEndMessage()
        else:
            endMsg = huffman.TreeLeafEndMessage()
            return endMsg
Exemple #3
0
    def get_branch(bitreader, tree=None):
        bit = bitreader.readbit()

        if bit == 1:

            left = get_branch(bitreader)
            right = get_branch(bitreader)
            tree = huffman.TreeBranch(left, right)
            return tree

        elif bit == 0:
            bit = bitreader.readbit()

            if bit == 1:
                byte = bitreader.readbits(8)
                leaf = huffman.TreeLeaf(byte)

            elif bit == 0:
                leaf = huffman.TreeLeafEndMessage()

            return leaf
def read_tree(bitreader):
    '''Read a description of a Huffman tree from the given bit reader,
    and construct and return the tree. When this function returns, the
    bit reader should be ready to read the next bit immediately
    following the tree description.

    Huffman trees are stored in the following format:
      * TreeLeafEndMessage is represented by the two bits 00.
      * TreeLeaf is represented by the two bits 01, followed by 8 bits
          for the symbol at that leaf.
      * TreeBranch is represented by the single bit 1, followed by a
          description of the left subtree and then the right subtree.

    Args:
      bitreader: An instance of bitio.BitReader to read the tree from.

    Returns:
      A Huffman tree constructed according to the given description.
    '''
    tree_dict = {  # maps the bit sequence to the tree instance
        '00': huffman.TreeLeafEndMessage(),
        '01': lambda i: huffman.TreeLeaf(i),
        '1': lambda l, r: huffman.TreeBranch(l, r)
    }

    b1 = bitreader.readbit()  # read first bit
    if b1 == 1:  # if first bit is a 1 it must be a branch
        left = read_tree(bitreader)  # apply recursively over left and right
        right = read_tree(bitreader)  # branch
        tree = tree_dict['1'](left, right)
    else:  # otherwise its either a endLeaf or valueLeaf
        b2 = bitreader.readbit()
        b = b1 + b2
        if b == 0:
            tree = tree_dict['00']
        elif b == 1:
            tree = tree_dict['01'](bitreader.readbits(8))
    # print(tree)
    return tree
Exemple #5
0
def read_tree(bitreader):
    '''Read a description of a Huffman tree from the given bit reader,
    and construct and return the tree. When this function returns, the
    bit reader should be ready to read the next bit immediately
    following the tree description.

    Huffman trees are stored in the following format:
      * TreeLeafEndMessage is represented by the two bits 00.
      * TreeLeaf is represented by the two bits 01, followed by 8 bits
          for the symbol at that leaf.
      * TreeBranch is represented by the single bit 1, followed by a
          description of the left subtree and then the right subtree.

    Args:
      bitreader: An instance of bitio.BitReader to read the tree from.

    Returns:
      A Huffman tree constructed according to the given description.
    '''
    bit = bitreader.readbit()
    # if the the first bit is 0, need to read the next bit
    if bit == 0:
        bit = bitreader.readbit()
        if bit == 1:
            # if the combination is 01, create a leaf with the next byte
            byte = bitreader.readbits(8)
            tree = huffman.TreeLeaf(byte)
        else:
            # if the combination is 00, create an end message
            tree = huffman.TreeLeafEndMessage()

    else:  # if the first bit is 1, create a branch and recurse on the left side
        # then the right side
        tree = huffman.TreeBranch(read_tree(bitreader), read_tree(bitreader))

    return tree