发布网友 发布时间:2022-04-30 03:38
共1个回答
热心网友 时间:2023-10-09 22:24
------------------------------------------------------------------HuffTree.h文件:/**四川大学.软件学院**文件名称:HuffTree.h*摘要:利用有序表,建立哈夫曼树**作者:高云翔*完成日期:2008.11.14*/#ifndefHUFFTREE_H#defineHUFFTREE_H//由于SAList类要用到HuffTree类,此处即为虚定义classHuffTree;//有序表类的声明classSAList{intfence;intmaxSize;intlistSize;public:SAList(intsize=0);~SAList();HuffTree**listArray;voidsetStart();voidnext();intgetLength();boolinsert(HuffTree*item);//排序插入HuffTree*remove();boollessThan(HuffTree*x,HuffTree*y);};//哈弗曼树结点的类声明classHuffTreeNode{public:unsignedcharvalue;unsignedlongintweight;unsignedintleftOrRight;//若为左儿子,则值为0,若为右儿子,则值为1HuffTreeNode*parent;HuffTreeNode*leftChild;HuffTreeNode*rightChild;boolisLeaf;};//哈夫曼树的声明classHuffTree{public:HuffTreeNode*subRoot;HuffTree();HuffTree(unsignedcharval,unsignedlongintfreq);HuffTree(HuffTree*l,HuffTree*r);HuffTree*buildHuffTree(SAList*sal,SAList*copy);};#endif--------------------------------------------------------------------HuffTree.cpp文件://哈夫曼树的实现#include#include"HuffTree.h"//有序表类的声明SAList::SAList(intsize){maxSize=size;listSize=fence=0;listArray=newHuffTree*[maxSize];}SAList::~SAList(){delete[]listArray;}voidSAList::setStart(){fence=0;}voidSAList::next(){if(fenceweightsubRoot->weight;}//哈夫曼树的实现HuffTree::HuffTree(){subRoot=newHuffTreeNode;subRoot->weight=0;}HuffTree::HuffTree(unsignedcharval,unsignedlongintfreq){subRoot=newHuffTreeNode;subRoot->value=val;subRoot->weight=freq;subRoot->isLeaf=true;}HuffTree::HuffTree(HuffTree*l,HuffTree*r){subRoot=newHuffTreeNode;subRoot->leftChild=l->subRoot;subRoot->rightChild=r->subRoot;subRoot->leftChild->parent=subRoot->rightChild->parent=subRoot;subRoot->weight=l->subRoot->weight+r->subRoot->weight;subRoot->isLeaf=false;}HuffTree*HuffTree::buildHuffTree(SAList*sal,SAList*copy){HuffTree*temp1,*temp2,*temp3;temp1=temp2=temp3=newHuffTree;for(inti=sal->getLength();i>1;i--){sal->setStart();temp1=sal->remove();temp2=sal->remove();temp1->subRoot->leftOrRight=0;temp2->subRoot->leftOrRight=1;temp3=newHuffTree(temp1,temp2);if(temp1->subRoot->isLeaf)copy->insert(temp1);if(temp2->subRoot->isLeaf)copy->insert(temp2);sal->insert(temp3);}returntemp3;}------------------------------------------------------------------HuffmanCodeing.h文件:/**四川大学.软件学院**文件名称:HuffmanCodeing.h*摘要:利用哈夫曼树,对文件内容进行哈夫曼编码与译码**作者:高云翔*完成日期:2008.11.21*/#ifndefHUFFMANCODING_H#defineHUFFMANCODING_H#include#include"HuffTree.h"usingnamespacestd;//栈结构structLink{unsignedintelement;Link*next;Link(constunsignedint&elemval,Link*nextval=NULL);Link(Link*nextval=NULL);};structLStack{Link*top;intsize;LStack(){size=0;top=NULL;}~LStack(){clear();}voidclear();boolpush(constunsignedint&item);boolpop(unsignedint&it);};//缓冲区structBuffer{unsignedcharbyte;//表示字节,每个字节由8个位组成unsignedintbits;//表示位(比特)voidclear();//清空缓存区};//临时保存叶子结点的数域structSave{unsignedcharch;unsignedlongintval;};classHuffmanCoding{private:SAList*list,*copy;//有序表HuffTree*tree;//哈夫曼树Bufferbuffer;//缓存区LStack*stack;//栈Savesave;//临时保存叶子结点的数域FILE*sourceFile;//表示源文件FILE*targetFile;//表示目标文件unsignedlonginttotal;//表示要进行编码的文件的总字节数(number#include#include"HuffmanCoding.h"usingnamespacestd;Link::Link(constunsignedint&elemval,Link*nextval){element=elemval;next=nextval;}Link::Link(Link*nextval){next=nextval;}voidLStack::clear(){while(top!=NULL){Link*temp=top;top=top->next;deletetemp;}size=0;}boolLStack::push(constunsignedint&item){top=newLink(item,top);size++;returntrue;}boolLStack::pop(unsignedint&it){if(size==0)returnfalse;it=top->element;Link*temp=top;top=top->next;size--;deletetemp;returntrue;}voidBuffer::clear(){bits=0;byte=0;}voidHuffmanCoding::code(){char*sourceFileName=newchar[];char*targetFileName=newchar[];total=0;numOfLeaf=0;cout>sourceFileName;sourceFile=fopen(sourceFileName,"rb");//判断源文件是否存在,不存在则要求用户重新输入while(sourceFile==NULL){cout>sourceFileName;sourceFile=fopen(sourceFileName,"rb");}fgetc(sourceFile);//判断文件内容是否为空,若为空则退出程序if(feof(sourceFile)){cout>targetFileName;targetFile=fopen(targetFileName,"wb");//判断目标文件是否可以建立while(targetFile==NULL){cout>targetFileName;targetFile=fopen(targetFileName,"wb");}coutbuildHuffTree(list,copy);//利用已建立的有序表,建立哈夫曼树//开始编码,并将编码后的内容输出到目标文件rewind(sourceFile);//将文件指针重新指向文件内容起始处unsignedchartempChar=fgetc(sourceFile);//取文件内容的下一个字符unsignedinttempInt;HuffTreeNode*tempTreeNode;stack=newLStack();buffer.clear();//清空缓存区//当文件内容全部被扫描完,循环结束while(!feof(sourceFile)){//搜索匹配tempChar的叶子的值for(inti=0;igetLength();i++){if(tempChar==copy->listArray[i]->subRoot->value){stack->clear();tempTreeNode=copy->listArray[i]->subRoot;while(tempTreeNode!=tree->subRoot){stack->push(tempTreeNode->leftOrRight);tempTreeNode=tempTreeNode->parent;}//endwhilewhile(stack->pop(tempInt)){write(tempInt);}break;}//endif}//endfortempChar=fgetc(sourceFile);}//endwhile//如果缓存区还有剩余的位,则添加0到缓存区,直到够满8个位建立个字符,并向目标文件写入该字符if(buffer.bits>0){for(unsignedinti=buffer.bits;i>sourceFileName;sourceFile=fopen(sourceFileName,"rb");//判断源文件是否存在,不存在则要求用户重新输入while(sourceFile==NULL){cout>sourceFileName;sourceFile=fopen(sourceFileName,"rb");}fgetc(sourceFile);//判断文件内容是否为空,若为空则退出程序if(feof(sourceFile)){cout>targetFileName;targetFile=fopen(targetFileName,"wb");//判断目标文件是否可以建立while(targetFile==NULL){cout>targetFileName;targetFile=fopen(targetFileName,"wb");}coutbuildHuffTree(list,copy);//利用已建立的有序表,建立哈夫曼树//译码开始buffer.clear();//清空缓存区unsignedinttempInt;HuffTreeNode*tempTreeNode;while(total>0){tempTreeNode=tree->subRoot;while(!tempTreeNode->isLeaf){tempInt=read();if(tempInt==0){tempTreeNode=tempTreeNode->leftChild;}else{tempTreeNode=tempTreeNode->rightChild;}}fputc(tempTreeNode->value,targetFile);total--;}cout0)numOfLeaf++;}//将源文件的字符总数及叶子结点总数保存到目标文件fwrite(&total,sizeof(unsignedlongint),1,targetFile);fwrite(&numOfLeaf,sizeof(unsignedint),1,targetFile);//建立叶子结点有序表list=newSAList(numOfLeaf);copy=newSAList(numOfLeaf);//依次扫描ch,将权值不为0的字符插入到叶子结点有序表中//并将此字符及其权值保存到目标文件for(intk=0;k0){temp=newHuffTree(k,ch[k]);list->insert(temp);save.ch=k;save.val=ch[k];fwrite(&save,sizeof(save),1,targetFile);}}}//利用建立的缓存,向目标文件中输出字符voidHuffmanCoding::write(unsignedintc){++buffer.bits;buffer.byte=(buffer.byteinsert(temp);}}//从压缩文件中读取一个比特unsignedintHuffmanCoding::read(){if(buffer.bits==0){buffer.bits=8;buffer.byte=fgetc(sourceFile);}if(buffer.byte&(1<<(buffer.bits-1))){buffer.bits--;return1;}else{buffer.bits--;return0;}}-------------------------------------------------------------------Driver.cpp文件:#include"HuffmanCoding.h"intmain(){HuffmanCodingh;h.code();h.decode();system("PAUSE");return0;}一共5个文件,文件名已经给你了,你直接拷贝就可以,绝对我自己的原创,我还没给别人看过,希望你能利用好这段代码