-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
171 lines (140 loc) · 4.23 KB
/
BinaryTree.cpp
File metadata and controls
171 lines (140 loc) · 4.23 KB
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
#include "BinaryTree.h"
void BinaryTree::writeToFile(Node* root)
{
if (root == nullptr)
return;
bool isLeaf = (root->data != '\0');
if (isLeaf)
std::cout << root->data << ":" << root->frequency << ":" << root->huffmanCode << std::endl;
writeToFile(root->left);
writeToFile(root->right);
}
void BinaryTree::createHufmannCodes(Node* root, std::string code)
{
if (root == nullptr)
return;
root->huffmanCode = code;
createHufmannCodes(root->left, code+"0"); // goes left, add 0 to code
createHufmannCodes(root->right, code+"1"); // goes right, add 1 to code
}
void BinaryTree::printTree(Node* node, int cell)
{
if (node == NULL)
return;
printTree(node->right, cell+6);
for (int i = 0; i < cell; i++)
std::cout << " ";
if (node->data != '\0') // if current node is leaf
std::cout << node->frequency << std::endl;
else { // if current node is not leaf
std::cout << node->frequency;
std::cout << "----------";
std::cout << std::endl;
}
std::cout << "\n";
printTree(node->left, cell+6);
}
void BinaryTree::createTree(std::vector<Node> nodes)
{
Node* nodeNotLeaf1 = new Node('\0', -2);
Node* nodeNotLeaf2 = new Node('\0', -2);
// if only one or two unique char
if (nodes.size() < 3) {
for (int i = 0; i < nodes.size(); i++) {
char data = nodes.at(i).data;
int freq = nodes.at(i).frequency;
Node* node = new Node(data, freq);
if (i == 0) {
root->left = node;
root->frequency += root->left->frequency;
}
else if (i == 1) {
root->right = node;
root->frequency += root->right->frequency;
}
}
}
// if only 3 unique char
else if (nodes.size() == 3) {
for (int i = 0; i < nodes.size(); i++) {
char data = nodes.at(i).data;
int freq = nodes.at(i).frequency;
Node* node = new Node(data, freq);
if (i == 0)
nodeNotLeaf1->left = node;
if (i == 1)
nodeNotLeaf1->right = node;
if (i == 2) {
nodeNotLeaf1->frequency = nodeNotLeaf1->right->frequency + nodeNotLeaf1->left->frequency;
root->left = nodeNotLeaf1;
root->right = node;
root->frequency = root->left->frequency + root->right->frequency;
}
}
}
// if there is more than 3 unique char
else {
for (int i = 0; i < nodes.size(); i++) {
char data = nodes.at(i).data;
int freq = nodes.at(i).frequency;
Node* node = new Node(data, freq);
if (i == 0)
nodeNotLeaf1->left = node;
else if (i == 1)
nodeNotLeaf1->right = node;
else if (i == 2)
nodeNotLeaf2->left = node;
else if (i == 3) {
nodeNotLeaf2->right = node;
nodeNotLeaf1->frequency = nodeNotLeaf1->right->frequency + nodeNotLeaf1->left->frequency;
nodeNotLeaf2->frequency = nodeNotLeaf2->right->frequency + nodeNotLeaf2->left->frequency;
}
else if (i % 2 == 0) {
Node* tempSibling = new Node('\0', nodeNotLeaf1->frequency);
tempSibling->right = nodeNotLeaf1->right;
tempSibling->left = nodeNotLeaf1->left;
nodeNotLeaf1->left = tempSibling;
nodeNotLeaf1->right = node;
nodeNotLeaf1->frequency = nodeNotLeaf1->right->frequency + nodeNotLeaf1->left->frequency;
}
else if (i % 2 == 1) {
Node* tempSibling = new Node('\0', nodeNotLeaf2->frequency);
tempSibling->right = nodeNotLeaf2->right;
tempSibling->left = nodeNotLeaf2->left;
nodeNotLeaf2->right = tempSibling;
nodeNotLeaf2->left = node;
nodeNotLeaf2->frequency = nodeNotLeaf2->right->frequency + nodeNotLeaf2->left->frequency;
}
}
root->left = nodeNotLeaf1;
root->right = nodeNotLeaf2;
root->frequency = root->left->frequency + root->right->frequency;
}
createHufmannCodes(root, "");
}
void BinaryTree::encode(Node* node, char c, std::string& output)
{
if (node == nullptr)
return ;
if (c == node->data)
output+= node->huffmanCode;
encode(node->left,c,output);
encode(node->right,c,output);
}
void BinaryTree::decode(Node* node, std::string code, std::string& output)
{
if (node == nullptr)
return;
for (char c : code) {
if (node) {
if (c == '0')
node = node->left;
else if (c == '1')
node = node->right;
if (node->data != '\0') {
output += node->data;
node = this->root;
}
}
}
}