hafumanshuWord文档格式.docx

上传人:b****6 文档编号:18872573 上传时间:2023-01-01 格式:DOCX 页数:30 大小:27.13KB
下载 相关 举报
hafumanshuWord文档格式.docx_第1页
第1页 / 共30页
hafumanshuWord文档格式.docx_第2页
第2页 / 共30页
hafumanshuWord文档格式.docx_第3页
第3页 / 共30页
hafumanshuWord文档格式.docx_第4页
第4页 / 共30页
hafumanshuWord文档格式.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

hafumanshuWord文档格式.docx

《hafumanshuWord文档格式.docx》由会员分享,可在线阅读,更多相关《hafumanshuWord文档格式.docx(30页珍藏版)》请在冰豆网上搜索。

hafumanshuWord文档格式.docx

while(i<

l)

q=(zhizhend)malloc(sizeof(elem));

p->

next=q;

p=q;

i++;

}

next=head;

return 

1;

chuangjian(dui 

d)//创建一个空队

a;

a=xunhuan(d.base,maxsize);

if(a==0)return 

d.size=0;

chazhao(dui 

d,zhizhend 

p)//找到队列的头结点,用p返回其地址值

if(d.size==0)return 

p=d.base;

=maxsize-d.size+1)///////

p=p->

next;

jindui(dui 

d,zhizhen 

e)//进队

if(d.size>

=maxsize)return 

d.base=d.base->

d.base->

xhx=e;

d.size++;

chudui(dui 

e)//出队,用e返回出队元素的值 

p;

a=chazhao(d,p);

e=p->

d.size--;

//队完

beiyong(zhizhen 

li,zhizhen 

s)//创建一个备用静态链表 

li返回其首地址 

并创建一个已用空链表 

用s返回其首地址

i;

li=(zhizhen)malloc(256*sizeof(lian));

if(!

li)return 

for(i=0;

i<

255;

i++)

(li+i)->

next=i+1;

l=0;

r=0;

quan=0;

/////////////////////////

 

(li+255)->

next=0;

s=li+1;

li->

next=s->

s->

jmalloc(zhizhen 

s)//从备用链表中取一个结点放入已用链表s中

p=s->

next=li->

q=(li+li->

next)->

(s+s->

next-1)->

next=p;

min(int 

i,int 

j,zhizhen 

s)//找到链表s中权值最小的两个结点

i=s->

j=(s+s->

p=(s+j-1)->

while(p)

if((s+p-1)->

quan<

(s+i-1)->

quan||(s+p-1)->

(s+j-1)->

quan)

quan>

quan)j=p;

else

quan)i=p;

if((s+i-1)->

else 

j=p;

p=(s+p-1)->

jfree(int 

s)//从s中把i,j结点剔除

p=1;

next==i)(s+p-1)->

next=(s+i-1)->

next==j)(s+p-1)->

next=(s+j-1)->

hafuman(zhizhen 

s,zhizhen 

li)//创建一棵霍夫曼树

i,j;

while((s+s->

next)

min(i,j,s);

jmalloc(li,s);

l=i;

*((s+i-1)->

yima)='

0'

;

yima+1)='

\0'

r=j;

*((s+j-1)->

1'

quan=((s+i-1)->

quan+(s+j-1)->

quan);

作者:

徐小孩 

2008-1-2623:

52 

回复此发言 

2

jfree(i,j,s);

hebing(zhizhen 

a,zhizhen 

b)//把a与b所指向的yima合并 

并把所得字符串付给b

i=0,j=0;

p[20];

while(a->

yima[i]!

='

p[i]=a->

yima[i];

while(b->

yima[j]!

p[i]=b->

yima[j];

j++;

j=0;

while(j<

i)

b->

yima[j]=p[j];

yima[j]='

bianma(zhizhen 

s)//编码

e;

dui 

d;

p,i=0;

chuangjian(d);

jindui(d,(s+p-1));

while

(1)

chudui(d,e);

if(e->

l==0)

if(d.size==0)break;

continue;

jindui(d,(s+e->

l-1));

r-1));

i){i=1;

hebing(e,(s+e->

zhao(jie 

*m,int 

i)//查找数组m中前i-1个元素是否有与m[i]值相等的元素 

有返回值为j 

否为-1

j;

c;

c=(m+i)->

for(j=0;

j<

j++)///////////////////

if((m+j)->

zifu==c)return 

-1;

shuru(jie 

i,zhizhen 

li,int 

len)//字符串的输入,m为字符存储所用链表的首地址,i为字符链表的长度,len为所输入字符中无重复的字符个数

i=0;

len=0;

beiyong(li,s);

while((c=getchar())!

\n'

(m+i)->

zifu=c;

j=zhao(m,i);

if(j!

=-1)

zhixiang=(m+j)->

(s+(m+j)->

zhixiang-1)->

quan++;

zhixiang=s->

(s+(m+i)->

date=c;

len++;

void 

print(jie 

s)//输出编码

j++)/////////

printf("

%c 

"

(m+j)->

zifu);

%s 

(s+(m+j)->

yima);

\n"

);

pipei(char 

*a,char 

*b)//判断a、b两个字符串是否相等

while(*(a+i)!

if(*(a+i)==*(b+i))i++;

if(*(b+i)=='

)return 

yima(zhizhen 

li,char 

*ma,int 

len,char 

c)//译码

p=li+2;

=len)

if(pipei(p->

yima,ma))

c=p->

date;

p++;

main()

jie 

m[50];

//i表征m的长度

qw,len;

ma[20],c;

s,li;

请输入一个字符串(字符串中至少有两个不同的字符)\n"

shuru(m,i,s,li,len);

hafuman(s,li);

bianma(s);

其哈夫曼编码为\n"

print(m,i,s);

请输入操作\n1、译码 

2、退出\n"

scanf("

%d"

&

qw);

if(qw==1)

请输入所要翻译的编码\n"

%s"

ma);

if(yima(li,ma,len,c))printf("

其对应的字符为%c\n"

c);

输入有错\n"

if(qw==2)break;

if(qw!

=1&

qw!

=2)printf("

贪心法求哈夫曼编码

huffman.cpp:

iostream.h>

math.h>

#include"

huffman.h"

#defineDN62

#defineSIZE200

voidmain(){

char*sym="

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

chars[SIZE],a[20],c;

char*d=newchar[5*SIZE];

cout<

<

请输入符号序列:

cin>

>

s;

ExtBinTree<

char>

h(DN,s,sym);

h.HuffmanTree();

inti=0;

编码:

while(s[i]!

){

h.Coding(s[i]);

i++;

}

endl;

intn=i;

各字符出现的频率及码字:

int*f=h.GetFr();

for(i=0;

DN;

i++)

if(f[i]!

=0)

sym[i]<

'

:

double(f[i])/n<

\t'

h.Coding(sym[i])<

输入符号熵:

h.Entropy(0)<

编码平均码长:

h.Entropy

(1)<

i=0;

译码:

if(i==0)d=strcpy(d,h.Coding(s[i]));

elsed=strcat(d,h.Coding(s[i]));

h.DeCoding(d);

}

minheap.h:

template<

classType>

classMinHeap{

public:

MinHeap(intmaxSize);

//常成员函数不能修改this指向的内容

MinHeap(Typearr[],intn);

~MinHeap(){delete[]heap;

intInsert(constType&

x);

intRemoveMin(Type&

//用参数作返回值

TypeDeleteMin();

intIsEmpty()const{returnCurrentSize==0;

intIsFull()const{returnCurrentSize==MaxHeapSize;

voidMakeEmpty(){CurrentSize=0;

private:

enum{DefaultSize=10};

Type*heap;

intCurrentSize;

intMaxHeapSize;

voidFilterDown(constintstart,constintend);

voidFilterUp(intstart);

};

MinHeap<

Type>

MinHeap(intmaxSize){//根据给定大小maxSize,建立堆对象

MaxHeapSize=DefaultSize<

maxSize?

maxSize:

DefaultSize;

//确定堆大小

heap=newType[MaxHeapSize];

//创建堆空间

CurrentSize=0;

//初始化

MinHeap(Typearr[],intn){//根据给定数组中的数据和大小,建立堆对象

n?

n:

heap=arr;

//数组传送

CurrentSize=n;

//当前堆大小

intcurrentPos=(CurrentSize-2)/2;

//最后非叶

while(currentPos>

=0){//从下到上逐步扩大,形成堆

FilterDown(currentPos,CurrentSize-1);

//从currentPos开始,到CurrentSize-1为止,调整

currentPos--;

intMinHeap<

Insert(constType&

x){//在堆中插入新元素x

if(CurrentSize==MaxHeapSize){//堆满

堆已满"

return0;

heap[CurrentSize]=x;

//插在表尾

FilterUp(CurrentSize);

//向上调整为堆

CurrentSize++;

//堆元素增一

return1;

voidMinHeap<

FilterUp(intstart){//从start开始,向上直到0,调整堆

intj=start,i=(j-1)/2;

//i是j的双亲

Typetemp=heap[j];

while(j>

0){

if(heap[i].root->

data.key<

=temp.root->

data.key)break;

else{heap[j]=heap[i];

j=i;

i=(i-1)/2;

heap[j]=temp;

FilterDown(constintstart,constintend){

inti=start,j=2*i+1;

Typetemp=heap[i];

while(j<

=end){

if(j<

end&

heap[j].root->

data.key>

heap[j+1].root->

data.key)j++;

if(temp.root->

=heap[j].root->

else{

heap[i]=heap[j];

i=j;

j=2*j+1;

heap[i]=temp;

RemoveMin(Type&

x){

if(!

CurrentSize){

Heapempty"

x=heap[0];

heap[0]=heap[CurrentSize-1];

CurrentSize--;

FilterDown(0,CurrentSize-1);

TypeMinHeap<

DeleteMin(){//不可以用引用,因为堆扩展时会覆盖返回的元素,引用会出错

Typex=heap[0];

returnx;

huffman.h:

minheap.h"

string.h>

structNodeData{

Typesymbol;

//结点代表的符号

intkey;

//记录频数

charnum;

//结点编码

classExtBinTree;

classElement{

friendclassExtBinTree<

friendclassMinHeap<

ExtBinTree<

>

Element<

(Typena='

^'

intf=0,charn='

data.key=f;

data.symbol=na;

data.num=n;

parent=leftChild=rightChild=NULL;

NodeData<

data;

*parent,*leftChild,*r

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 国外设计风格

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1