利用优先队列(priority_queue)实现哈夫曼编码

 代码

#include<iostream>
#include <string>
#include <queue>
#include <utility>
#include<cstring>
#include<stdlib.h>
#include <algorithm>
using namespace std;
typedef struct treenode{
   char c;
    int weight;
    string huffcode;
    treenode *lchild,*rchild;
    treenode():lchild(NULL),rchild(NULL),c('\0'),weight(0),huffcode(""){}
}treenode;
struct cmp1 {
    bool operator() (treenode *a, treenode * b) {
        return a->weight > b->weight;
    }
};
struct cmp2 {
    bool operator() (treenode *a, treenode * b) {
        return a->c > b->c;
    }
};
priority_queue <treenode*,vector<treenode*>,cmp2 > res;
priority_queue <treenode*,vector<treenode*>,cmp1 > q;
void creatcode(treenode *t)
{
      
    //cout<<t->weight<<endl;
    if(t->lchild)
    {
        t->lchild->huffcode = t->huffcode+"0";
        creatcode(t->lchild);
    }
 if(!(t->lchild)&&!(t->rchild))
 {
     res.push(t);
     /*a[cnt++].c=t->c;
     a[cnt++].huffcode=t->huffcode;*/
 }
    if(t->rchild)
    {
        t->rchild->huffcode=t->huffcode+"1";
        creatcode(t->rchild);
    }
}
int main()
{
    char st[10];
    int x;
   while(cin>>st)
   {
       x=0;
       for (int i = 2; i < strlen(st); i ++ )
       {  
           x=x*10+st[i]-'0';
       }
         treenode *t=new treenode;
       t->c=st[0];
       //cout<<t->c<<' '<<x<<endl;
       t->weight=x;
       q.push(t);
   }
     //依次找出权值最小的两个结点
    while(q.size()!=1)
   {
       treenode *x,*y;
       x=q.top();
       q.pop();
       y=q.top();
       q.pop();
       //cout<<x->c<<' '<<x->weight<<' '<<y->c<<' '<<y->weight<<endl;
       treenode *t= new treenode;
       t->weight=x->weight+y->weight;
      // cout<<t->weight<<endl;
       t->lchild=x;
       t->rchild=y;
       q.push(t);
       //cout<<q.top()->weight;
   }
  // cout<<q.top()->lchild->lchild->weight<<endl;
    creatcode(q.top());
    while(res.size())
    {
        cout<<res.top()->c<<':'<<res.top()->huffcode<<endl;
        res.pop();
    }
    /*int len=sizeof(a)/sizeof(treenode);
    sort(a,a+len);
    for (int i = 0; i < 8; i ++ )
    cout<<a[i].c<<':'<<a[i].huffcode<<endl;*/
}

有个问题找了特别久,就是结构体的初始化,不初始化会出现奇怪的bug,还是得把语法学好啊

以及本来想用个结构体存结果的,但不知为啥也报了错。。。干脆就再用个队列吧

根据引用中提到的,优先队列的时间复杂度为O(logn),其中n为队列中元素的个数。因此,使用priority_queue实现哈夫曼树的时间复杂度为O(nlogn),其中n为哈夫曼树中叶子节点的个数。 实现哈夫曼树的步骤如下: 1. 统计每个字符在文本中出现的频率,并将其作为权值构建一个森林,每个节点都是一棵只包含一个字符的树。 2. 从森林中选出两棵根节点权值最小的树,将它们合并成一棵新树,新树的根节点权值为两棵树的根节点权值之和。 3. 将新树插入到森林中,并删除原来的两棵树。 4. 重复步骤2和3,直到森林中只剩下一棵树,即为哈夫曼树。 下面是使用priority_queue实现哈夫曼树的C++代码示例: ```c++ #include <iostream> #include <queue> using namespace std; struct TreeNode { char ch; int freq; TreeNode *left, *right; TreeNode(char c, int f) : ch(c), freq(f), left(NULL), right(NULL) {} }; struct cmp { bool operator() (TreeNode* a, TreeNode* b) { return a->freq > b->freq; } }; TreeNode* buildHuffmanTree(string s) { int n = s.size(); vector<int> freq(256, 0); for (int i = 0; i < n; i++) { freq[s[i]]++; } priority_queue<TreeNode*, vector<TreeNode*>, cmp> pq; for (int i = 0; i < 256; i++) { if (freq[i] > 0) { pq.push(new TreeNode(i, freq[i])); } } while (pq.size() > 1) { TreeNode* left = pq.top(); pq.pop(); TreeNode* right = pq.top(); pq.pop(); TreeNode* parent = new TreeNode('#', left->freq + right->freq); parent->left = left; parent->right = right; pq.push(parent); } return pq.top(); } int main() { string s = "hello world"; TreeNode* root = buildHuffmanTree(s); // do something with the Huffman tree return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值