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

Constructs and manages a Huffman tree for encoding. More...

#include <Huffman.hpp>

Collaboration diagram for HuffmanTree:
Collaboration graph

Public Member Functions

void encode_vec (vector< uint8_t > &vec)
 Encodes a vector of bytes in-place using the Huffman codes.
 
void encodeFile (const string &filename)
 Reads a file and encodes its contents using the Huffman tree.
 

Public Attributes

unique_ptr< HuffmanNoderoot
 Pointer to the root node of the Huffman tree.
 
unordered_map< uint8_t, bitset< 32 > > code8b
 Map of 8-bit codes for each character.
 
unordered_map< uint8_t, bitset< 32 > > code16b
 Map of 16-bit codes for each character.
 
unordered_map< uint8_t, bitset< 32 > > code32b
 Map of 32-bit codes for each character.
 

Private Member Functions

unordered_map< uint8_t, uint64_t > count_characters (const vector< uint8_t > &str)
 Counts the frequency of each byte in the input string.
 
unique_ptr< HuffmanNodecreate_tree (priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodes > &pq)
 Creates a Huffman tree from a given min-heap (priority queue).
 
priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodescreate_min_heap ()
 Creates a min-heap of Huffman nodes from the frequency map.
 
void create_code (const unique_ptr< HuffmanNode > &p, bitset< 32 > unique_code)
 Recursively generates the code for each character from the tree.
 
void create_codes ()
 Initializes the code tables for all 8, 16, and 32-bit representations.
 
void print_codes ()
 Prints all the generated codes to standard output.
 

Private Attributes

unordered_map< uint8_t, uint64_t > frecuencies
 Stores the frequency of each byte (character) in the input.
 

Detailed Description

Constructs and manages a Huffman tree for encoding.

This class builds a Huffman tree from a byte vector, generates codes for each byte, and can encode input data using 8, 16, or 32-bit representations. It is optimized for educational demonstration purposes and supports visualizing and printing the generated codes.

Member Function Documentation

◆ count_characters()

unordered_map< uint8_t, uint64_t > HuffmanTree::count_characters ( const vector< uint8_t > & str)
private

Counts the frequency of each byte in the input string.

Parameters
strVector of bytes to analyze.
Returns
Map from byte to frequency count.
Here is the caller graph for this function:

◆ create_code()

void HuffmanTree::create_code ( const unique_ptr< HuffmanNode > & p,
bitset< 32 > unique_code )
private

Recursively generates the code for each character from the tree.

Parameters
pPointer to current node.
unique_codeCurrent prefix code.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_codes()

void HuffmanTree::create_codes ( )
private

Initializes the code tables for all 8, 16, and 32-bit representations.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_min_heap()

priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodes > HuffmanTree::create_min_heap ( )
private

Creates a min-heap of Huffman nodes from the frequency map.

Returns
Priority queue containing leaf nodes for the tree.
Here is the caller graph for this function:

◆ create_tree()

unique_ptr< HuffmanNode > HuffmanTree::create_tree ( priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodes > & pq)
private

Creates a Huffman tree from a given min-heap (priority queue).

Parameters
pqA priority queue of Huffman nodes sorted by frequency.
Returns
Unique pointer to the root node of the created tree.
Here is the caller graph for this function:

◆ encode_vec()

void HuffmanTree::encode_vec ( vector< uint8_t > & vec)

Encodes a vector of bytes in-place using the Huffman codes.

Parameters
vecVector of bytes to encode.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encodeFile()

void HuffmanTree::encodeFile ( const string & filename)

Reads a file and encodes its contents using the Huffman tree.

Parameters
filenamePath to the input file.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_codes()

void HuffmanTree::print_codes ( )
private

Prints all the generated codes to standard output.

Member Data Documentation

◆ code16b

unordered_map<uint8_t, bitset<32> > HuffmanTree::code16b

Map of 16-bit codes for each character.

◆ code32b

unordered_map<uint8_t, bitset<32> > HuffmanTree::code32b

Map of 32-bit codes for each character.

◆ code8b

unordered_map<uint8_t, bitset<32> > HuffmanTree::code8b

Map of 8-bit codes for each character.

◆ frecuencies

unordered_map<uint8_t, uint64_t> HuffmanTree::frecuencies
private

Stores the frequency of each byte (character) in the input.

Used to build the Huffman tree.

◆ root

unique_ptr<HuffmanNode> HuffmanTree::root

Pointer to the root node of the Huffman tree.


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