Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Huffman.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
43#ifndef HUFFMAN_H
44#define HUFFMAN_H
45
46#include <memory>
47#include <istream>
48#include <utility>
49#include <tpl_binNodeUtils.H>
50#include <tpl_treap.H>
51#include <tpl_binHeap.H>
52#include <tpl_dynMapTree.H>
53#include <bitArray.H>
54#include <ah-errors.H>
55
56namespace Aleph {
57
58struct Huffman_Node;
59
61
63
64struct Huffman_Node : public BinHeap<size_t>::Node
65{
67
69
73
74public:
75 Huffman_Node() : BinHeap<size_t>::Node(0), bin_node(nullptr), freq_node(nullptr)
76 {
77 /* empty */
78 }
79
81 : BinHeap<size_t>::Node(0), bin_node(node), freq_node(nullptr)
82 {
83 /* empty */
84 }
85
87 { /* bin_node memory must not be released here */
88 }
89};
90
92
93static inline const size_t &get_freq(Huffman_Node *huffman_node) noexcept
94{
95 return huffman_node->get_key();
96}
97
98static inline void increase_freq(Huffman_Node *huffman_node) noexcept
99{
100 huffman_node->get_key()++;
101}
102
103static inline void set_freq(Huffman_Node *huffman_node, const size_t &freq) noexcept
104{
105 huffman_node->get_key() = freq;
106}
107
109static inline bool is_leaf(BinNode<std::string> *p) noexcept
110{
111 return LLINK(p) == nullptr and RLINK(p) == nullptr;
112}
113
120{
125
126 // The nodes of the symbols, from the last one that entered, linked by
127 // next_symbol. Undoing a generation and ending one walk them here: a
128 // walk of the map may need memory, for a map that is very deep
130
132
133 std::string end_symbol_;
134 size_t text_len_;
135
137 {
138 if (is_leaf(p))
139 {
140 const std::string &str = p->get_key();
141 codes.insert(str, BitArray(array));
142 return;
143 }
144 array.push(0);
145 build_prefix_encoding(LLINK(p), array, codes);
146 array.pop();
147 array.push(1);
148 build_prefix_encoding(RLINK(p), array, codes);
149 array.pop();
150 }
151
153 {
154 ah_domain_error_if(root_ == nullptr) << "Huffman encoding tree has not been generated";
155
156 BitArray array(0);
160 }
161
162 // Frees the nodes of a tree that are not leaves: those that a generation
163 // made. Its leaves are of the nodes of the symbols
164 template <class Node>
165 static void free_inner_nodes(Node *p) noexcept
166 {
167 if (p == nullptr or (LLINK(p) == nullptr and RLINK(p) == nullptr))
168 return;
171 delete p;
172 }
173
174 // Leaves the encoder as it was before a generation of the tree that did
175 // not end: the heap with the nodes of the symbols alone, and nothing of
176 // what the generation made. It allocates nothing
177 void undo_generation() noexcept
178 {
179 while (not heap_.is_empty())
180 if (auto *node = static_cast<Huffman_Node *>(heap_.getMin_ne());
181 not is_leaf(node->bin_node))
182 {
183 free_inner_nodes(node->bin_node);
184 free_inner_nodes(node->freq_node);
185 delete node;
186 }
187 for (Huffman_Node *leaf = symbols_; leaf != nullptr; leaf = leaf->next_symbol)
188 {
189 delete leaf->freq_node;
190 leaf->freq_node = nullptr;
191 heap_.insert(leaf); // noexcept: the keys are size_t
192 }
193 }
194
195 // Gives a node its node of frequencies, if it has none: that of a
196 // symbol, with its frequency
197 static void make_freq_node(Huffman_Node *node)
198 {
199 if (node->freq_node != nullptr)
200 return;
201 std::unique_ptr<Freq_Node> freq_node(new Freq_Node);
202 freq_node->get_key().first = node->bin_node->get_key();
203 freq_node->get_key().second = node->get_key();
204 node->freq_node = freq_node.release();
205 }
206
207 // A step of the algorithm of Huffman: the two nodes of least frequency
208 // leave the heap, and a new one enters with their trees below it. Without
209 // memory for it, the heap has the nodes that it had
210 void merge_two_nodes(const bool with_freqs)
211 {
212 // The two nodes of the step are made before the heap changes
213 std::unique_ptr<BinNode<std::string>> bin_node(new BinNode<std::string>);
214 std::unique_ptr<Huffman_Node> huffman_node(new Huffman_Node(bin_node.get()));
215 auto *l_huffman_node = static_cast<Huffman_Node *>(heap_.getMin()); // left
216 auto *r_huffman_node = static_cast<Huffman_Node *>(heap_.getMin()); // right
217 const size_t new_freq = get_freq(l_huffman_node) + get_freq(r_huffman_node);
218 Aleph::set_freq(huffman_node.get(), new_freq);
219
220 if (with_freqs)
221 try
222 {
223 make_freq_node(l_huffman_node);
224 make_freq_node(r_huffman_node);
225 std::unique_ptr<Freq_Node> freq_node(new Freq_Node);
226 freq_node->get_key().first = std::to_string(new_freq);
227 freq_node->get_key().second = new_freq;
228 LLINK(freq_node.get()) = l_huffman_node->freq_node;
229 RLINK(freq_node.get()) = r_huffman_node->freq_node;
230 huffman_node->freq_node = freq_node.release();
231 }
232 catch (...)
233 {
234 heap_.insert(l_huffman_node); // noexcept: the keys are size_t
235 heap_.insert(r_huffman_node);
236 throw;
237 }
238
239 // Nothing throws from here on. The nodes of the symbols stay until the
240 // tree is whole: the encoder goes back to them if the generation fails
241 LLINK(bin_node.get()) = l_huffman_node->bin_node;
242 RLINK(bin_node.get()) = r_huffman_node->bin_node;
243 if (not is_leaf(l_huffman_node->bin_node))
244 delete l_huffman_node;
245 if (not is_leaf(r_huffman_node->bin_node))
246 delete r_huffman_node;
247 bin_node.release();
248 heap_.insert(huffman_node.release());
249 }
250
251 bool test_end(const std::string &str) const
252 {
253 if (end_symbol_ == "NO-END")
254 return false;
255 return end_symbol_ == str;
256 }
257
258 // Makes the leaf of str and its node, of frequency freq, and enters the
259 // node in the map of the symbols and then in the heap. Only the map can
260 // throw: then neither node is anywhere, and both are freed here
261 Huffman_Node * insert_symbol_node(const std::string &str, const size_t freq)
262 {
263 auto *bin_node = new BinNode<std::string>(str);
264 Huffman_Node *huffman_node = nullptr;
265 try
266 {
267 huffman_node = new Huffman_Node(bin_node);
268 symbol_map_.insert(str, huffman_node);
269 }
270 catch (...)
271 {
272 delete huffman_node;
273 delete bin_node;
274 throw;
275 }
276 Aleph::set_freq(huffman_node, freq);
277 heap_.insert(huffman_node); // noexcept: the keys are size_t
278 huffman_node->next_symbol = symbols_;
279 symbols_ = huffman_node;
280 return huffman_node;
281 }
282
283 void update_freq(const std::string &str)
284 {
285 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
286
287 ah_domain_error_if(test_end(str)) << "End symbol has already been inserted";
288
289 Huffman_Node *huffman_node = nullptr;
290 if (auto huffman_node_ptr = symbol_map_.search(str);
291 huffman_node_ptr == nullptr) // symbol defined previously?
292 huffman_node = insert_symbol_node(str, 0); // No ==> a new entry
293 else
294 huffman_node = huffman_node_ptr->second; // already defined, retrieve it
295
296 increase_freq(huffman_node);
297 heap_.update(huffman_node);
298 }
299
300 static void append_code(BitArray &bit_stream, const BitArray &symbol_code)
301 {
302 const size_t symbol_code_size = symbol_code.size();
303 for (size_t i = 0; i < symbol_code_size; ++i)
304 bit_stream.push(symbol_code[i]);
305 }
306
307public:
309 Huffman_Encoder_Engine() : root_(nullptr), freq_root_(nullptr), end_symbol_("NO-END"), text_len_(0)
310 {
311 // empty
312 }
313
319
320private:
321 struct Get_Key
322 {
323 std::string operator () (BinNode<std::string> *p) const noexcept
324 {
325 return is_leaf(p) ? p->get_key() : "";
326 }
327 };
328
329 struct Load_Key
330 {
331 void operator () (BinNode<std::string> *p, std::istream &input) const noexcept
332 {
333 if (is_leaf(p))
334 input >> p->get_key();
335 }
336 };
337
338public:
350 void save_tree(std::ostream &output) const
351 {
352 ah_domain_error_if(root_ == nullptr) << "Huffman tree has not been generated";
353
356 prefix.save(output);
357
359 output << '\n';
360
362 }
363
382 void save_tree_in_array_of_chars(const std::string &array_name, std::ostream &output) const
383 {
384 ah_domain_error_if(root_ == nullptr) << "Huffman tree has not been generated";
385
386 Aleph::save_tree_in_array_of_chars<BinNode<std::string>, Get_Key>(root_, array_name, output);
387 }
388
391 {
392 ah_domain_error_if(root_ == nullptr) << "Huffman tree has not been generated";
393
394 return root_;
395 }
396
414 BinNode<std::string> *generate_huffman_tree(const bool &with_freqs = false)
415 {
416 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
417 ah_domain_error_if(heap_.is_empty()) << "No symbols have been inserted";
418
419 freq_root_ = nullptr;
420
421 Code_Map codes;
422 Huffman_Node *huffman_root = nullptr;
423 try
424 {
425 while (heap_.size() > 1) // until only one node remains
426 merge_two_nodes(with_freqs);
427
428 // The remaining node has the prefix tree. It stays in the heap, which
429 // is who frees it, while the map of the codes is built
430 huffman_root = static_cast<Huffman_Node *>(heap_.top());
431 if (with_freqs)
432 make_freq_node(huffman_root); // it has none if it is a single symbol
433 BitArray array(0);
434 build_prefix_encoding(huffman_root->bin_node, array, codes);
435 }
436 catch (...)
437 {
439 throw;
440 }
441
442 // Nothing throws from here on
444 root_ = huffman_root->bin_node;
445 if (with_freqs)
446 freq_root_ = huffman_root->freq_node;
447 if (not is_leaf(root_))
448 delete huffman_root; // the node of a symbol goes with the others
449 while (symbols_ != nullptr)
450 delete std::exchange(symbols_, symbols_->next_symbol);
452 code_map_.swap(codes);
453
454 return root_;
455 }
456
467 void load_tree(std::istream &input)
468 {
470
473
475 prefix.load(input);
476 root_ = bits_to_tree<BinNode<std::string>>(prefix);
477
480
482 }
483
486 {
487 ah_domain_error_if(freq_root_ == nullptr) << "Huffman tree has not been generated";
488
489 return freq_root_;
490 }
491
501 void set_freq(const std::string &str, const size_t &freq)
502 {
503 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
504 ah_domain_error_if(test_end(str)) << "End symbol has already been inserted";
505
506 // Search symbol str
507 auto huffman_node_ptr = symbol_map_.search(str);
508 if (huffman_node_ptr != nullptr) // already defined?
509 {
510 std::string msg = "Frequency for symbol " + str + " has already set";
511 ah_domain_error() << msg; // Yes ==> this is an error!
512 }
513
514 insert_symbol_node(str, freq);
515 }
516
517private:
518 static const size_t Max_Token_Size = 256;
519
520 static void save_string_as_bytes(const std::string &str, std::ostream &output)
521 {
522 output << str.size() << " ";
523 for (unsigned char c : str)
524 output << static_cast<unsigned int>(c) << " ";
525 }
526
527 static std::string load_string_from_bytes(std::istream &input)
528 {
529 size_t len = 0;
530 input >> len;
531 ah_domain_error_if(not input) << "Malformed Huffman tree stream";
532 ah_domain_error_if(len > Max_Token_Size) << "Symbol too large in Huffman tree stream";
533
534 std::string str;
535 str.resize(len);
536 for (size_t i = 0; i < len; ++i)
537 {
538 unsigned int byte = 0;
539 input >> byte;
540 ah_domain_error_if(not input) << "Malformed Huffman tree stream";
541 ah_domain_error_if(byte > 255u) << "Invalid byte value in Huffman tree stream";
542 str[i] = static_cast<char>(static_cast<unsigned char>(byte));
543 }
544 return str;
545 }
546
548 {
550 return;
551
552 if (is_leaf(p))
553 {
555 output << '\n';
556 return;
557 }
558
561 }
562
563 static void load_leaf_keys_in_prefix(BinNode<std::string> *p, std::istream &input)
564 {
566 return;
567
568 if (is_leaf(p))
569 {
570 p->get_key() = load_string_from_bytes(input);
571 return;
572 }
573
576 }
577
578 void insert_end_symbol_node(const std::string &str)
579 {
580 insert_symbol_node(str, 0);
581 }
582
583 void clear_build_state() noexcept
584 {
585 while (not heap_.is_empty())
586 {
587 auto *node = static_cast<Huffman_Node *>(heap_.getMin_ne());
588 delete node->bin_node;
589 delete node;
590 }
591 symbols_ = nullptr;
594 text_len_ = 0;
595 end_symbol_ = "NO-END";
596 }
597
598public:
609 void read_input(char *input, const bool &with_freqs = false)
610 {
611 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
612
613 char *curr_stream = input;
614 char curr_token[Max_Token_Size];
615 curr_token[1] = '\0';
616 text_len_ = 0;
617
618 while (*curr_stream != '\0')
619 {
620 curr_token[0] = *curr_stream++;
621 update_freq(curr_token);
622 text_len_++;
623 }
624
625 if (end_symbol_ == "NO-END")
627 else if (symbol_map_.search(end_symbol_) == nullptr)
629 generate_huffman_tree(with_freqs);
630 }
631
642 void read_input(std::istream &input, const bool &with_freqs = false)
643 {
644 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
645
646 char curr_token[2] = {'\0', '\0'};
647 char ch = '\0';
648 text_len_ = 0;
649 while (input.get(ch))
650 {
651 curr_token[0] = ch;
652 update_freq(curr_token);
653 ++text_len_;
654 }
655
656 if (end_symbol_ == "NO-END")
658 else if (symbol_map_.search(end_symbol_) == nullptr)
660 generate_huffman_tree(with_freqs);
661 }
662
664 void set_end_of_stream(const std::string &str)
665 {
666 ah_domain_error_if(end_symbol_ != "NO-END") << "End symbol has already been inserted";
667
668 ah_domain_error_if(root_ != nullptr) << "Huffman encoding tree has already been generated";
669
670 ah_domain_error_if(symbol_map_.search(str) != nullptr)
671 << "End symbol is already present as a normal symbol";
672
673 std::string end = str; // the copy can throw, before the symbol enters
675 end_symbol_.swap(end);
676 }
677
679 const std::string &get_end_of_stream() const noexcept
680 {
681 return end_symbol_;
682 }
683
694 size_t encode(char *input, BitArray &bit_stream)
695 {
696 ah_domain_error_if(root_ == nullptr) << "Huffman tree has not been generated";
697
698 ah_domain_error_if(end_symbol_ == "NO-END") << "End symbol has not been configured";
699
700 char *curr_stream = input;
701 char curr_token[Max_Token_Size];
702 curr_token[1] = '\0';
703 while (*curr_stream != '\0')
704 {
705 curr_token[0] = *curr_stream++;
706 append_code(bit_stream, code_map_.find(curr_token));
707 }
709
710 return bit_stream.size();
711 }
712
723 size_t encode(std::istream &input, BitArray &bit_stream)
724 {
725 ah_domain_error_if(root_ == nullptr) << "Huffman tree has not been generated";
726
727 ah_domain_error_if(end_symbol_ == "NO-END") << "End symbol has not been configured";
728
729 char curr_token[2] = {'\0', '\0'};
730 char ch = '\0';
731 while (input.get(ch))
732 {
733 curr_token[0] = ch;
734 append_code(bit_stream, code_map_.find(curr_token));
735 }
736
738 return bit_stream.size();
739 }
740};
741
748{
750 std::string end_symbol;
751
752public:
760 Huffman_Decoder_Engine(BinNode<std::string> *p, const std::string &end) : root(p), end_symbol(end)
761 {
762 // empty
763 }
764
767 {
768 ah_domain_error_if(root == nullptr) << "Huffman tree has not been generated";
769
770 return root;
771 }
772
781 void decode(const BitArray &bit_stream, std::ostream &output) const
782 {
783 const size_t &bit_stream_len = bit_stream.size();
785 for (size_t i = 0; i < bit_stream_len; ++i)
786 {
787 if (bit_stream.read_bit(i) == 0)
788 p = LLINK(p);
789 else
790 p = RLINK(p);
791
792 ah_domain_error_if(p == nullptr) << "Invalid bits sequence";
793
794 if (is_leaf(p)) // leaf?
795 { // yes ==> write symbol and reset to root
796 const std::string &symbol = p->get_key();
797 if (symbol == end_symbol) // end reached?
798 break;
799
800 output << symbol;
801 p = root; // reset to root, a new code will be read
802 }
803 }
804 }
805};
806
807} // end namespace Aleph
808
809#endif // HUFFMAN_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
WeightedDigraph::Node Node
Space-efficient bit array implementation.
Key & get_key() noexcept
Node for binary search tree.
Key & get_key() noexcept
Contiguous array of bits.
Definition bitArray.H:201
int read_bit(const size_t i) const
Read bit i.
Definition bitArray.H:409
void pop()
Removes the last bit of the array.
Definition bitArray.H:512
void load(std::istream &input)
Loads an array of bits saved with save().
Definition bitArray.H:661
void push(const unsigned int value)
Inserts the value at the end of the array.
Definition bitArray.H:506
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Definition bitArray.H:350
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
void empty()
Remove all the elements of the set.
Node * insert(Node *p) noexcept(nothrow_cmp)
Inserts a node into a heap.
bool is_empty() const noexcept
void update(Node *p) noexcept(nothrow_cmp)
Updates the priority of a node contained in the heap.
Node * getMin_ne() noexcept(nothrow_cmp)
Removes the node with the lowest priority from a heap that is not empty.
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
Node * getMin()
Removes the node with the lowest priority from the heap.
const size_t & size() const noexcept
BinNode< std::string > * root
Definition Huffman.H:749
Huffman_Decoder_Engine(BinNode< std::string > *p, const std::string &end)
Decoder constructor.
Definition Huffman.H:760
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
Definition Huffman.H:766
void decode(const BitArray &bit_stream, std::ostream &output) const
Decode a bit stream.
Definition Huffman.H:781
void insert_end_symbol_node(const std::string &str)
Definition Huffman.H:578
Freq_Node *& get_freq_root()
Returns the root of the frequency tree.
Definition Huffman.H:485
static void save_leaf_keys_in_prefix(BinNode< std::string > *p, std::ostream &output)
Definition Huffman.H:547
void update_freq(const std::string &str)
Definition Huffman.H:283
void save_tree_in_array_of_chars(const std::string &array_name, std::ostream &output) const
Generate C/C++ array declarations for a Huffman tree.
Definition Huffman.H:382
void save_tree(std::ostream &output) const
Save a Huffman tree into a stream.
Definition Huffman.H:350
size_t encode(char *input, BitArray &bit_stream)
Encode the input text.
Definition Huffman.H:694
static std::string load_string_from_bytes(std::istream &input)
Definition Huffman.H:527
void load_tree(std::istream &input)
Load and build a binary tree from a stream.
Definition Huffman.H:467
static const size_t Max_Token_Size
Definition Huffman.H:518
static void make_freq_node(Huffman_Node *node)
Definition Huffman.H:197
void clear_build_state() noexcept
Definition Huffman.H:583
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
Definition Huffman.H:390
void set_freq(const std::string &str, const size_t &freq)
Define the frequency of a symbol.
Definition Huffman.H:501
void merge_two_nodes(const bool with_freqs)
Definition Huffman.H:210
BinNode< std::string > * root_
Definition Huffman.H:121
static void free_inner_nodes(Node *p) noexcept
Definition Huffman.H:165
static void save_string_as_bytes(const std::string &str, std::ostream &output)
Definition Huffman.H:520
void read_input(char *input, const bool &with_freqs=false)
Read a NUL-terminated character std::string, count frequencies and build the prefix tree.
Definition Huffman.H:609
static void load_leaf_keys_in_prefix(BinNode< std::string > *p, std::istream &input)
Definition Huffman.H:563
void read_input(std::istream &input, const bool &with_freqs=false)
Read a stream, count frequencies and build the prefix tree.
Definition Huffman.H:642
bool test_end(const std::string &str) const
Definition Huffman.H:251
void set_end_of_stream(const std::string &str)
Defines the end-of-stream symbol.
Definition Huffman.H:664
~Huffman_Encoder_Engine()
Destructor - frees all allocated memory.
Definition Huffman.H:315
void undo_generation() noexcept
Definition Huffman.H:177
Huffman_Encoder_Engine()
Encoder constructor.
Definition Huffman.H:309
static void append_code(BitArray &bit_stream, const BitArray &symbol_code)
Definition Huffman.H:300
static void build_prefix_encoding(BinNode< std::string > *p, BitArray &array, Code_Map &codes)
Definition Huffman.H:136
Huffman_Node * insert_symbol_node(const std::string &str, const size_t freq)
Definition Huffman.H:261
const std::string & get_end_of_stream() const noexcept
Return the configured end-of-stream symbol.
Definition Huffman.H:679
size_t encode(std::istream &input, BitArray &bit_stream)
Encode the input text from a stream.
Definition Huffman.H:723
BinNode< std::string > * generate_huffman_tree(const bool &with_freqs=false)
Generate the Huffman prefix tree.
Definition Huffman.H:414
void tree_to_bits(Node *root, BitArray &array)
Compute a bit code for the binary tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
BinHeap< size_t > Huffman_Heap
Definition Huffman.H:91
DynMapTree< std::string, BitArray, Treap_Vtl > Code_Map
Definition Huffman.H:108
static void increase_freq(Huffman_Node *huffman_node) noexcept
Definition Huffman.H:98
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
static const size_t & get_freq(Huffman_Node *huffman_node) noexcept
Definition Huffman.H:93
static bool is_leaf(BinNode< std::string > *p) noexcept
Definition Huffman.H:109
static void set_freq(Huffman_Node *huffman_node, const size_t &freq) noexcept
Definition Huffman.H:103
Node heap without virtual destructor.
std::string operator()(BinNode< std::string > *p) const noexcept
Definition Huffman.H:323
void operator()(BinNode< std::string > *p, std::istream &input) const noexcept
Definition Huffman.H:331
Huffman_Node(BinNode< std::string > *node)
Definition Huffman.H:80
BinNode< std::string > * bin_node
Definition Huffman.H:66
Huffman_Node * next_symbol
The node of the symbol that entered the encoder before this one, if this is the node of a symbol: the...
Definition Huffman.H:72
Freq_Node * freq_node
Definition Huffman.H:68
#define RLINK(i, n)
#define LLINK(i, n)
Binary heap implementation using tree structure.
Utility functions for binary tree operations.
Dynamic key-value map based on balanced binary search trees.
Treap: randomized BST combining tree and heap properties.
ofstream output
Definition writeHeap.C:215