-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.cpp
99 lines (80 loc) · 2.12 KB
/
TreeNode.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
#include "TreeNode.h"
namespace Huffman {
TreeNode::TreeNode() : _data(), _left(nullptr), _right(nullptr)
{
}
TreeNode::TreeNode(const char ch) : _data(ch), _left(nullptr), _right(nullptr)
{
}
TreeNode::TreeNode(int count, TreeNode* left, TreeNode* right)
: _data(count), _left(nullptr), _right(nullptr)
{
}
TreeNode::TreeNode(int count, const char type, TreeNode* left, TreeNode* right)
: _data(count, type), _left(left), _right(right)
{
}
TreeNode::TreeNode(Item& data) : TreeNode(data.getCount(), data.getChar(), nullptr, nullptr)
{
}
TreeNode::TreeNode(const TreeNode& other)
: TreeNode(other.getNodeCount(), other.getNodeChar(), other.leftNode(), other.rightNode())
{
}
void TreeNode::setNodePtrs(TreeNode* left, TreeNode* right)
{
_left = left;
_right = right;
}
int TreeNode::getNumOfChildren() const
{
int count = 0;
if (_left != nullptr)
count++;
if (_right != nullptr)
count++;
return count;
}
void TreeNode::InOrder()
{
if (_left != nullptr)
_left->InOrder();
cout << _data.getChar() << endl;
if (_right != nullptr)
_right->InOrder();
}
void TreeNode::PrintHuffmanNode(string s, TreeNode* arr, int size)
{
if (_left != nullptr) {
_left->PrintHuffmanNode(s.append("0"), arr, size);
if(!s.empty())
s.pop_back();
}
if (isHuffmanNode()) {
cout << getNodeChar() << " means : " << s << endl;
Clac(arr, getNodeChar(), size, s.length());
}
if (_right != nullptr) {
_right->PrintHuffmanNode(s.append("1"), arr, size);
if (!s.empty())
s.pop_back();
}
}
void TreeNode::Clac(TreeNode* arr, const char key, int size, int bits) {
for (int i = 0; i < size; ++i) {
if (arr[i].getNodeChar() == key)
arr[i].setCount(arr[i].getNodeCount() * bits);
}
}
void TreeNode::setHuffman(int count, TreeNode* left, TreeNode* right)
{
_left = left;
_right = right;
_data.setCountHuffman(count);
}
bool TreeNode::isHuffmanNode()
{
if (_left == nullptr && _right == nullptr && !_data.isSigned()) return true;
else return false;
}
}