-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.h
executable file
·72 lines (64 loc) · 1.55 KB
/
TreeNode.h
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
#ifndef __TREENODE_H__
#define __TREENODE_H__
#include <string>
/*
* A Huffman Tree Node
* Represents both leaf and internal nodes
*/
class Node
{
private:
unsigned char value;
double prob;
const Node* lchild;
const Node* rchild;
public:
/*
* Constructs a new leaf node for character _value
*/
Node (unsigned char _value = 0, double _prob = -1)
{
value = _value;
prob = _prob;
lchild = 0;
rchild = 0;
}
/*
* Constructs a new internal node with the probability
* equal to the sum of the probabilities of each
* child node
*/
Node (const Node* _lchild, const Node* _rchild)
{
value = 0;
prob = _lchild -> prob + _rchild -> prob;
lchild = _lchild;
rchild = _rchild;
}
// Destructor
~Node ()
{
if (lchild)
return;
delete lchild;
delete rchild;
}
/*
* Overloading the '<' operator
* Used as a comparison operator to order the priority queue
* The less likely the character is, the higher the priority
*/
bool operator < (const Node &n) const
{ return prob > n.prob; }
/*
* Stores the Huffman code
*/
void store_code (std::string* arr, std::string code = "") const;
/*
* Accessor Methods
*/
const Node* get_lchild () const { return lchild; }
const Node* get_rchild () const { return rchild; }
unsigned char get_value () const { return value; }
};
#endif