-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
144 lines (125 loc) · 4.74 KB
/
node.h
File metadata and controls
144 lines (125 loc) · 4.74 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
using namespace std;
//先声明
template <class char_type, class weight_type>
class HuffNode // huffman 树的节点的抽象类
{
public:
virtual weight_type get_weight() = 0; //返回节点的权值
virtual bool is_leaf() = 0; //判断是不是叶子节点
virtual HuffNode<char_type, weight_type>* get_left() = 0; //返回当前节点的指向左孩子节点的指针
virtual HuffNode<char_type, weight_type>* get_right() = 0; //返回当前节点的指向右孩子节点的指针
virtual void set_left(HuffNode<char_type, weight_type> *child) = 0; //设置当前节点的左孩子节点
virtual void set_right(HuffNode<char_type, weight_type> *child) = 0; //设置当前节点的右孩子节点
};
template <class char_type, class weight_type>
class HuffLeafNode: public HuffNode<char_type, weight_type> //huffman 树的叶子节点
{
public:
HuffLeafNode(const char_type &ch, const weight_type &w); //构造函数
virtual ~HuffLeafNode(){} //析构函数
char_type get_char(); //返回叶节点的字符
weight_type get_weight(); //返回节点的权值
bool is_leaf(); //判断是否为叶节点
HuffNode<char_type, weight_type>* get_left(); //返回当前节点的指向左孩子节点的指针
HuffNode<char_type, weight_type>* get_right(); //返回当前节点的指向右孩子节点的指针
void set_left(HuffNode<char_type, weight_type> *child){} //设置当前节点的左孩子节点
void set_right(HuffNode<char_type, weight_type> *child){} //设置当前节点的右孩子节点
private:
char_type cha; //叶节点存储的字符
weight_type weight; //叶节点的权重
};
template<class char_type, class weight_type>
class HuffInNode: public HuffNode<char_type, weight_type>
{
public:
HuffInNode(const weight_type &w,
HuffNode<char_type, weight_type> *lc = nullptr, HuffNode<char_type, weight_type> *rc = nullptr); //构造函数
virtual ~HuffInNode(){} //析构函数
weight_type get_weight(); //返回该节点权重
bool is_leaf(); //判断是否为叶节点
HuffNode<char_type, weight_type>* get_left(); //返回当前节点的指向左孩子节点的指针
HuffNode<char_type, weight_type>* get_right(); //返回当前节点的指向右孩子节点的指针
void set_left(HuffNode<char_type, weight_type> *child); //设置当前节点的左孩子节点
void set_right(HuffNode<char_type, weight_type> *child); //设置当前节点的右孩子节点
private:
HuffNode<char_type, weight_type> *l_child; //指向左孩子的指针
HuffNode<char_type, weight_type> *r_child; //指向右孩子的指针
weight_type weight; //当前节点的权重
};
//后定义
//HuffLeafNode 实现
template<class char_type, class weight_type>
HuffLeafNode<char_type, weight_type>::HuffLeafNode(const char_type &ch, const weight_type &w)
{
weight = w;
cha = ch;
}
template<class char_type, class weight_type>
char_type HuffLeafNode<char_type, weight_type>::get_char()
{
return cha;
}
template<class char_type, class weight_type>
weight_type HuffLeafNode<char_type, weight_type>::get_weight()
{
return weight;
}
template<class char_type, class weight_type>
bool HuffLeafNode<char_type, weight_type>::is_leaf()
{
return true;
}
template<class char_type, class weight_type>
HuffNode<char_type, weight_type>* HuffLeafNode<char_type, weight_type>::get_left()
{
return nullptr;
}
template<class char_type, class weight_type>
HuffNode<char_type, weight_type>* HuffLeafNode<char_type, weight_type>::get_right()
{
return nullptr;
}
//HuffInNode 实现
template<class char_type, class weight_type>
HuffInNode<char_type, weight_type>::HuffInNode(const weight_type &w,
HuffNode<char_type, weight_type> *lc, HuffNode<char_type, weight_type> *rc)
{
weight = w;
l_child = lc;
r_child = rc;
}
template<class char_type, class weight_type>
weight_type HuffInNode<char_type, weight_type>::get_weight()
{
return weight;
}
template<class char_type, class weight_type>
bool HuffInNode<char_type, weight_type>::is_leaf()
{
return false;
}
template<class char_type, class weight_type>
HuffNode<char_type, weight_type>* HuffInNode<char_type, weight_type>::get_left()
{
return l_child;
}
template<class char_type, class weight_type>
HuffNode<char_type, weight_type>* HuffInNode<char_type, weight_type>::get_right()
{
return r_child;
}
template<class char_type, class weight_type>
void HuffInNode<char_type, weight_type>::set_left(HuffNode<char_type, weight_type> *child)
{
l_child = child;
return;
}
template<class char_type, class weight_type>
void HuffInNode<char_type, weight_type>::set_right(HuffNode<char_type, weight_type> *child)
{
r_child = child;
return;
}
#endif // NODE_H_INCLUDED