南邮哈夫曼编码和译码系统.docx
- 文档编号:3576044
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:22
- 大小:142.86KB
南邮哈夫曼编码和译码系统.docx
《南邮哈夫曼编码和译码系统.docx》由会员分享,可在线阅读,更多相关《南邮哈夫曼编码和译码系统.docx(22页珍藏版)》请在冰豆网上搜索。
南邮哈夫曼编码和译码系统
实验题二:
哈夫曼编码和译码系统
(1)所设计的系统重复显示以下菜单项:
B―――建树:
读入字符集和各字符频度,建立哈夫曼树.
T―――遍历:
先序和中序遍历二叉树。
E―――生成编码:
根据已建成的哈夫曼树,产生各字符的哈夫曼编码。
C―――编码:
输入由字符集中字符组成的任意字符串,利用已生成的哈夫曼编码进行编码,显示编码结果,并将输入的字符串及其编码结果分别保存在磁盘文件textfile.txt和codefile.txt中。
D―――译码:
读入codefile.txt,利用已建成的哈夫曼树进行译码,并将译码结果存入磁盘文件result。
txt中。
P―――打印:
屏幕显示文件textfile.txt、codefile。
txt和result.txt。
X―――退出。
源代码
#include〈iostream〉
#include〈cstdlib〉
#include〈iostream〉
#include #include〈string> usingnamespacestd; int*weightArray; strings; string*codeArray; template structBTNode{ Telement; BTNode BTNode(){ lChild=rChild=NULL; } BTNode(constT&x){ element=x; lChild=rChild=NULL; } BTNode(constT&x,BTNode〈T〉*l,BTNode〈T〉*r){ element=x; lChild=l; rChild=r; } }; 实验报告 template〈classT〉 classBinaryTree{ public: BinaryTree(){ root=NULL; } ~BinaryTree(){} boolisEmpty()const{ returnroot==NULL; } voidclear(){ postClear(root); } boolretRoot(T&x)const; voidmakeTree(const&x,BinaryTree voidbreakTree(T&x,BinaryTree〈T>&left,BinaryTree voidpreOrder(){ preOrder(root); } voidinOrder(){ inOrder(root); } voidpostOrder(){ postOrder(root); } BTNode〈T>*copy(BTNode intsize(){ returnsize(root); } voidchange(){ change(root); } voidbreathFirst(){ breathFirst(root); } intheight(){ returnheight(root); } voidleaf(){ prePrint(root); } 实验报告 protected: BTNode〈T〉*root; private: voidclear(BTNode voidchange(BTNode voidpostClear(BTNode voidprePrint(BTNode intsize(BTNode〈T〉*t); intheight(BTNode〈T>*t); voidpreOrder(BTNode voidinOrder(BTNode〈T〉*t); voidpostOrder(BTNode〈T>*t); voidbreathFirst(BTNode voidvisit(T&x){ cout<〈x〈〈”"; } }; template〈classT> boolBinaryTree : retRoot(T&x)const{ if(root){ x=root-〉element; returntrue; }elsereturnfalse; } template voidBinaryTree〈T〉: : makeTree(const&x,BinaryTree〈T〉&left,BinaryTree if(root||&left==&right)return; root=newBTNode root,right.root); left.root=right.root=NULL; } template voidBinaryTree : breakTree(T&x,BinaryTree if(! root||&left==&right||left.root||right。 root)return; x=root-〉element; left.root=root-〉lChild; right。 root=root—〉rChild; deleteroot; root=NULL; } 实验报告 } template voidBinaryTree〈T〉: : preOrder(BTNode if(t){ visit(t—〉element); preOrder(t-〉lChild); preOrder(t—>rChild); } } template voidBinaryTree : inOrder(BTNode if(t){ inOrder(t-〉lChild); visit(t—〉element); inOrder(t—>rChild); } } template voidBinaryTree : postOrder(BTNode if(t){ postOrder(t->lChild); postOrder(t->rChild); visit(t—>element); } } template〈classT〉 voidBinaryTree : clear(BTNode〈T>*t){ deletet; t=NULL; } template〈classT> voidBinaryTree〈T〉: : postClear(BTNode〈T>*t){ if(t){ postClear(t—>lChild); postClear(t—>rChild); deletet; } } 实验报告 template BTNode : copy(BTNode〈T〉*t){ if(! t)returnNULL; BTNode q-〉lChild=copy(t—>lChild); q—〉rChild=copy(t—>rChild); returnq; } template intBinaryTree〈T〉: : size(BTNode if(! t)return0; elsereturnsize(t—>lChild)+size(t-〉rChild); } template〈classT〉 voidBinaryTree〈T>: : change(BTNode if(! t)return; BTNode clear(t->rChild); t-〉rChild=t-〉lChild; t—>lChild=q; change(t—>lChild); change(t—〉rChild); } template〈classT〉 voidBinaryTree〈T>: : breathFirst(BTNode if(! t)return; queue q1.push(t); BTNode while(! q1.empty()){ node=q1。 front(); visit(node—〉element); q1.pop(); if(node—>lChild)q1。 push(node->lChild); if(node—>rChild)q1。 push(node—>rChild); } } template〈classT〉 intBinaryTree : height(BTNode〈T〉*t) 实验报告 { if(t==NULL)return0; else { intm=height(t->lChild); intn=height(t—〉rChild); return(m>n)? (m+1): (n+1); } } template〈classT〉 voidBinaryTree〈T>: : prePrint(BTNode〈T〉*t){ if(t){ if((t—>lChild==NULL)&&(t-〉rChild==NULL)){ visit(t—>element); return; } prePrint(t—〉lChild); prePrint(t—>rChild); } } template〈classT〉 classPrioQueue{ public: PrioQueue(intmSize=20); ~PrioQueue(){delete[]q;}; boolIsEmpty()const{returnn==0;} boolIsFull()const{returnn==maxSize;} voidAppend(constT&x); voidServe(T&x); private: voidAdjustDown(intr,intj); voidAdjustUp(intj); T*q; intn,maxSize; }; template PrioQueue : PrioQueue(intmSize){ maxSize=mSize; n=0; 实验报告 q=newT[maxSize]; } template〈classT〉 voidPrioQueue〈T〉: : AdjustUp(intj){ inti=j;Ttemp=q[i]; while(i〉0&&temp〈q[(i—1)/2]){ q[i]=q[(i—1)/2]; i=(i-1)/2; } q[i]=temp; } template voidPrioQueue : Append(constT&x) { if(IsFull()){ cout<〈"Overflow”; return; } q[n++]=x; AdjustUp(n-1); } template〈classT> voidPrioQueue〈T〉: : Serve(T&x) { if(IsEmpty()){ cout〈〈”Underflow”; return; } x=q[0];q[0]=q[——n]; AdjustDown(0,n-1); } template voidPrioQueue : AdjustDown(intr,intj){ intchild=2*r+1; Ttemp=q[r]; while(child<=j){ if((child〈j)&&(q[child]〉q[child+1]))child++; if(temp〈=q[child])break; q[(child-1)/2]=q[child]; child=2*child+1; } 实验报告 q[(child—1)/2]=temp; } template〈classT> classHfmTree: publicBinaryTree public: operatorT()const{returnweight;} TgetW(){returnweight;} voidputW(constT&x){ weight=x; } voidSetNull(){root=NULL;} voidcode(string&c){ code(root,c); } voiddecode(strings); private: Tweight; voidcode(BTNode }; template voidHfmTree : decode(stringdecodeString){ if(codeArray==NULL){ cout〈<”尚未编码! ”<〈endl; return; } BTNode for(inti=0;i length();i++){ if(decodeString[i]! ='0’&&decodeString[i]! =’1'){ cout<<"所给码格式不正确! "< return; } if(searchNode—〉lChild==NULL&&searchNode—>rChild==NULL){ Tvalue=searchNode—>element; for(intj=0;j〈s.length();j++){ if(value==weightArray[j]){ cout〈〈s[j]; break; } } 实验报告 searchNode=root; } if(decodeString[i]=='0')searchNode=searchNode—〉lChild; if(decodeString[i]=='1')searchNode=searchNode—〉rChild; } if(searchNode—>lChild==NULL&&searchNode—〉rChild==NULL){ Tvalue=searchNode->element; for(intj=0;j length();j++){ if(value==weightArray[j]){ cout< break; } } } cout<〈endl; } template voidHfmTree〈T〉: : code(BTNode〈T>*t,string&c){ if(t){ if(t->lChild==NULL&&t->rChild==NULL){ for(inti=0;i length();i++){ if(t-〉element==weightArray[i]){ codeArray[i]=c; //cout<〈"NO"〈〈i<〈”"; cout〈<"字符"〈 break; } } } if(t—>lChild! =NULL){ stringls; ls。 assign(c); ls。 append("0"); code(t->lChild,ls); } if(t->rChild! =NULL){ 实验报告 stringrs; rs。 assign(c); rs.append("1”); code(t->rChild,rs); } } } template HfmTree PrioQueue〈HfmTree〈T>>pq(n);//空优先权队列 HfmTree〈T〉x,y,z;//空哈夫曼树 for(inti=0;i z。 makeTree(w[i],x,y); z.putW(w[i]); pq.Append(z); z。 SetNull(); } for(i=1;i〈n;i++){ pq.Serve(x);//取出最小权值的哈夫曼树对象x pq.Serve(y);//取出最小权值的哈夫曼树对象y z。 makeTree(x。 getW()+y。 getW(),x,y); z.putW(x。 getW()+y.getW()); pq.Append(z); z。 SetNull(); } pq。 Serve(z); returnz; } voidinput(HfmTree cout〈<"请输入需要编码的字符组成的字符串: "; cin>>s; weightArray=newint[s。 length()]; codeArray=newstring[s。 length()]; for(inti=0;i〈s。 length();i++){ cout〈<”请输入第”〈〈(i+1)<<"个字符的权值: ”〈 cin〉>weightArray[i]; } p=CreateHfmTree(weightArray,s.length()); //p。 postOrder(); } 实验报告 } voidcreateCode(HfmTree if(codeArray==NULL){ cout〈〈”树为空! "< return; } stringc; p.code(c); } voidencode(){ if(codeArray==NULL){ cout<〈"尚未编码! "〈〈endl; return; } stringencodeString; cout〈〈”请输入需要编码的字符串: ”; cin>〉encodeString; cout<〈"\n经过编码的码值为: "; for(inti=0;i for(intj=0;j〈s.length();j++){ if(s[j]==encodeString[j]){ cout<〈codeArray[j]; break; } } } cout〈〈endl; } voidmain(){ boolflag=true; HfmTree stringdecodeString; while(flag){ cout〈〈"B——建树”〈〈"T—-遍历"<〈"E--生成编码"<〈endl; cout<<”C——编码"〈<"D——译码”<〈”X——退出"<〈endl; cout〈〈"请输入指令: ”; charc; cin〉〉c; cout< switch(c){ case'B': 实验报告 input(p); break; case’T’: if(p! =NULL){ cout<〈”前序遍历: "; p。 preOrder(); cout〈 cout〈<”中序遍历: "; p。 inOrder(); cout〈 cout〈〈”后序遍历: "; p。 postOrder(); cout<〈endl; cout<〈"广度优先遍历:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 南邮哈夫曼 编码 译码 系统
![提示](https://static.bdocx.com/images/bang_tan.gif)