My Project
Loading...
Searching...
No Matches
Huffman Class Reference

Handles the complete lifecycle of Huffman encoding and decoding. More...

#include <Huffman.h>

Public Member Functions

void computeHistogram (const std::string &inputFilePath, uint64_t histogram[ALPHABET_SIZE])
 Computes the frequency of each byte in the input file.
NodebuildHuffmanTree (const uint64_t histogram[ALPHABET_SIZE])
 Builds the optimal Huffman tree using a priority queue.
void generateCodeTable (Node *root, Code &currentNode, Code codeTable[ALPHABET_SIZE])
 Generates the binary codes for all characters by traversing the tree.
NodereconstructTree (const uint8_t tree_dump[], uint32_t dump_size)
 Rebuilds a Huffman tree from its serialized post-order dump.
void writeTreeDump (Node *root, std::ofstream &outFile)
 Serializes the Huffman tree to a file using post-order traversal.
void compressFile (const std::string &inputFilePath, const std::string &outputFilePath)
 Compresses an input file using Huffman encoding.
void decompressFile (const std::string &inputFilePath, const std::string &outputFilePath)
 Decompresses a Huffman encoded file back to its original state.

Detailed Description

Handles the complete lifecycle of Huffman encoding and decoding.

Member Function Documentation

◆ buildHuffmanTree()

Node * Huffman::buildHuffmanTree ( const uint64_t histogram[ALPHABET_SIZE])

Builds the optimal Huffman tree using a priority queue.

Parameters
histogramThe pre-computed array of character frequencies.
Returns
Pointer to the root node of the generated Huffman tree.

◆ compressFile()

void Huffman::compressFile ( const std::string & inputFilePath,
const std::string & outputFilePath )

Compresses an input file using Huffman encoding.

Parameters
inputFilePathPath to the original file to compress.
outputFilePathPath where the compressed binary file (.huff) will be saved.

◆ computeHistogram()

void Huffman::computeHistogram ( const std::string & inputFilePath,
uint64_t histogram[ALPHABET_SIZE] )

Computes the frequency of each byte in the input file.

Parameters
inputFilePathPath to the file to be analyzed.
histogramArray of size ALPHABET_SIZE where frequencies will be stored.

◆ decompressFile()

void Huffman::decompressFile ( const std::string & inputFilePath,
const std::string & outputFilePath )

Decompresses a Huffman encoded file back to its original state.

Parameters
inputFilePathPath to the compressed file (.huff).
outputFilePathPath where the restored data will be written (or /dev/stdout).

◆ generateCodeTable()

void Huffman::generateCodeTable ( Node * root,
Code & currentNode,
Code codeTable[ALPHABET_SIZE] )

Generates the binary codes for all characters by traversing the tree.

Parameters
rootPointer to the root of the Huffman tree.
currentNodeA temporary Code object used during traversal (must be empty initially).
codeTableArray where the resulting codes will be stored, indexed by character.

◆ reconstructTree()

Node * Huffman::reconstructTree ( const uint8_t tree_dump[],
uint32_t dump_size )

Rebuilds a Huffman tree from its serialized post-order dump.

Parameters
tree_dumpArray containing the 'L' (Leaf) and 'T' (Interior) byte sequence.
dump_sizeThe length of the tree_dump array.
Returns
Pointer to the root of the reconstructed Huffman tree.

◆ writeTreeDump()

void Huffman::writeTreeDump ( Node * root,
std::ofstream & outFile )

Serializes the Huffman tree to a file using post-order traversal.

Parameters
rootPointer to the root of the tree to be dumped.
outFileThe output file stream where the tree will be written.

The documentation for this class was generated from the following files: