Module dsa.huffman

Module to access functions for Huffman Compression.

Functions

def bitstring_to_bytes(s: str) ‑> bytes

Convert a bitstring to bytes.

Args

s : str
The bitstring.

Returns

Bitstring converted to bytes.

def build_frequency_table(s: str) ‑> PriorityQueue

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

Args

s : str
The string to encode.

Returns

A priority queue of the characters based on frequencies.

def build_huffman_dictionary(node: TreeNode, bit_string: str = '') ‑> dict

Given a TreeNode, build a Huffman Dictionary.

Args

node : TreeNode
The Huffman Node.
bit_string : str
The bit string.

Returns

A Huffman Dictionary.

def build_huffman_tree(pq: PriorityQueue) ‑> Tree

Accepts a priority queue and returns a Huffman Tree.

Args

pq : PriorityQueue
A PriorityQueue containing TreeNodes of characters based on frequencies.

Returns

A Huffman Tree.

def bytes_to_bitstring(ba: bytes, bitlength: int = 8) ‑> str

Convert bytes to bitstring.

Args

ba : bytes
The bytes to convert.
bitlength : int
The bit length.

Returns

The bytes converted to bitstring.

def character_frequency(s: str) ‑> dict

Takes a string a returns a dictionary of character frequencies.

Args

s : str
The string to analyze.

Returns

Dictionary containing character frequency.

def huffman_decode(encoded_data: str, tree: Tree) ‑> str

Decode the encoded data using the Huffman Tree.

Args

encoded_data : str
The encoded data.
tree : Tree
The Huffman Tree.

Returns

The decoded data.

def huffman_encode(st: str, hd: dict) ‑> str

Encode the string using the Huffman Dictionary.

Args

st : str
The string to encode.
hd : dict
The Huffman Dictionary.

Returns

The encoded string.