-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman_tree.cpp
190 lines (160 loc) · 4.91 KB
/
huffman_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <map>
#include <vector>
#include <ostream>
#include <istream>
#include <memory>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cmath>
#include "huffman_tree.hpp"
#include "huffman_tree_item.hpp"
using std::map;
using std::vector;
using std::istream;
using std::ifstream;
using std::ofstream;
using std::char_traits;
using std::pair;
using std::shared_ptr;
using std::unique_ptr;
using std::for_each;
using std::find_if;
using std::make_shared;
using std::make_pair;
HuffmanTree* HuffmanTree::Parse(istream& input)
{
input.seekg(0,input.end);
long long process = input.tellg();
input.seekg(0,input.beg);
//Create a map to store the counter for each character
auto byteMap = make_shared<map<char,long>>();
//verify the input stream
auto c = input.get();
if (c == char_traits<char>::eof()) return nullptr;
//The global character counter
double length = 0;
//Read and count not until the input stream ends
while (!input.eof())
{
auto emplace_result = byteMap->emplace(c,0L);
if ( emplace_result.second == false) (*emplace_result.first).second++;
length++;
c = input.get();
}
//Initialize the return tree
auto tree = new HuffmanTree();
//Traversing the initial map to store the tree's terminal node
for (auto it = byteMap->begin();it!=byteMap->end();it++)
{
HuffmanTreeItem::NodeContent content;
content.c = it->first;
content.percent = it->second / length;
content.type = HuffmanTreeItem::NodeType::LEAF;
HuffmanTreeItem* item = new HuffmanTreeItem(content);
//Store terminal tree node for the return tree
tree->_terminalNodes.push_back(item);
}
tree->MakeChildItem();
return tree;
}
HuffmanTreeItem* HuffmanTree::GetRootItem() const
{
return _rootNode;
}
HuffmanTreeItem* HuffmanTree::GetTerminalItem( char c ) const
{
auto it = find_if(_terminalNodes.begin(),_terminalNodes.end(),[=](HuffmanTreeItem* item) -> bool
{
return item->GetContent().c == c;
});
return it == _terminalNodes.end() ? nullptr : *it;
}
HuffmanTree::HuffmanTree()
{
}
void HuffmanTree::MakeChildItem()
{
//the temporary collection for Huffman binary tree item generation
auto collection = make_shared<vector<HuffmanTreeItem*>>();
for_each(begin(_terminalNodes),end(_terminalNodes),[=](HuffmanTreeItem* item) -> void
{
collection->push_back(item);
});
while (collection->size() != 1)
{
//sort by the ascending order
sort(collection->begin(),collection->end(),[](HuffmanTreeItem* left,HuffmanTreeItem* right) -> bool
{
return left->GetContent().percent < right->GetContent().percent;
});
auto first = collection->at(0);
auto second = collection->at(1);
HuffmanTreeItem::NodeContent content;
content.type = HuffmanTreeItem::NodeType::MIDDLE;
content.percent = first->GetContent().percent + second->GetContent().percent;
//char value -1 stands for no character
content.c = -1;
HuffmanTreeItem* item = new HuffmanTreeItem(content);
//Set the parent-child relations
first->SetParent(item);
second->SetParent(item);
item->SetLeft(first);
item->SetRight(second);
//Remove these two nodes having been handled
collection->erase(collection->begin());
collection->erase(collection->begin());
//Add the middle tree node which has just been created
collection->push_back(item);
//set the return tree
if (collection->size() != 1)
_innerNodes.push_back(item);
else
_rootNode = item;
}
}
HuffmanTree::~HuffmanTree()
{
delete _rootNode;
for_each(_terminalNodes.begin(),_terminalNodes.end(),[] (HuffmanTreeItem* item) -> void
{
delete item;
});
for_each(_innerNodes.begin(),_innerNodes.end(),[] (HuffmanTreeItem* item) -> void
{
delete item;
});
}
ostream& operator << (ostream &os,HuffmanTree* const tree)
{
//Store the size of the terminal node
size_t size = tree->_terminalNodes.size();
char bytes[sizeof(size)];
for (auto i = 0;i<sizeof(size);i++)
{
bytes[i] = size & 0xFF;
size >>= 8;
}
os.write(bytes,1);
for_each(begin(tree->_terminalNodes),end(tree->_terminalNodes),[&](HuffmanTreeItem* item) -> void
{
os << item;
});
return os;
}
istream& operator >> (istream& is,HuffmanTree* const tree)
{
//Read the quantity of terminal nodes
char size_c = 0;
is.get(size_c);
unsigned char size = *reinterpret_cast<unsigned char*>(&size_c);
//De serialize each terminal node
for (auto i = 0; i < size; ++i)
{
HuffmanTreeItem* item = new HuffmanTreeItem();
is >> item;
tree->_terminalNodes.push_back(item);
}
tree->MakeChildItem();
return is;
}