




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验题二:哈夫曼编码和译码系统(1)所设计的系统重复显示以下菜单项:B建树:读入字符集和各字符频度,建立哈夫曼树。T遍历:先序和中序遍历二叉树。E生成编码:根据已建成的哈夫曼树,产生各字符的哈夫曼编码。C编码:输入由字符集中字符组成的任意字符串,利用已生成的哈夫曼编码进行编码,显示编码结果,并将输入的字符串及其编码结果分别保存在磁盘文件textfile.txt和codefile.txt中。D译码:读入codefile.txt,利用已建成的哈夫曼树进行译码,并将译码结果存入磁盘文件result.txt中。P打印:屏幕显示文件textfile.txt、codefile.txt和result.txt
2、。X退出。源代码#include #include #include #include #include using namespace std;int *weightArray;string s;string *codeArray;template struct BTNode T element;BTNode *lChild, *rChild; BTNode( lChild = rChild = NULL; BTNode(const T& x element = x;lChild = rChild = NULL; BTNode(const T& x, BTNode * l,
3、BTNode * r element = x;lChild = l;rChild = r;实 验 报 告template class BinaryTree public:BinaryTree( root = NULL;BinaryTree( bool isEmpty( const return root = NULL;void clear( postClear(root;bool retRoot(T& x const;void makeTree(const& x, BinaryTree & left, BinaryTree & right; void break
4、Tree(T& x, BinaryTree & left, BinaryTree & right; void preOrder( preOrder(root;void inOrder( inOrder(root;void postOrder( postOrder(root;BTNode * copy(BTNode *t; int size( return size(root;void change( change(root;void breathFirst( breathFirst(root;int height( return height(root;void lea
5、f( prePrint(root;实 验 报 告protected:BTNode * root; private:void clear(BTNode * t; void change(BTNode * t; void postClear(BTNode * t; void prePrint(BTNode * t; int size(BTNode * t; int height(BTNode * t; void preOrder(BTNode * t; void inOrder(BTNode * t; void postOrder(BTNode * t; void breathFirst(BTNo
6、de * t; void visit(T& x cout << x << " "template bool BinaryTree :retRoot(T& x const if (root x = root -> element;return true;else return false;template void BinaryTree :makeTree(const& x, BinaryTree & left, BinaryTree & right if (root | &left = &
7、right return;root = new BTNode (x, left.root, right.root; left.root = right.root = NULL;template void BinaryTree :breakTree(T& x, BinaryTree & left, BinaryTree & right if (!root | &left = &right | left.root | right.root return;x = root -> element;left.root = root -> lChild;
8、right.root = root -> rChild;delete root;root = NULL;实 验 报 告template void BinaryTree :preOrder(BTNode * t if (t visit(t -> element;preOrder(t -> lChild;preOrder(t -> rChild;template void BinaryTree :inOrder(BTNode * t if (t inOrder(t -> lChild;visit(t -> element;inOrder(t -> rChi
9、ld;template void BinaryTree :postOrder(BTNode * t if (t postOrder(t -> lChild;postOrder(t -> rChild;visit(t -> element;template void BinaryTree :clear(BTNode * t delete t;t = NULL;template void BinaryTree :postClear(BTNode * t if (t postClear(t -> lChild;postClear(t -> rChild;delete t
10、;实 验 报 告template BTNode * BinaryTree :copy(BTNode *t if (!t return NULL;BTNode * q = new BTNode (t ->element; q -> lChild = copy(t -> lChild;q -> rChild = copy(t -> rChild;return q;template int BinaryTree :size(BTNode * t if (!t return 0;else return size(t -> lChild + size(t ->
11、rChild;template void BinaryTree :change(BTNode * t if (!t return;BTNode *q = copy(t -> rChild; clear(t -> rChild;t -> rChild = t -> lChild;t -> lChild = q;change(t -> lChild;change(t -> rChild;template void BinaryTree :breathFirst(BTNode * t if (!t return;queue *> q1; q1.push
12、(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 int BinaryTree :height(BTNode * t 实 验 报 告 if(t = NULL return 0; else int m = height( t -> lChild ; int n = h
13、eight(t -> rChild; return (m > n ? (m + 1 : (n + 1; template void BinaryTree :prePrint(BTNode * t if (t if (t -> lChild = NULL && (t -> rChild = NULL visit(t -> element;return;prePrint(t -> lChild;prePrint(t -> rChild;template class PrioQueue public:PrioQueue(int mSize=2
14、0;PrioQueue(delete q;bool IsEmpty( constreturn n=0;bool IsFull( constreturn n=maxSize;void Append(const T &x;void Serve(T& x;private:void AdjustDown (int r, int j;void AdjustUp (int j;T* q;int n,maxSize;template PrioQueue :PrioQueue(int mSize maxSize=mSize;n=0;实 验 报 告q=new TmaxSize;template
15、void PrioQueue :AdjustUp (int j int i=j;T temp=qi;while (i>0 && temp qi=q(i-1/2; i=(i-1/2;qi=temp;template void PrioQueue :Append(const T &x if(IsFull( cout<< "Overflow"return;qn+=x;AdjustUp(n-1;template void PrioQueue :Serve(T& x if(IsEmpty(cout<< "Unde
16、rflow"return;x=q0;q0=q-n;AdjustDown (0, n-1;template void PrioQueue :AdjustDown (int r, int j int child = 2 * r + 1;T temp = qr;while (child <= j if (child < j && (qchild > qchild + 1 child+;if (temp <= qchild break;q(child - 1 / 2 = qchild;child = 2 * child + 1;实 验 报 告q(chi
17、ld - 1 / 2 = temp;template class HfmTree: public BinaryTree public:operator T(const return weight;T getW(return weight;void putW(const T& x weight=x; void SetNull(root=NULL; void code(string& c code(root, c;void decode(string s;private:T weight;void code(BTNode * t, string& c; ;template
18、void HfmTree :decode(string decodeString if (codeArray = NULL cout << "尚未编码!" << endl;return;BTNode * searchNode = root; for (int i = 0; i < decodeString.length(; i+ if (decodeStringi != '0' && decodeStringi != '1' cout << "所给码格式不正确!"
19、<< endl;return;if (searchNode -> lChild = NULL && searchNode -> rChild = NULL T value = searchNode -> element;for (int j = 0; j < s.length(; j+ if (value = weightArrayj cout << sj;break;实 验 报 告searchNode = root;if (decodeStringi = '0' searchNode = searchNode -
20、> lChild;if (decodeStringi = '1' searchNode = searchNode -> rChild;if (searchNode -> lChild = NULL && searchNode -> rChild = NULL T value = searchNode -> element;for (int j = 0; j < s.length(; j+ if (value = weightArrayj cout << sj;break; cout << endl;te
21、mplate void HfmTree :code(BTNode * t, string& c if (t if (t -> lChild = NULL && t -> rChild = NULL for (int i = 0; i < s.length(; i+ if (t -> element = weightArrayi codeArrayi = c;/cout << "NO" << i << " "cout << "字符" <
22、< si << "的权重是" << weightArrayi << ", 哈弗曼编码是" << codeArrayi << endl; break;if (t -> lChild != NULL string ls;ls.assign(c;ls.append("0"code(t -> lChild, ls;if (t -> rChild != NULL 实 验 报 告string rs;rs.assign(c;rs.append("1&quo
23、t;code(t -> rChild, rs;template HfmTree CreateHfmTree (T *w,int n PrioQueue > pq(n; / 空优先权队列HfmTree x,y,z; / 空哈夫曼树for (int i=0;i 构造 n 棵只有一个结点的哈夫曼树 z.makeTree(wi,x,y; z.putW(wi; pq.Append(z; z.SetNull(; for (i=1;i pq.Serve(x; /取出最小权值的哈夫曼树对象xpq.Serve(y; /取出最小权值的哈夫曼树对象y z.makeTree(x.getW(+y.getW(
24、,x,y; z.putW(x.getW(+y.getW(;pq.Append(z; z.SetNull(; pq.Serve(z; return z; void input(HfmTree & p cout << "请输入需要编码的字符组成的字符串: "cin >> s;weightArray = new ints.length(;codeArray = new strings.length(;for (int i = 0; i < s.length(; i+ cout << "请输入第" <<
25、; (i + 1 << "个字符的权值:" << endl;cin >> weightArrayi;p = CreateHfmTree(weightArray, s.length(;/p.postOrder(; 实 验 报 告void createCode(HfmTree & p if (codeArray = NULL cout << "树为空!" << endl;return;string c;p.code(c;void encode( if (codeArray = NULL co
26、ut << "尚未编码!" << endl;return;string encodeString;cout << "请输入需要编码的字符串: "cin >> encodeString;cout << "n经过编码的码值为:"for (int i = 0; i < encodeString.length(; i+ for (int j = 0; j < s.length(; j+ if (sj = encodeStringj cout << codeAr
27、rayj;break;cout << endl;void main( bool flag = true;HfmTree p; string decodeString;while (flag cout << "B建树" << " T遍历" << " E生成编码" << endl;cout << "C编码" << " D译码" << " X退出" << endl;cout << "请输入指令:"char c;cin >> c;cout << endl;switch (c case 'B':实 验 报 告input(p;break;case 'T'
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 把字句微课程设计方案
- 缺铁性贫血的内科护理学
- 七年级美术上册课件大纲
- 小学今冬明春防火安全教育
- 教育心理学教师考编
- 职工旅游合同协议
- 屋面光伏设计合同协议
- 足疗劳务合同协议
- 垃圾车融资租赁合同协议
- 糖尿病足自我护理指南
- (正式版)JTT 421-2024 港口固定式起重机安全要求
- 【中国信科-中信科移动】2023星地融合通信白皮书
- 脑电图判读异常脑电图
- 人体所需的七大营养素(卓越)
- 《小学生预防溺水安全教育班会》课件
- 传统园林技艺智慧树知到期末考试答案2024年
- 直播中的礼仪与形象塑造
- 2024年八年级数学下册期中检测卷【含答案】
- 老年人中医健康知识讲座总结
- 海南声茂羊和禽类半自动屠宰场项目环评报告
- 《民法典》合同编通则及司法解释培训课件
评论
0/150
提交评论