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 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <iostream>
34# include <fstream>
35# include <tpl_binNodeUtils.H>
36# include <Huffman.H>
37# include <huffman_btreepic.H>
38
39using namespace std;
40
41char poema1 [] =
42" Las cosas\n"
43"\n"
44"El bastón, las monedas, el llavero,\n"
45"la dócil cerradura, las tardías\n"
46"notas que no leerán los pocos días\n"
47"que me quedan, los naipes y el tablero,\n"
48"\n"
49"un libro y en sus páginas la ajada\n"
50"violeta, monumento de una tarde\n"
51"sin duda inolvidable y ya olvidada,\n"
52"el rojo espejo occidental en que arde\n"
53"\n"
54"una ilusoria aurora. ¡Cuántas cosas,\n"
55"láminas, umbrales, atlas, copas, clavos,\n"
56"nos sirven como tácitos esclavos,\n"
57"\n"
58"ciegas y extrañamente sigilosas!\n"
59"Durarán más allá de nuestro olvido;\n"
60"no sabrán nunca que nos hemos ido.\n"
61"\n"
62" Jorge Luis Borges\n";
63
64
65char poema [] =
66"El enamorado\n"
67"\n"
68"Lunas, marfiles, instrumentos, rosas,\n"
69"lámparas y la línea de Durero,\n"
70"las nueve cifras y el cambiante cero,\n"
71"debo fingir que existen esas cosas.\n"
72"\n"
73"Debo fingir que en el pasado fueron\n"
74"Persépolis y Roma y que una arena\n"
75"sutil midió la suerte de la almena\n"
76"que los siglos de hierro deshicieron.\n"
77"\n"
78"Debo fingir las armas y la pira\n"
79"de la epopeya y los pesados mares\n"
80"que roen de la tierra los pilares.\n"
81"\n"
82"Debo fingir que hay otros. Es mentira.\n"
83"Sólo tú eres. Tú, mi desventura\n"
84"y mi ventura, inagotable y pura.\n"
85"\n"
86" Jorge Luis Borges\n";
87
88
89const size_t read_and_encode(char * str,
92{
93 huffman_engine.read_input(str, true);
94
95 const size_t bit_stream_len = huffman_engine.encode(str, bit_stream);
96
97 return bit_stream_len;
98}
99
100
101void print_node(BinNode<string> * p, int, int)
102{
103 cout << p->get_key() << " ";
104}
105
106void print_code(BitArray & cod, const size_t & len)
107{
108 for (int i = 0; i < len; ++i)
109 cout << cod[i] << " ";
110 cout << endl << endl;
111}
112
113int main()
114{
115 ofstream output("borges.Tree", ios::out);
117
119
120 size_t code_len = 2048;
121
123
125
127
128 Huffman_Decoder_Engine decoder(encoder.get_root(), "");
129
130 huffman_to_btreepic(encoder.get_freq_root(), true);
131
132 decoder.decode(code, std::cout);
133
134 std::cout << std::endl;
135
136 destroyRec(encoder.get_root());
137}
Huffman coding for data compression.
Node for binary search tree.
Key & get_key() noexcept
Contiguous array of bits.
Definition bitArray.H:201
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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.
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