⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 赫夫曼.txt

📁 此文件是哈夫曼程序源代码,如果程序不能打开,有TXT格式的,复制下,运行就可以了.但文件名要和里面的相同哦,即哈夫曼
💻 TXT
字号:
#include <iostream> 
#include <string> 
using namespace std; 
int a[100]; // 用来存放编码的数组 
int maxsize(0); // 用来记录编码的长度 
static int count; // 用来记录字符的长度 
int number(0); //存放字符串的长度 
class node // 存放字符和字符频率 
{ 
public: 
int sign; 
char ch; 
int frequence; 
node():frequence(0),sign(0){} 
node(char x,int f=0){ch=x;frequence=f;} 
operator =(node n){ch=n.ch;frequence=n.frequence;} 
}; 


int len(char ch[])// 用来求字符串的长度 
{ 
int i(0),num(0); 
while(ch[i]!=NULL) 
{ 
num++; 
i++; 
} 
number=num; 
return num; 
} 
//=------------------------------------------- 

//--------------------------------------------------以下是编码类------------------------------------- 

//-------------------------------------------------- 
class hufftree; 

class huffnode 
{friend class hufftree; 
public: 
huffnode *next,*leftchild,*rightchild,*parent; 
int code[100]; 
int data; 
char ch; 
huffnode(huffnode * pnext=NULL,huffnode*left=NULL,huffnode*right=NULL) 
{next=pnext;leftchild=left;rightchild=right;} 


huffnode(int item,char a,huffnode *pnext=NULL,huffnode*left=NULL,huffnode*right=NULL) 
{data=item;next=pnext;ch=a;leftchild=left;rightchild=right;} 
}; 





class hufftree 
{ 

public: 
node array[100]; 
huffnode *head,*listhead,*huffroot; 


hufftree() 
{huffnode *newnode=new huffnode(); 

head=newnode; 
head->next=NULL;} 



void Insert(int x,char a)//===--------------------------插入函数------------------------- 
{ 
huffnode *p,*pre; 
p=head->next; 
if(p==NULL) 
{ 
huffnode *newnode=new huffnode(x,a,NULL,NULL,NULL); 
head->next=newnode; 
huffroot=newnode; 

} 
else 
{ 
huffnode *newnode=new huffnode(x,a,NULL,NULL,NULL); 
while(p) 
{ 
pre=p; 
p=p->next; 
} 
pre->next=newnode; 
huffroot=newnode; 
} 

} 



void connect(huffnode *p) // 将所有的HUFFMAN树的叶子结点链成一个单链表 
{ 
huffnode *t=head,*pre; 
while(t) 
{ 
pre=t; 
t=t->next; 
} 
pre->next=p; 
p->next=NULL; 
} 


huffnode * Min()/// -------------------------------求频率最小的那个接点 
{ 
if(head->next==NULL){cout<<"No element!.."<<endl;} 
else 

{ 

int min(1000); 
huffnode *p=head->next; 
huffnode *t; 
while(p!=NULL) 
{ 
if(min>p->data) 
{ 
min=p->data; 
t=p; 

} 
p=p->next; 
} 


delet(t); 

return t; 
} 
} 


void Frequence(char line[])// 求每个字符的频率 
{ 
char ch[100]; 
for(int x=0;x<len(ch);x++) 
{ 
ch[x]=line[x]; 
} 
int j(0),length(0); 
int i,k; 

int size=len(ch); 

for(k=0;k<size;k++) 
{ 
if(ch[k]!=NULL) 
{ 
array[j].ch=ch[k]; 
for( i=0;i<size;i++) 
{ 
if(ch[i]!=NULL) 
{ 
if(array[j].ch==ch[i]) 
{ 
++array[j].frequence; 
ch[i]=NULL; 
} 
} 
} 
j++; 
} 
} 
length=j; 
count=length; 
cout<<"length="<<length<<endl; 
for(i=0;i<length;i++) 
{ 
cout<<"The"<<i<<"th char is "<<array[i].ch<<" The frequence is "<<array[i].frequence<<endl; 

} 

} 
void Sethufftree()//--------------------------------造树problem---------------------------------------- 
{ 
huffnode* pt=head->next; 
while(pt) 
{ 
huffnode *p=Min(); 
huffnode *t=Min(); 
if(t==NULL) 
{ 
Insert((p->data),NULL); 
} 

else 
{ 
Insert((p->data+t->data),NULL); 
} 
huffroot->leftchild=p; 
huffroot->rightchild=t; 
p->parent=huffroot; 
if(t!=NULL) 
{ 
t->parent=huffroot; 
} 
pt=head->next->next; 
if(pt==NULL) 
{ 
huffroot->parent=pt; 
head->next=NULL; 
} 
} 
} 


void Inorder(huffnode *r)//---------------------------huffmantree 的中序遍历------------------- 
{ 


if(r!=NULL) 
{ 
Inorder(r->leftchild); 
cout<<r->data; 
if(r->leftchild==NULL && r->rightchild==NULL) 
{ 


connect(r); 

cout<<r->ch; 
} 
cout<<endl; 
Inorder(r->rightchild); 
} 
} 

void output()//-------------------------------------输出函数----------------------------------- 
{ 
huffnode *p=head->next;//-----------------------------有头接点-------------------------------------- 
while(p) 
{ 
cout<<p->ch<<" "<<p->data<<endl; 
p=p->next; 
} 
} 


void delet(huffnode * p) //-----------------------这里只断开指针,并没有真正的删除------------- 
{ 
huffnode *t=head->next,*pre=head; 
if(t==NULL){cout<<"All of the elements have been deleted!.."<<endl;exit(1);} 
else 
{ 
while(t!=p) 
{ 
pre=t; 

t=t->next; 

} 
pre->next=p->next; 
} 
} 



void huffcode() // 构造huffman编码 
{ 
huffnode *p=head->next,*t; 

while(p!=NULL) 
{ int i=99; 
t=p; 

while(t->parent!=NULL) 
{ 
if(t->parent->leftchild==t) 
{ 
p->code[i]=0; 
i--; 
} 
else 
{ 
p->code[i]=1; 
i--; 
} 
t=t->parent; 
} 
p=p->next; 

} 

} 

void outputcode()//-----------------------------------------------输出编码-------------------- 
{ 
huffnode *p=head->next; 
int j(0); 
while(p) 
{ 
cout<<"\nThe char "<<p->ch<<" 's huffman code is "; 
for(int i=0;i<100;i++) 
{ 
if(p->code[i]==0 || p->code[i]==1) 
{ 
cout<<p->code[i]; 
a[j]=p->code[i]; 
j++; 
} 

} 

p=p->next; 
} 
maxsize=j; 
} 


void sortcode() 
{ 
for(int m=0;m<count;m++) 
{ 
huffnode *p=head->next; 
while(p) 
{ 

if(array[m].ch==p->ch) 
{ 
cout<<"\nThe char "<<p->ch<<" 's huffman code is "; 
for(int i=0;i<100;i++) 
{ 
if(p->code[i]==0 || p->code[i]==1) 
{ 
cout<<p->code[i]; 
} 
} 
} 
p=p->next; 
} 
} 
} 

void Incode(char cha[]) 
{ 
int j(0); 
cout<<"NUmber "<<len(cha)<<endl; 
for(int k=0;k<number;k++) 
{cout<<cha[k];} 



for(int m=0;m<number;m++) 
{ 
huffnode *p=head->next; 
while(p) 
{ 

if(cha[m]==p->ch) 
{ 
cout<<"\nThe char "<<p->ch<<" 's huffman code is "; 
for(int i=0;i<100;i++) 
{ 
if(p->code[i]==0 || p->code[i]==1) 
{ 
cout<<p->code[i]; 
a[j]=p->code[i]; 
j++; 
} 
} 
} 
p=p->next; 
} 
} 
maxsize=j; 

} 
void transcode() // 进行译码 
{ 
huffnode *p=huffroot; 
int i(0); 
cout<<"\nAfter translating, the code is: "; 
while(p) 
{ 
if(a[i]==0) 
{ 
p=p->leftchild; 
} 
else 
{ 
p=p->rightchild; 
} 
if(p->leftchild==NULL && p->rightchild==NULL) 
{ 
cout<<p->ch; 
p=huffroot; 

} 
i++; 
if(i>=maxsize)break; 
} 
} 


}; 








void main() //主函数 
{ 
char ch[100]; 
cout<<"Input the char "; 
cin.getline(ch,sizeof(ch)); 
hufftree list ; 
list.Frequence(ch); 
for( int i=0;i<count;i++) 
{ 

list.Insert(list.array[i].frequence,list.array[i].ch); 
} 


list.output(); 

cout<<"Finish output!...."<<endl; 

list.Sethufftree(); 
list.Inorder(list.huffroot); 

cout<<"This is the result of after connecting"<<endl; 
list.output(); 
list.huffcode(); 
list.sortcode(); 
//cin.getline(ch,sizeof(ch)); 
list.Incode(ch); 
list.transcode(); 
cout<<endl; 
for(int j=0;j<=maxsize;j++) 
cout<<a[j]; 

cout<<endl; 

cout<<"There are "<<__LINE__<<" lines in this programm"<<endl; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -