-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cpp
47 lines (35 loc) · 885 Bytes
/
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
47
//
// Created by gabriel on 21/03/18.
//
#include "Node.h"
Node::Node(Node* leftChild, Node* rightChild) :
parent(nullptr),
leftChild(leftChild),
rightChild(rightChild),
character(0) {
leftChild->parent = this;
rightChild->parent = this;
}
Node::Node(unsigned char character) :
parent(nullptr),
leftChild(nullptr),
rightChild(nullptr),
character(character) {}
Node::~Node() {
delete leftChild;
delete rightChild;
}
bool Node::isLeaf() {
return leftChild == nullptr;
}
bool Node::isRoot() {
return parent == nullptr;
}
void Node::writeCodeIn(HuffmanWriter &writer) {
if (this->isRoot()) return;
this->parent->writeCodeIn(writer);
writer.writeBit(this->parent->leftChild == this);
}
Node *Node::getChild(bool witch) {
return witch ? this->leftChild : this->rightChild;
}