1、1/Queen.h#includeusing namespace std;template class Queuepublic:Queue(int size)maxsize=size;q=new Tmaxsize;front=rear=0;virtual bool IsEmpty()constreturn front=rear;bool Front(T x=q(front+1)%maxsize;return true;virtual bool IsFull()constreturn (rear+1)%maxsize=front;virtual bool EnQueue(T x)if(IsFul
2、l()return false;q(rear=(rear+1)%maxsize)=x;return true;bool DeQueue()int a;if(IsEmpty()return false;a=front;front=(front+1)%maxsize;return true;void Clear() front=rear=0;int front,rear;int maxsize;T *q;/BinaryTree.h#include“Queue.h“templatestruct BTNodeBTNode()lchild=rchild=NULL;parent=NULL;BTNode(c
3、onst Tlchild=rchild=NULL;parent=NULL;letter=le;BTNode(const Tlchild=l;rchild=r;parent=NULL;letter=le;BTNode(const Tlchild=l;rchild=r;parent=p;letter=le;/*BTNode* lchild=a-lchild;rchild=a-rchild;parent=a-parent;*/T element;char letter;BTNode* lchild,*rchild;BTNode* parent;templateclass BinaryTree2pub
4、lic:BinaryTree()root=NULL;BinaryTree()/if(root)/ clear(Visit);bool IsEmpty()constif(root=NULL)return true;return false;int Size() return Size(root);void clear(T /*BTNode* copy()copy(root);return root;*/bool Root(T return true;elsereturn false;void MakeTree(const T BTNode* a=new BTNode;if(root|root=n
5、ew BTNode(x,b,left.root,right.root);left.root=NULL;right.root=NULL;/root-parent=root;if(root)q.EnQueue(root);while(!(q.IsEmpty()q.DeQueue();if(a-lchild)a-lchild-parent=a;q.EnQueue(a-lchild);if(a-rchild)a-rchild-parent=a;q.EnQueue(a-rchild);void BreakTree(T x=root-element;left.root=root-lchild;right.
6、root=root-rchild; void PreOrder() /先序遍历的递归PreOrder(root);void InOrder() /中序遍历的递归InOrder(root);BTNode* root;private:int Size(BTNode *t)3if(!t)return 0;elsereturn Size(t-lchild)+Size(t-rchild)+1;void clear(BTNode* if(r-rchild) clear(t-rchild);t-lchild=t-rchild=NULL;delete t;t=NULL;void PreOrder(BTNode
7、* t)if(t)Visit(t);PreOrder(t-lchild);PreOrder(t-rchild);void InOrder(BTNode* t)if(t) InOrder(t-lchild);Visit(t);InOrder(t-rchild);template void Visit(BTNode *t)coutelementusing namespace std;template class PrioQueuepublic:PrioQueue(int msize)maxsize=msize;n=0;q=new Tmaxsize;PrioQueue()delete q;bool
8、IsEmpty()return n=0;bool IsFull()return n=maxsize;void Append(const T if(tempint m=0;template class HfmTree:public BinaryTreepublic:operator T()constreturn weight;T getW()return weight;void putW(const Tchar getl()return let;void putl(const charvoid setNull()root=NULL;HfmTree()root=NULL;HfmTree Creat
9、eHfmTree(T w,char a,int n) /创建哈夫曼树PrioQueue pq(n);HfmTree x,y,z;for(int i=0;i* t,int a,int b) /产生编码 int i=0;ofstream ofile(“f:codefile.txt“,ios:app);if(!ofile)coutparent)if(t-parent-lchild=t) /比较 t 的地址,若 t 即指向双亲,又指向左孩子,则在 t 上的字符编码为 0ai=0;else /反之为 1ai=1;i+;t=t-parent;for(int j=0;j *t;t=root;char ch,
10、q;int i=0;ifstream ifile(“f:codefile.txt“);if(!ifile)coutlchild)t=t-lchild;if(i=1)if(t-rchild)t=t-rchild;if(!(t-lchild)xfile.put(q);t=root;xfile.close();ifile.close();private:T weight;char let;6;templateBTNode* find(char a,BTNode* b,BTNode* if(b)find(a,b-lchild,c);find(a,b-rchild,c);if(b-lchild)if(b
11、-rchild)return c;void bianma(char *a,BTNode *b,int n,HfmTree d) /编整个字符串的码 BTNode *c;int e100;for(int i=0;in;return n;HfmTree creat(char *c,int *d,int n) coutci;coutdi;HfmTree a,b;b=a.CreateHfmTree(d,c,n);return b;void main() int n,i=0;char e;HfmTree d;n=input();char *a=new charn;int *b=new intn;char
12、 f100;coute;switch(e)case B: d=creat(a,b,n);break;case T:coutfi;ofile *g;int h100;for(i=0;im;i+)g=find(fi,d.root,g);pile(g,h,1);coutendl;break;case D:d.translation();break;case P:char ch;ifstream ifs(“f:textfile.txt“);ifstream io(“f:codefile.txt“);ifstream is(“f:result.txt“);cout“输入的字符串为:“endl;while(ifs.get(ch)coutch;coutendl;cout“它的编码为:“endl;while(io.get(ch)coutch;coutendl;cout“译码为:“endl;while(is.get(ch)coutch;coutendl;break;ifs.close();io.close();is.close();case X:break;cout“请继续选择:“endl;while(e!=X);8