huffman编译码---高分相送。

fengxiarong707 2003-06-08 04:10:15
请各位帮忙,本人不回写这个程序。
...全文
39 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengxiarong707 2003-07-19
  • 打赏
  • 举报
回复
谢谢各位大虾的指示,.不过那的第2个问题希望各位能尽快帮我解决.!就是我朋友的Authorware软件她打开不了,请各位指示.
lsl7909 2003-06-11
  • 打赏
  • 举报
回复
我这有源程序.lsl7909@sina.com
chenmujian81 2003-06-11
  • 打赏
  • 举报
回复
都是乱码
countersars 2003-06-10
  • 打赏
  • 举报
回复
保证正确!!!
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACKSIZE 10
#define INCREASEMENT 5
typedef struct{
char *top;
char *base;
int size;
}STACK;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNODE,*HUFFMANTREE;
typedef char ** HUFFMANCODE;
void huffmancoding(HUFFMANTREE *,HUFFMANCODE *,int *,int);
void gethcode(HUFFMANTREE,HUFFMANCODE *,int);
void select(HUFFMANTREE *,int,int *,int * );

void main()
{
HUFFMANTREE ht;
HUFFMANCODE hc;
/*int w[]={5,29,7,8,14,23,3,11};
int n=8,i; */
int n,i,*w;
printf("\n\npress any key to start!");
getch();
printf("\n\ninput the number:(-1 to end)");
printf("\n");
for(i=0;1;i++){
scanf("%d",&w[i]);
if(w[i]==-1){
i--;
break;
}
}
n=i+1;
huffmancoding(&ht,&hc,w,n);
for(i=1;i<=n;i++) printf("\n\n%s",hc[i]);
}

void select(HUFFMANTREE *ht,int n,int *s1,int *s2)
{
int less,least,i,j,temp;
for(i=1;i<=n;i++)
if((*ht)[i].parent==0){ least=i; break;}
for(i++;i<=n;i++)
if((*ht)[i].parent==0){ less=i; break;}
if((*ht)[least].weight>(*ht)[less].weight){
temp=less;
less=least;
least=temp;
}
for(i++;i<=n;i++){
if((*ht)[i].parent==0){
if((*ht)[i].weight<=(*ht)[least].weight){less=least;least=i;}
else if((*ht)[i].weight<(*ht)[less].weight&&(*ht)[i].weight>=(*ht)[least].weight) less=i;
}
}
*s1=least;
*s2=less;
}
void huffmancoding(HUFFMANTREE *ht,HUFFMANCODE *hc,int *w,int n)
{
int i,m,s1,s2,c,f,start;
HUFFMANTREE p;
char *cd;
if(n<=1) return;
m=2*n-1;
(*ht)=(HUFFMANTREE)malloc((m+1)*sizeof(HTNODE));
for(p=*ht+1,i=1;i<=n;i++,p++,w++){
p->weight=*w;
p->lchild=p->rchild=p->parent=0;
}
for(;i<=m;i++,p++){
p->weight=0;
p->lchild=p->rchild=p->parent=0;
}
for(i=n+1;i<=m;i++){
select(ht,i-1,&s1,&s2);
(*ht)[s1].parent=i;
(*ht)[s2].parent=i;
(*ht)[i].lchild=s1;
(*ht)[i].rchild=s2;
(*ht)[i].weight=(*ht)[s1].weight+(*ht)[s2].weight;
}
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(*ht)[i].parent;f;c=f,f=(*ht)[f].parent)
if((*ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
*ht=p;
free(cd);
}
void gethcode(HUFFMANTREE ht,HUFFMANCODE *hc,int n)
{
int start,c,i,j,f;
char *cd;
(*hc)=(HUFFMANCODE)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(sizeof(char)*n);
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
for(c=i,f=(ht)[i].parent;f;c=f,f=(ht)[f].parent)
if((ht)[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
(*hc)[i]=(char *)malloc(sizeof(char)*(n-start));
strcpy((*hc)[i],&cd[start]);
}
free(cd);
}
WhatCanIdoing 2003-06-09
  • 打赏
  • 举报
回复
//初始化huftree
for(int i=0;i<m+1;++i)
{huftree[i].parent=0; huftree[i].lch=0; huftree[i].rch=0;}
for(int i=1,j=0;i<n+1;++i){
for(;j<=num;){
if(a[j].weight==0) {++j; continue;}
break;}
huftree[i].weight=a[j].weight;
huftree[i].letter=a[j].letter;
++j;}
//把n to m 初始化
for(int i=n+1;i<m+1;++i){
huftree[i].weight=0;
huftree[i].letter=' ';}

//打印初始化结果
outfile<<"letter "<<"weight "<<"parent "<<"lch "<<"rch"<<endl;
for(int i=1;i<m+1;++i){
outfile<<huftree[i].letter<<setw(10)
<<huftree[i].weight<<setw(10)
<<huftree[i].parent<<setw(10)
<<huftree[i].lch<<setw(10)
<<huftree[i].rch<<endl;}
outfile<<endl;

for(int i=n+1;i<m+1;++i) //建立huftree
{

int s1=1,s2=1;
for(int k=1;k<i;++k){ //min_1
if(huftree[k].parent!=0) continue;
if(huftree[s1].parent!=0){s1=k; continue;}
if(huftree[s1].weight>huftree[k].weight)
s1=k;
}

for(int k=1;k<i;++k){ //min_2
if(huftree[k].parent!=0 || k==s1) {if(s1==s2) ++s2; continue;}
if(huftree[s2].parent!=0){ s2=k; continue;}
if(huftree[s2].weight>huftree[k].weight)
s2=k;
}

huftree[s1].parent=i;
huftree[s2].parent=i;
huftree[i].lch=s1;
huftree[i].rch=s2;
huftree[i].weight=huftree[s1].weight+huftree[s2].weight;

//打印结果
outfile<<"min_1 huftree["<<s1<<"].weight="<<huftree[s1].weight<<endl
<<"min_2 huftree["<<s2<<"].weight="<<huftree[s2].weight<<endl;
outfile<<"letter "<<"weight "<<"parent "<<"lch "<<"rch"<<endl;
for(int i=1;i<m+1;++i){
outfile<<huftree[i].letter<<setw(10)
<<huftree[i].weight<<setw(10)
<<huftree[i].parent<<setw(10)
<<huftree[i].lch<<setw(10)
<<huftree[i].rch<<endl;}
outfile<<endl;
}

//求hufcode
for(int i=1;i<n+1;++i)
{
cd.start=n-1;
int c=i; int f=huftree[c].parent;
while(f!=0){
if(huftree[f].lch==c)
cd.bits[cd.start]='0';
else
cd.bits[cd.start]='1';
cd.start=cd.start-1; c=f; f=huftree[f].parent;
} //while
hufcode[i]=cd;
for(int j=0;j<n;++j) cd.bits[j]=' ';
} //for

//打印hufcode
for(int i=1;i<n+1;++i){
for(int j=0;j<n;++j)
outfile<<hufcode[i].bits[j]<<' ';
outfile<<"hufcode["<<i<<"].start="<<hufcode[i].start<<endl;
}

//将原文译为hufcode
while(infile2.get(CHAR)){
for(int i=1;i<n+1;++i){
if(huftree[i].letter==CHAR)
for(int j=hufcode[i].start+1;j<n;++j){
outfile2<<hufcode[i].bits[j];}
}
}
}

我刚学C++ 时写的,^_^
WhatCanIdoing 2003-06-09
  • 打赏
  • 举报
回复
(1) 初始化(可对任一个不含中文的文本进行字符出现次数统计,初始化哈夫曼树);
(2) 对以上文本进行编码;
在文件"in_put.txt"中读入char,完成统计char个数,初始化并建立huffmantree,建立huffmancode,输出在文件"huffmantree.doc"中,并将"in_put.txt"译成huffmancode输出在文件"code_hufcode.doc"中

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//用于统计字符出现个数的辅助结构
struct statrstics{
char letter;
long weight;
};

struct nodetype{
char letter;
unsigned int weight;
unsigned int parent,lch,rch;
};

void huffmantree_code()
{
//所需要的各文本文件
ofstream outfile("huffmantree.txt");
ofstream outfile2("code_hufcode.txt");
ifstream infile("in_file.txt");
ifstream infile2("in_file.txt");

if(!infile){ //判断文件是否打开
cerr<<"error:unable to open input file!\n";
exit(-1);
}
if(!outfile){
cerr<<"eroor:unble to open output file!\n";
exit(-2);
}
if(!outfile2){
cerr<<"error:unable to open input file!\n";
exit(-3);
}

//初始化 a 用于建立huffman_tree
const int num=94;
statrstics a[num];
for(int i=0,j=32;i<=94;++i,++j)
{
a[i].letter=j;
a[i].weight=0;
}

//统计char出现次数
char CHAR;
while(infile.get(CHAR)){
for(int i=0;i<=num;++i)
if(a[i].letter==CHAR) ++a[i].weight;}


//计算x用来初始化n
int x=0;
for(int i=0;i<=num;++i)
if(a[i].weight!=0)
x=x+1;

const int n=x;
const int m=2*n-1;

//字符的编码
struct codetype{
char bits[n];
int start;
};

nodetype huftree[m+1];
codetype hufcode[n+1];
codetype cd;
for(int j=0;j<n;++j) cd.bits[j]=' ';
huigll 2003-06-09
  • 打赏
  • 举报
回复
//拷于csdn文档
#include <string.h>
#include <stdio.h>


#define MAX_NODE 1024

#define MAX_WEIGHT 4096



typedef struct HaffmanTreeNode {

char ch, code[15];

int weight;

int parent, lchild, rchild;

} HTNode, *HaTree;



typedef struct {

HTNode arr[MAX_NODE];

int total;

} HTree;



/* 统计字符出现的频率 */

int statistic_char(char *text, HTree *t){

int i, j;

int text_len = strlen(text);

t->total = 0;

for (i=0; i<text_len; i++) {

for (j=0; j<t->total; j++) if (t->arr[j].ch == text[i]){

t->arr[j].weight ++;

break;

}

if (j==t->total) {

t->arr[t->total].ch = text[i];

t->arr[t->total].weight = 1;

t->total ++;

}

}

printf("char frequence\n");

for (i=0; i<t->total; i++)

printf("'%c' %d\n", t->arr[i].ch, t->arr[i].weight);

printf("\n");

return t->total;

}



int create_htree(HTree *t)

{

int i;

int total_node = t->total * 2 - 1;

int min1, min2; /* 权最小的两个结点 */

int min1_i, min2_i; /* 权最小结点对应的编号 */

int leaves = t->total;

for (i=0; i<leaves; i++) {

t->arr[i].parent = -1;

t->arr[i].rchild = -1;

t->arr[i].lchild = -1;

}

while (t->total < total_node) {

min1 = min2 = MAX_WEIGHT;

for (i=0; i<t->total; i++) { /* 对每一个结点 */

if (t->arr[i].parent == -1 /* 结点没有被合并 */

&& t->arr[i].weight < min2) { /* 结点的权比最小权小 */

if (t->arr[i].weight < min1) { /* 如果它比最小的结点还小 */

min2_i = min1_i; min2 = min1;

min1_i = i; min1 = t->arr[i].weight;

}

else

{

min2_i = i; min2 = t->arr[i].weight;

}

}

}

t->arr[t->total].weight = min1 + min2;

t->arr[t->total].parent = -1;

t->arr[t->total].lchild = min1_i;

t->arr[t->total].rchild = min2_i;

t->arr[min1_i].parent = t->total;

t->arr[min2_i].parent = t->total;

t->arr[t->total].ch = ' ';

t->total ++;

}

return 0;

}



/* 对哈夫曼树进行编码 */

void coding(HTree *t, int head_i, char *code)

{

if ( head_i == -1) return;

if (t->arr[head_i].lchild == -1 && t->arr[head_i].rchild == -1) {

strcpy(t->arr[head_i].code, code);

printf("'%c': %s\n", t->arr[head_i].ch, t->arr[head_i].code);

}

else {

int len = strlen(code);

strcat(code, "0");

coding(t, t->arr[head_i].lchild, code);

code[len] = '1';

coding(t, t->arr[head_i].rchild, code);

code[len] = '\0';

}

}



/* 中序打印树 */

void print_htree_ldr(HTree *t, int head_i, int deep, int* path)

{

int i;

if (head_i == -1) return;

path[deep] = 0;

print_htree_ldr(t, t->arr[head_i].lchild, deep + 1, path);

if (deep) printf(" ");

for (i=1; i<deep; i++) printf("%s", path[i]==path[i-1]?" ":"│ ");

int dir = path[i]==path[i-1];

if ( t->arr[head_i].lchild == -1 && t->arr[head_i].rchild == -1)

printf("%s── %d '%c'\n", dir? "┌":"└",

t->arr[head_i].weight, t->arr[head_i].ch);

else if (head_i != t->total-1)

printf("%s─%02d┤\n", dir? "┌":"└", t->arr[head_i].weight);

else

printf(" %02d┤\n", t->arr[head_i].weight);

path[deep] = 1;

print_htree_ldr(t, t->arr[head_i].rchild, deep + 1, path);

}



/* 对字符进行编码 */

void code_string(char *text, HTree *t)

{

int i, j, text_len = strlen(text);

int n = 0;

for (i=0; i<text_len; i++) {

char ch = text[i];

for (j=0; j<t->total; j++) if (ch == t->arr[j].ch) {

printf("%s ", t->arr[j].code);

n += strlen(t->arr[j].code);

break;

}

}

printf("\n%d chars, Total len = %d\n", text_len, n);

}



int main(int argc, char* argv[])

{

HTree t;

char text[128]="ABAAAAEEEAAACCCCAAAACCDEA";

char code[128] = "";

int path[16]={0};

statistic_char(text, &t);

create_htree(&t);

print_htree_ldr(&t, t.total-1, 0, path);

coding(&t, t.total-1, code);

code_string(text, &t);

return 0;

}


julise 2003-06-08
  • 打赏
  • 举报
回复
你一是学“数据结构”了!我可以提一点!其基本思想是比较简单的:
A B C D
0\ /1 0\ /1
E F
0\ /1
G
这样A为00
B为01
C为10
D为11
明白点吧!
snipersu 2003-06-08
  • 打赏
  • 举报
回复
到网上搜一个,到处是.

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧