huffman算法c语言程序,哈夫曼算法构造代码

#include

#include

using namespace std;

struct Node{

char c;

int value;

int par;

char tag;    //tag='0',表示左边;tag='1',表示右边

bool isUsed;    //判断这个点是否已经用过

Node(){

par=-1;

isUsed=false;

}

};

int input(Node*,int);   //输入节点信息

int buildedTree(Node*,int); //建哈夫曼树

int getMin(Node*,int);  //寻找未使用的,具有最小频率值的节点

int outCoding(Node*,int);   //输出哈夫曼编码

int main ()

{

int n;

cin>>n;

Node *nodes=new Node[2*n-1];

input(nodes,n);

buildedTree(nodes,n);

outCoding(nodes,n);

delete(nodes);

return 0;

}

int input(Node* nodes,int n){

for(int i=0;i

cin>>(nodes+i)->c;

cin>>(nodes+i)->value;

}

return 0;

}

int buildedTree(Node* nodes,int n){

int last=2*n-1;

int t1,t2;

for(int i=n;i

t1=getMin(nodes,i);

t2=getMin(nodes,i);

(nodes+t1)->par=i; (nodes+t1)->tag='0';

(nodes+t2)->par=i; (nodes+t2)->tag='1';

(nodes+i)->value=(nodes+t1)->value+(nodes+t2)->value;

}

return 0;

}

int getMin(Node* nodes,int n){

int minValue=10000000;

int pos=0;

for(int i=0;i

{

if((nodes+i)->isUsed == false && (nodes+i)->value

minValue=(nodes+i)->value;

pos=i;

}

}

(nodes+pos)->isUsed=true;

return pos;

}

int outCoding(Node* nodes,int n){

char a[100];

int pos,k,j;

char tmp;

for(int i=0;i

k=0;

pos=i;

memset(a,'\0',sizeof(a));

while((nodes+pos)->par!=-1){

a[k++]=(nodes+pos)->tag;

pos=(nodes+pos)->par;

}

strrev(a);    //翻转字符串

cout<c<value<

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言基于哈夫曼树的数据压缩算法代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_FREQ 256 typedef struct { unsigned char ch; int freq; } node_t; node_t heap[MAX_FREQ]; int heap_size = 0; void swap(node_t *a, node_t *b) { node_t tmp = *a; *a = *b; *b = tmp; } void push_heap(node_t x) { heap[++heap_size] = x; int i = heap_size; while (i > 1 && heap[i].freq < heap[i/2].freq) { swap(&heap[i], &heap[i/2]); i /= 2; } } node_t pop_heap() { node_t ret = heap[1]; heap[1] = heap[heap_size--]; int i = 1; while (i*2 <= heap_size) { int child = i*2; if (child+1 <= heap_size && heap[child+1].freq < heap[child].freq) child++; if (heap[i].freq < heap[child].freq) break; swap(&heap[i], &heap[child]); i = child; } return ret; } int freq[MAX_FREQ]; char code[MAX_FREQ][MAX_FREQ]; int code_len[MAX_FREQ]; void build_huffman_tree() { while (heap_size > 1) { node_t a = pop_heap(); node_t b = pop_heap(); node_t c = {0, a.freq + b.freq}; push_heap(c); for (int i = 0; i < MAX_FREQ; i++) { code[c.ch][i] = '\0'; if (code[a.ch][i] != '\0') { code[c.ch][i+1] = '\0'; strcat(code[c.ch], code[a.ch]); code_len[c.ch] = strlen(code[c.ch]); } if (code[b.ch][i] != '\0') { code[c.ch][i+1] = '\0'; strcat(code[c.ch], code[b.ch]); code_len[c.ch] = strlen(code[c.ch]); } } } } int main() { char str[] = "hello world"; int len = strlen(str); for (int i = 0; i < len; i++) freq[str[i]]++; for (int i = 0; i < MAX_FREQ; i++) { if (freq[i] > 0) { node_t n = {i, freq[i]}; push_heap(n); } } build_huffman_tree(); printf("Original string: %s\n", str); printf("Huffman code:\n"); for (int i = 0; i < len; i++) { printf("%s", code[str[i]]); } printf("\n"); return 0; } ``` 该程序首先统计给定字符串中每个字符出现的频率,然后使用哈夫曼算法将字符编码为可变长度的二进制编码。程序使用了一个最小堆来维护频率最小的节点,因此该程序的时间复杂度为 $O(n \log n)$,其中 $n$ 是字符集大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值