-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
68 lines (55 loc) · 1.15 KB
/
encode.cpp
File metadata and controls
68 lines (55 loc) · 1.15 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
#include "encode.hpp"
Node::Node(int c, char ch) : count(c), character(ch), valid(true) {
left = nullptr;
right = nullptr;
}
Node::Node(int c) : count(c), character('\0'), valid(false) {
left = nullptr;
right = nullptr;
}
Node::~Node() {
delete left;
left = nullptr;
delete right;
right = nullptr;
}
void Node::setLeft(const Node* l) {
left = l;
}
void Node::setRight(const Node* r) {
right = r;
}
bool Node::isValid() const {
return valid;
}
int Node::getCount() const {
return count;
}
char Node::getCharacter() const {
return character;
}
bool Node::operator<(const Node& node) const {
return (count < node.getCount());
}
void Node::printTree() const {
printTree("");
}
void Node::printTree(std::string s) const {
if (valid) {
std::cout << character << " : " << s << '\n';
return;
}
left->printTree(s + "1");
right->printTree(s + "0");
}
void Node::fillCode(std::string codes[255]) const {
fillCode(codes, "");
}
void Node::fillCode(std::string codes[255], std::string s) const {
if (valid) {
codes[(int)character] = s;
return;
}
left->fillCode(codes, s + "1");
right->fillCode(codes, s + "0");
}