Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_huffman.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <iostream>
29# include <fstream>
30# include <tpl_binNodeUtils.H>
31# include <Huffman.H>
32# include <huffman_btreepic.H>
33
34using namespace std;
35
36char poema1 [] =
37" Las cosas\n"
38"\n"
39"El bastón, las monedas, el llavero,\n"
40"la dócil cerradura, las tardías\n"
41"notas que no leerán los pocos días\n"
42"que me quedan, los naipes y el tablero,\n"
43"\n"
44"un libro y en sus páginas la ajada\n"
45"violeta, monumento de una tarde\n"
46"sin duda inolvidable y ya olvidada,\n"
47"el rojo espejo occidental en que arde\n"
48"\n"
49"una ilusoria aurora. ¡Cuántas cosas,\n"
50"láminas, umbrales, atlas, copas, clavos,\n"
51"nos sirven como tácitos esclavos,\n"
52"\n"
53"ciegas y extrañamente sigilosas!\n"
54"Durarán más allá de nuestro olvido;\n"
55"no sabrán nunca que nos hemos ido.\n"
56"\n"
57" Jorge Luis Borges\n";
58
59
60char poema [] =
61"El enamorado\n"
62"\n"
63"Lunas, marfiles, instrumentos, rosas,\n"
64"lámparas y la línea de Durero,\n"
65"las nueve cifras y el cambiante cero,\n"
66"debo fingir que existen esas cosas.\n"
67"\n"
68"Debo fingir que en el pasado fueron\n"
69"Persépolis y Roma y que una arena\n"
70"sutil midió la suerte de la almena\n"
71"que los siglos de hierro deshicieron.\n"
72"\n"
73"Debo fingir las armas y la pira\n"
74"de la epopeya y los pesados mares\n"
75"que roen de la tierra los pilares.\n"
76"\n"
77"Debo fingir que hay otros. Es mentira.\n"
78"Sólo tú eres. Tú, mi desventura\n"
79"y mi ventura, inagotable y pura.\n"
80"\n"
81" Jorge Luis Borges\n";
82
83
84const size_t read_and_encode(char * str,
87{
88 huffman_engine.read_input(str, true);
89
90 const size_t bit_stream_len = huffman_engine.encode(str, bit_stream);
91
92 return bit_stream_len;
93}
94
95
96void print_node(BinNode<string> * p, int, int)
97{
98 cout << p->get_key() << " ";
99}
100
101void print_code(BitArray & cod, const size_t & len)
102{
103 for (int i = 0; i < len; ++i)
104 cout << cod[i] << " ";
105 cout << endl << endl;
106}
107
108int main()
109{
110 ofstream output("borges.Tree", ios::out);
112
114
115 size_t code_len = 2048;
116
118
120
122
123 Huffman_Decoder_Engine decoder(encoder.get_root(), "");
124
125 huffman_to_btreepic(encoder.get_freq_root(), true);
126
127 decoder.decode(code, std::cout);
128
129 std::cout << std::endl;
130
131 destroyRec(encoder.get_root());
132}
Huffman coding for data compression.
Node for binary search tree.
Key & get_key() noexcept
Contiguous array of bits.
Definition bitArray.H:189
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Huffman tree visualization for btreepic LaTeX package.
void huffman_to_btreepic(Freq_Node *p, const bool with_level_adjust=false)
Generate btreepic specification for a Huffman tree.
std::ostream * output_ptr
Output stream pointer for btreepic commands.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
STL namespace.
char poema1[]
char poema[]
void print_node(BinNode< string > *p, int, int)
void print_code(BitArray &cod, const size_t &len)
int main()
const size_t read_and_encode(char *str, Huffman_Encoder_Engine &huffman_engine, BitArray &bit_stream)
Utility functions for binary tree operations.
ofstream output
Definition writeHeap.C:215