-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.cpp
270 lines (221 loc) · 8.26 KB
/
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Autor: Jordan Jarolim (xjarol03)
* Datum: 16.2.2017
* Soubor: tree.cpp
* Komentar: Trida zajistujici veskere operace se stromem a to jak pri kodovani tak i pri dekodovani. Predevsim vsak vkladani do stromu, rekonfiguraci stromu, update vah uzlu a hledani prvku ve strome. Dale take vypis stromu pro pomocne ucely.
*/
#include "tree.hpp"
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
std::vector<int64_t> Tree::_listOfUsedChars;
Tree* Tree::nyt = NULL;
Tree* Tree::root = NULL;
/* Terminal symbol of 16 1's */
unsigned short Tree::peof = 65535;
/* Constructors */
Tree::Tree(int64_t value, int64_t wei, int64_t num, Tree* l, Tree* r, Tree* p)
:val(value), weight(wei), order(num), left(l), right(r), parent(p)
{}
Tree::Tree()
:val(0),weight(0),order(0), left(0), right(0), parent(0)
{}
/* Methods */
/**
* Check if incoming symbol was already seen
* @param symbol to check
* @return bool
*/
bool Tree::alreadySeen(int64_t symbol){
if(std::find(Tree::_listOfUsedChars.begin(), Tree::_listOfUsedChars.end(), symbol) != Tree::_listOfUsedChars.end())
return true;
else
return false;
}
/**
* Create new NYT node and new symbol node
* @param symbol to be added
*/
void Tree::newSymbol(int64_t symbol){
Tree* originalNyt = Tree::nyt;
Tree::nyt = new Tree(NYT, INIT_WEIGHT, originalNyt->order - 2, NULL, NULL, originalNyt);
originalNyt->val = CROSS;
originalNyt->left = Tree::nyt;
originalNyt->right = new Tree(symbol, INIT_WEIGHT, originalNyt->order -1, NULL, NULL, originalNyt);
originalNyt->right->updateTree();
}
/**
* Update symbol weight
* @param symbol to be updated
*/
void Tree::updateSymbol(int64_t symbol){
Tree* found = NULL;
if ((found = this->findSymbol(symbol, this)) != NULL){
found->updateTree();
}
else{
cerr<<"Internal error\n";
exit(-1);
}
}
/**
* Find symbol to update its weight
* @param symbol to be updated
* @param parent recursive parent
* @return Tree* or NULL
*/
Tree* Tree::findSymbol(int64_t symbol, Tree* parent){
Tree* found = NULL;
if (parent != NULL){
if (parent->val != symbol){
if ((found = this->findSymbol(symbol, parent->left)) != NULL)
return found;
else if((found = this->findSymbol(symbol, parent->right)) != NULL)
return found;
else return NULL;
}
else return parent;
}
else return NULL;
}
/**
* Find nex sibling to swap
* @param order to be found
* @param parent recursive parent
* @return Tree* or NULL
*/
Tree* Tree::findFarSibling(int64_t order, Tree* parent){
Tree* found = NULL;
if (parent != NULL){
if (parent->order != order){
if ((found = this->findFarSibling(order, parent->left)) != NULL)
return found;
else if((found = this->findFarSibling(order, parent->right)) != NULL)
return found;
else return NULL;
}
else return parent;
}
else return NULL;
}
/**
* Reorder tree nodes to meet requirements for Huffman tree
*/
void Tree::updateTree(){
Tree* current = this;
Tree* toSwap = NULL;
Tree* currentParent = NULL;
Tree* swapParent = NULL;
Tree* tempNode = NULL;
int64_t tempOrder = 0;
//current->weight = current->left->weight + current->right->weight;
/* while not the root */
while (current->parent != NULL){
bool repeat = true;
while (repeat){
repeat = false;
/*cout<<"find node to swap:"<<current->order<<" : "<<current->order+1<<"\n\n";
Tree::printTree(Tree::root);
cout<<"\n\n----------------------------------\n";*/
toSwap = findFarSibling(current->order+1, Tree::root);
if (toSwap == current->parent){
toSwap = findFarSibling(current->order+2, Tree::root);
}
/* Swap nodes */
if (toSwap != NULL && current->weight == toSwap->weight){
repeat = true;
/*
cout<<"should swap:"<<current->order<<" : "<<toSwap->order<<" and parent is:"<<current->parent->order<<"\n\n";
Tree::printTree(Tree::root);
cout<<"\n\n----------------------------------\n";*/
/* Swap in one subtree */
if (toSwap == current->parent->right || toSwap == current->parent->left){
//cout<<"Swapping\n";
tempNode = current;
current->parent->left = current->parent->right;
current->parent->right = tempNode;
tempOrder = current->order;
current->order = current->parent->left->order;
current->parent->left->order = tempOrder;
/*cout<<"swapped:"<<current->order<<" : "<<toSwap->order<<" and parent is:"<<current->parent->order<<"\n\n";
Tree::printTree(Tree::root);
cout<<"\n\n----------------------------------\n";*/
}
/* Swap with element from other subtree */
else{
if (toSwap->parent != NULL){
swapParent = toSwap->parent;
currentParent = current->parent;
if (current == currentParent->left){
currentParent->left = toSwap;
}
else{
currentParent->right = toSwap;
}
if (toSwap == swapParent->left){
swapParent->left = current;
}
else{
swapParent->right = current;
}
toSwap->parent = currentParent;
current->parent = swapParent;
tempOrder = current->order;
current->order = toSwap->order;
toSwap->order = tempOrder;
/*cout<<"swapped:"<<current->order<<" : "<<toSwap->order<<" and parent is:"<<current->parent->order<<"\n\n";
Tree::printTree(Tree::root);
cout<<"\n\n----------------------------------\n";*/
}
}
}else{
current->weight++;
//cout << "raised weight of "<<current->order<<"\n";
current=current->parent;
}
}
}
/* update root */
current->weight = current->left->weight + current->right->weight;
/*cout<<"Weight of root updated\n";
Tree::printTree(Tree::root);
cout<<"\n\n------------------------------------------------------------\n\n\n";*/
}
void Tree::getWeightClass(std::vector<Tree*> *weightClass, Tree* parent){
if (parent != NULL){
if ((parent->weight == this->weight) && (parent->order != this->order) && (parent != Tree::root) && (parent != this->parent)){
weightClass->push_back(parent);
}
this->getWeightClass(weightClass, parent->left);
this->getWeightClass(weightClass, parent->right);
}
}
/**
* Print Tree recursively
* @param p - tree pointer
* @param indent - indentation
*/
void Tree::printTree(Tree* p, int indent)
{
int64_t parent;
if(p != NULL) {
if(p->right != NULL) {
printTree(p->right, indent+4);
}
if (indent) {
std::cout << std::setw(indent) << ' ';
}
if (p->right != NULL) std::cout<<" /\n" << std::setw(indent) << ' ';{
if (p->parent != NULL)
parent = p->parent->order;
else
parent = 0;
cout<< "order: " << p->order << " | parent: "<<parent<< " | weight: "<<p->weight<<" symbol: "<<(char)p->val<<"\n";
}
if(p->left != NULL) {
std::cout << std::setw(indent) << ' ' <<" \\\n";
printTree(p->left, indent+4);
}
}
}