易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
C++使用优先队列来构建huffman树[哈夫曼树]
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
#include <algorithm>
using
namespace
std;
char
Table[26];
struct
Node
{
int
freq;
char
val;
Node * left;
Node * right;
Node():left(NULL), right(NULL) , freq(0) , val(
"0"
){}
};
class
Cmp
{
public
:
bool
operator() (
const
Node * a,
const
Node * b)
const
{
return
a->freq > b->freq;
// 从小到大 ,freq 小的 优先级别高
}
};
priority_queue<Node* , vector<Node*> , Cmp> myQueue;
void
BuildTree()
{
for
(
int
i = 0; i < 26; ++ i)
{
if
(Table[i] > 0)
{
Node * node =
new
Node();
node->freq = Table[i];
node->val =(
char
) (i +
"A"
);
myQueue.push(node);
}
}
while
(myQueue.size() > 1)
{
Node * f = myQueue.top();
myQueue.pop();
Node * s = myQueue.top();
myQueue.pop();
Node * tmp =
new
Node();
tmp->freq = f->freq + s->freq;
tmp->left = f;
tmp->right = s;
myQueue.push(tmp);
}
//cout << myQueue.top()->freq<<endl;
}
struct
PrintNode
{
int
freq;
char
val;
string code;
};
vector<PrintNode> vpn;
bool
cmp(
const
PrintNode & a ,
const
PrintNode & b)
{
return
a.freq > b.freq;
}
void
Print( Node * node , string res)
{
if
(node == NULL)
{
return
;
}
if
(node->val !=
"0"
)
{
PrintNode pn;
pn.val = node->val;
pn.freq = node->freq;
pn.code = res;
vpn.push_back(pn);
//cout <<node->val <<" | " << node->freq <<" | "<< res <<endl;
return
;
}
Print(node->left , res +
"0"
);
Print(node->right, res +
"1"
);
delete
node->left;
delete
node->right;
}
int
main()
{
int
N;
memset(Table , 0 ,
sizeof
(Table));
cin >> N;
for
(
int
i = 0; i < N; ++i)
{
char
ch ;
cin >> ch;
if
(ch >=
"A"
&& ch <=
"Z"
)
{
++Table[ch -
"A"
];
}
}
BuildTree();
Node * root = myQueue.top();
myQueue.pop();
Print(root ,
""
);
sort(vpn.begin() , vpn.end() , cmp);
for
(
int
i = 0; i < vpn.size(); ++i)
{
cout <<vpn[i].val <<
" "
<<vpn[i].freq <<
" "
<< vpn[i].code<<endl;
}
return
0;
}
具体的构成算法不再这里讨论了,
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图