Exemplo n.º 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))
Exemplo n.º 2
0
 def recurse(bitreader):
     if bitreader.readbit():  # if we get a 1, that means we have a branch
         left = recurse(bitreader)  # find left tree
         right = recurse(bitreader)  # find right tree
         return huffman.TreeBranch(left, right)  # return tree branch
     else:  # we got a 0
         if bitreader.readbit():  # we got a 01
             return huffman.TreeLeaf(bitreader.readbits(8))  # read byte
         else:
             return huffman.TreeLeaf(None)  # return empty tree leaf
Exemplo n.º 3
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
Exemplo n.º 4
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:
	* TreeLeaf is represented by the two bits 01, followed by 8 bits
	for the symbol at that leaf.
	* TreeLeaf that is None (the special "end of message" character)
	is represented by the two bits 00.
	* 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.
	'''
	#since bitreader is an instance of bitio.BitReader, we use readbit() to read first bit
	first_bit = bitreader.readbit()
	#if first bit is 1 means start from 1, it is branch
	if first_bit == 1:
		#read left sub tree until reach leaf		
		leftsub = read_tree(bitreader)
		#read right sub tree until reach leaf
		rightsub = read_tree(bitreader)
		#build the huffman tree branch
		Branch = huffman.TreeBranch(leftsub, rightsub)
		return Branch

	else:
		#there is a leaf
		second_bit = bitreader.readbit()
		#if second bit is 0 means EOF reached
		if second_bit == 0:
			#build the huffman tree leaf
			Eof = huffman.TreeLeaf(None)
			return Eof
		elif second_bit == 1:
			#if second bit is 1 means there is a character, read the bits of character
			value = bitreader.readbits(8)
			#build the huffman tree leaf and store the 8 bits
			valueleaf = huffman.TreeLeaf(value)
			return valueleaf
Exemplo n.º 5
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
Exemplo n.º 6
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.
    '''
    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
Exemplo n.º 7
0
    def recurse_read(bitreader, huffman_tree=None):
        # read the first bit
        bit_read = bitreader.readbit()

        # if the first bit is 1, it is a TreeBranch
        if bit_read == 1:
            left_leaf = recurse_read(bitreader)
            right_leaf = recurse_read(bitreader)
            huffman_tree = huffman.TreeBranch(left_leaf, right_leaf)
            return huffman_tree

        # there is two cases when the first bit is 0
        elif bit_read == 0:
            bit_read = bitreader.readbit()

            # case of a none TreeLeaf
            if bit_read == 0:
                leaf = huffman.TreeLeaf(None)
            # case of a normal TreeLeaf
            elif bit_read == 1:
                # use bitreader to get value from the following byte
                byte_read = bitreader.readbits(8)
                leaf = huffman.TreeLeaf(byte_read)
            return leaf
Exemplo n.º 8
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
Exemplo n.º 9
0
    util.write_tree(tree, writer)
    writer.flush()

with open("simple.txt", 'rb') as file:
    reader = bitio.BitReader(file)
    new_tree = util.read_tree(reader)

with open("simple.txt", 'wb') as file:
    writer = bitio.BitWriter(file)

    print("Hey bitch")
    util.write_tree(new_tree, writer)
    writer.flush()

#==============================================================================
#==============================================================================
with open("simple.txt",'wb') as f:
    f.write(b'00')
with open("simple.txt", 'rb') as file:
    reader = bitio.BitReader(file)
    tree = huffman.TreeBranch(huffman.TreeBranch(huffman.TreeLeaf(ord('A')),huffman.TreeLeaf(None)),huffman.TreeLeaf(ord('B')))
    print(util.decode_byte(tree,reader))
"""
tree = huffman.TreeBranch(
    huffman.TreeBranch(huffman.TreeLeaf(ord('A')), huffman.TreeLeaf(None)),
    huffman.TreeLeaf(ord('B')))
table = huffman.make_encoding_table(tree)
encoded = table[65]
for bit in table[None]:
    print(bit)
Exemplo n.º 10
0
 def read_treeBranch():
     return huffman.TreeBranch(read_prefix(), read_prefix())