forked from maxtors/Huffman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
46 lines (41 loc) · 1.21 KB
/
Node.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
#include <iostream>
#include "Node.h"
// ---------- NODE (LEAF) CONSTRUCTOR ---------------------------------------------------
Node::Node(char d, int s, NODE_TYPE t) {
type = t;
data = d;
frequency = s;
left = right = NULL;
}
// ---------- NODE (BIND) CONSTRUCTOR ---------------------------------------------------
Node::Node(Node* l, Node* r, NODE_TYPE t) {
type = t;
data = 0;
frequency = l->getFrequency() + r->getFrequency();
left = l;
right = r;
}
// ---------- NODE DECONSTRUCTOR --------------------------------------------------------
Node::~Node() {
delete left;
delete right;
}
// ---------- GET NODE FREQUENCY --------------------------------------------------------
int Node::getFrequency() {
return frequency;
}
// ---------- FILL NODE WITH DATA -------------------------------------------------------
void Node::fill(std::map<char, std::pair<int, int>>& enc, int bits, int nbits) {
if (type == LEAF) {
enc[data] = std::pair<int, int>(bits, nbits);
}
else if (type == BIND) {
nbits += 1;
bits <<= 1;
left->fill(enc, bits, nbits);
bits += 1;
right->fill(enc, bits, nbits);
bits >>= 1;
nbits--;
}
}