dsa.huffman module

Module to access functions for Huffman Compression.

dsa.huffman.bitstring_to_bytes(s)

Convert a bitstring to bytes.

Parameters:

s (str) – The bitstring.

Return type:

bytes

Returns:

Bitstring converted to bytes.

dsa.huffman.build_frequency_table(s)

Accepts a string to encode and returns a heap of the characters.

Parameters:

s (str) – The string to encode.

Return type:

PriorityQueue

Returns:

A priority queue of the characters based on frequencies.

dsa.huffman.build_huffman_dictionary(node, bit_string='')

Given a TreeNode, build a Huffman Dictionary.

Parameters:
  • node (TreeNode) – The Huffman Node.

  • bit_string (str) – The bit string.

Return type:

dict

Returns:

A Huffman Dictionary.

dsa.huffman.build_huffman_tree(pq)

Accepts a priority queue and returns a Huffman Tree.

Parameters:

pq (PriorityQueue) – A PriorityQueue containing TreeNodes of characters based on frequencies.

Return type:

Tree

Returns:

A Huffman Tree.

dsa.huffman.bytes_to_bitstring(ba, bitlength=8)

Convert bytes to bitstring.

Parameters:
  • ba (bytes) – The bytes to convert.

  • bitlength (int) – The bit length.

Return type:

str

Returns:

The bytes converted to bitstring.

dsa.huffman.character_frequency(s)

Takes a string a returns a dictionary of character frequencies.

Parameters:

s (str) – The string to analyze.

Return type:

dict

Returns:

Dictionary containing character frequency.

dsa.huffman.huffman_decode(encoded_data, tree)

Decode the encoded data using the Huffman Tree.

Parameters:
  • encoded_data (str) – The encoded data.

  • tree (Tree) – The Huffman Tree.

Return type:

str

Returns:

The decoded data.

dsa.huffman.huffman_encode(st, hd)

Encode the string using the Huffman Dictionary.

Parameters:
  • st (str) – The string to encode.

  • hd (dict) – The Huffman Dictionary.

Return type:

str

Returns:

The encoded string.