-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanTree.cpp
159 lines (137 loc) · 3.2 KB
/
HuffmanTree.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
#include "HuffmanTree.h"
HuffmanTree::HuffmanTree(): root(NULL), size(0) {};
HuffmanTree::HuffmanTree(Heap& heap): root(NULL), size(0)
{
Node* tmpNode1;
Node* tmpNode2;
Node* newSubTree;
Node* newNode;
int sum;
while (heap.getSize()>0)
{
tmpNode1 = heap.top();
tmpNode1->makeVacant();
tmpNode2 = heap.top();
tmpNode2->makeVacant();
sum = tmpNode1->getCharObj().getFreq() + tmpNode2->getCharObj().getFreq();
if (tmpNode1->getSubTree())
tmpNode1 = tmpNode1->getSubTree();
if (tmpNode2->getSubTree())
tmpNode2 = tmpNode2->getSubTree();
newSubTree = new Node(tmpNode1, tmpNode2, sum);
if (heap.isEmpty())
root = newSubTree->getSubTree();
else
heap.insert(newSubTree);
}
}
HuffmanTree::~HuffmanTree()
{
freeTree(root);
}
void HuffmanTree::freeTree(Node* node)
{
if (node)
{
freeTree(node->getLeft());
freeTree(node->getRight());
delete node;
}
}
void HuffmanTree::setDictionary(Node* node, const string& code, Hash& hash)
{
if (node)
{
if (node->getRight() == NULL && node->getLeft() == NULL)
hash.add(new Character(node->getCharObj().getChar(), code));
setDictionary(node->getLeft(), code + "0", hash);
setDictionary(node->getRight(), code + "1", hash);
}
}
Hash& HuffmanTree::getDictionary()
{
Hash* hash = new Hash(1024);
setDictionary(root, "", *hash);
return *hash;
}
int HuffmanTree::height()
{
return root ? root->height() : 0;
}
void HuffmanTree::printPerLevel(Node* node, int level, int requieredLevel)
{
if (!node)
return;
if (level == requieredLevel)
{
if (node->vacant() == 0)
cout << "* ";
else
cout << node->getCharObj().getChar() << " ";
}
else if (level < requieredLevel)
{
printPerLevel(node->getLeft(), level + 1, requieredLevel);
printPerLevel(node->getRight(), level + 1, requieredLevel);
}
}
void HuffmanTree::code(Node* node, BitStreamer& stream, string& chars)
{
if (node == NULL)
return;
if (node->vacant())
{
stream << "0";
chars += node->getCharObj().getChar();
}
else
stream << "1";
code(node->getLeft(), stream, chars);
code(node->getRight(), stream, chars);
}
string HuffmanTree::buildFullHeader()
{
BitStreamer streamer;
string chars;
code(root, streamer, chars);
int len = chars.length();
if (len > 255)
len = 255;
// final code, includes huffman code and the encoded chars
return (char)len + (streamer.finish(true, false)) + chars;
}
string HuffmanTree::encode(const string& data)
{
Hash* dictionary = &getDictionary();
BitStreamer streamer;
string encoded;
for (int i = 0; i < data.length(); i++)
streamer << dictionary->getCharObj(data[i])->getBits();
delete dictionary;
return streamer.finish(true, true);
}
/* print metods */
void HuffmanTree::print(PRINT_TYPE type)
{
if (!root)
cout << "Empty\n";
else if (type == PREORDER)
print(root);
else if (type == LEVEL)
{
for (int i = 0; i < height() + 1; i++)
{
printPerLevel(root, 0, i);
cout << endl;
}
}
}
void HuffmanTree::print(Node* node)
{
if (node)
{
std::cout << node->getCharObj().getChar() << std::endl;
print(node->getLeft());
print(node->getRight());
}
}