南邮哈夫曼编码和译码系统.docx
《南邮哈夫曼编码和译码系统.docx》由会员分享,可在线阅读,更多相关《南邮哈夫曼编码和译码系统.docx(22页珍藏版)》请在冰豆网上搜索。
![南邮哈夫曼编码和译码系统.docx](https://file1.bdocx.com/fileroot1/2022-11/23/99805565-aa44-42f7-9efc-182cfdf647d3/99805565-aa44-42f7-9efc-182cfdf647d31.gif)
南邮哈夫曼编码和译码系统
实验题二:
哈夫曼编码和译码系统
(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;
BTNodeBTNode(){
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&left,BinaryTree〈T〉&right);
voidbreakTree(T&x,BinaryTree〈T>&left,BinaryTree&right);
voidpreOrder(){
preOrder(root);
}
voidinOrder(){
inOrder(root);
}
voidpostOrder(){
postOrder(root);
}
BTNode〈T>*copy(BTNode*t);
intsize(){
returnsize(root);
}
voidchange(){
change(root);
}
voidbreathFirst(){
breathFirst(root);
}
intheight(){
returnheight(root);
}
voidleaf(){
prePrint(root);
}
实验报告
protected:
BTNode〈T〉*root;
private:
voidclear(BTNodevoidchange(BTNode*t);
voidpostClear(BTNodevoidprePrint(BTNode*t);
intsize(BTNode〈T〉*t);
intheight(BTNode〈T>*t);
voidpreOrder(BTNode*t);
voidinOrder(BTNode〈T〉*t);
voidpostOrder(BTNode〈T>*t);
voidbreathFirst(BTNode*t);
voidvisit(T&x){
cout<〈x〈〈”";
}
};
template〈classT>
boolBinaryTree:
:
retRoot(T&x)const{
if(root){
x=root-〉element;
returntrue;
}elsereturnfalse;
}
templatevoidBinaryTree〈T〉:
:
makeTree(const&x,BinaryTree〈T〉&left,BinaryTree&right){
if(root||&left==&right)return;
root=newBTNode(x,left。
root,right.root);
left.root=right.root=NULL;
}
templatevoidBinaryTree:
:
breakTree(T&x,BinaryTree&left,BinaryTree〈T〉&right){
if(!
root||&left==&right||left.root||right。
root)return;
x=root-〉element;
left.root=root-〉lChild;
right。
root=root—〉rChild;
deleteroot;
root=NULL;
}
实验报告
}
templatevoidBinaryTree〈T〉:
:
preOrder(BTNode*t){
if(t){
visit(t—〉element);
preOrder(t-〉lChild);
preOrder(t—>rChild);
}
}
templatevoidBinaryTree:
:
inOrder(BTNodeif(t){
inOrder(t-〉lChild);
visit(t—〉element);
inOrder(t—>rChild);
}
}
templatevoidBinaryTree:
:
postOrder(BTNodeif(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*BinaryTree〈T>:
:
copy(BTNode〈T〉*t){
if(!
t)returnNULL;
BTNode*q=newBTNode(t-〉element);
q-〉lChild=copy(t—>lChild);
q—〉rChild=copy(t—>rChild);
returnq;
}
templateintBinaryTree〈T〉:
:
size(BTNodeif(!
t)return0;
elsereturnsize(t—>lChild)+size(t-〉rChild);
}
template〈classT〉
voidBinaryTree〈T>:
:
change(BTNodeif(!
t)return;
BTNoderChild);
clear(t->rChild);
t-〉rChild=t-〉lChild;
t—>lChild=q;
change(t—>lChild);
change(t—〉rChild);
}
template〈classT〉
voidBinaryTree〈T>:
:
breathFirst(BTNodeif(!
t)return;
queueq1;
q1.push(t);
BTNode*node;
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*t,string&c);
};
template
voidHfmTree:
:
decode(stringdecodeString){
if(codeArray==NULL){
cout〈<”尚未编码!
”<〈endl;
return;
}
BTNodefor(inti=0;ilength();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;
}
templatevoidHfmTree〈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);
}
}
}
templateHfmTreeCreateHfmTree(T*w,intn){
PrioQueue〈HfmTree〈T>>pq(n);//空优先权队列
HfmTree〈T〉x,y,z;//空哈夫曼树
for(inti=0;iz。
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(HfmTreecout〈<"请输入需要编码的字符组成的字符串:
";
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(HfmTreeif(codeArray==NULL){
cout〈〈”树为空!
"<return;
}
stringc;
p.code(c);
}
voidencode(){
if(codeArray==NULL){
cout<〈"尚未编码!
"〈〈endl;
return;
}
stringencodeString;
cout〈〈”请输入需要编码的字符串:
”;
cin>〉encodeString;
cout<〈"\n经过编码的码值为:
";
for(inti=0;ifor(intj=0;j〈s.length();j++){
if(s[j]==encodeString[j]){
cout<〈codeArray[j];
break;
}
}
}
cout〈〈endl;
}
voidmain(){
boolflag=true;
HfmTreestringdecodeString;
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<〈"广度优先遍历: