-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHuffNode.cpp
More file actions
53 lines (51 loc) · 975 Bytes
/
Copy pathHuffNode.cpp
File metadata and controls
53 lines (51 loc) · 975 Bytes
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
//Huffman tree node abstract base class
template <typename E> class HuffNode
{
public:
virtual ~HuffNode() {}
virtual int weight() =0;
virtual bool isLeaf() =0;
};
template <typename E>
class LeafNode : public HuffNode<E>
{
private:
E it;
int wgt;
public:
LeafNode(const E& val, int freq)
{
it = val;
wgt = freq;
}
int weight() {return wgt;}
E val() {return it;}
bool isLeaf() {return true;}
};
template<typename E>
class IntlNode : public HuffNode<E>
{
private:
HuffNode<E>* lc;
HuffNode<E>* rc;
int wgt;
public:
IntlNode(HuffNode<E>* l, HuffNode<E>* r)
{
wgt = l->weight() + r->weight();
lc = l;
rc = r;
}
int weight() {return wgt;}
bool isLeaf() {return false;}
HuffNode<E>* left() const {return lc;}
void setLeft(HuffNode<E>* b)
{
lc = (HuffNode<E>*)b;
}
HuffNode<E>* right() const {return rc;}
void setRight(HuffNode<E>* b)
{
rc = (HuffNode<E>*)b;
}
};