ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:142.86KB ,
资源ID:3576044      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3576044.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(南邮哈夫曼编码和译码系统.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

南邮哈夫曼编码和译码系统.docx

1、南邮哈夫曼编码和译码系统实验题二:哈夫曼编码和译码系统(1)所设计的系统重复显示以下菜单项:B建树:读入字符集和各字符频度,建立哈夫曼树.T遍历:先序和中序遍历二叉树。E生成编码:根据已建成的哈夫曼树,产生各字符的哈夫曼编码。C编码:输入由字符集中字符组成的任意字符串,利用已生成的哈夫曼编码进行编码,显示编码结果,并将输入的字符串及其编码结果分别保存在磁盘文件textfile.txt和codefile.txt中。D译码:读入codefile.txt,利用已建成的哈夫曼树进行译码,并将译码结果存入磁盘文件result。txt中。P打印:屏幕显示文件textfile.txt、codefile。tx

2、t和result.txt。X退出。源代码include iostream#include cstdlib#include iostreaminclude using namespace std;int *weightArray;string s;string codeArray;template struct BTNode T element; BTNodeT lChild, *rChild; BTNode() lChild = rChild = NULL; BTNode(const T& x) element = x; lChild = rChild = NULL; BTNode(const

3、 T& x, BTNodeT* l, BTNodeT r) element = x; lChild = l; rChild = r;;实 验 报 告templateclass Tclass 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

4、T& right); void breakTree(T x, BinaryTreeT& left, BinaryTree& right); void preOrder() preOrder(root); void inOrder() inOrder(root); void postOrder() postOrder(root); BTNodeT copy(BTNode t); int size() return size(root); void change() change(root); void breathFirst() breathFirst(root); int height() r

5、eturn height(root); void leaf() prePrint(root); 实 验 报 告protected: BTNodeT* root; private: void clear(BTNodeT* t); void change(BTNode* t); void postClear(BTNodeT t); void prePrint(BTNode* t); int size(BTNodeT t);int height(BTNodeT t); void preOrder(BTNode t); void inOrder(BTNodeT t); void postOrder(B

6、TNodeT t); void breathFirst(BTNode t); void visit(T x) cout bool BinaryTree::retRoot(T& x) const if (root) x = root - element; return true; else return false;template class T void BinaryTreeT:makeTree(const x, BinaryTreeT left, BinaryTree right) if (root | left = right) return; root = new BTNode(x,

7、left。root, right.root); left.root = right.root = NULL;template class T void BinaryTree::breakTree(T& x, BinaryTree& left, BinaryTreeT& right) if (!root left = right left.root | right。root) return; x = root - element; left.root = root - lChild; right。root = root rChild; delete root; root = NULL; 实 验

8、报 告template class Tvoid BinaryTreeT:preOrder(BTNode t) if (t) visit(t element); preOrder(t - lChild); preOrder(t rChild); template class Tvoid BinaryTree::inOrder(BTNode rChild); template class Tvoid BinaryTree::postOrder(BTNode lChild); postOrder(t - rChild); visit(t element); template class Tvoid

9、BinaryTree::clear(BTNodeT t) delete t; t = NULL;template class Tvoid BinaryTreeT:postClear(BTNodeT t) if (t) postClear(t lChild); postClear(t rChild); delete t; 实 验 报 告template BTNode* BinaryTreeT:copy(BTNodeT *t) if (!t) return NULL; BTNode q = new BTNode(t -element); q - lChild = copy(t lChild); q

10、 rChild = copy(t rChild); return q;template class Tint BinaryTreeT::size(BTNode lChild) + size(t - rChild);template class Tvoid BinaryTreeT:change(BTNodeT t) if (!t) return; BTNode rChild); clear(t - rChild); t - rChild = t - lChild; t lChild = q;change(t lChild); change(t rChild);template class Tvo

11、id BinaryTreeT::breathFirst(BTNodeT t) if (!t) return; queue q1; 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 class Tint BinaryTree lChild ); int n = height(t rCh

12、ild); return (m n) ? (m + 1) : (n + 1); template class Tvoid BinaryTreeT:prePrint(BTNodeT* t) if (t) if (t lChild = NULL) & (t - rChild = NULL) visit(t element); return; prePrint(t lChild); prePrint(t rChild); templateclass Tclass PrioQueue public: PrioQueue(int mSize=20);PrioQueue()delete q;; bool

13、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 class Tvoid PrioQue

14、ueT:AdjustUp (int j) int i=j;T temp=qi; while (i0 & tempq(i1)/2) qi=q(i1)/2; i=(i-1)/2; qi=temp;template void PrioQueueT::Append(const T x) if(IsFull()) coutvoid PrioQueueT::Serve(T x) if(IsEmpty()) cout ”Underflow”; return; x=q0;q0=qn; AdjustDown (0, n-1);template void PrioQueue::AdjustDown (int r,

15、 int j) int child = 2 * r + 1; T temp = qr; while (child 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(BT

16、Node* t, string c);template void HfmTree::decode(string decodeString) if (codeArray = NULL) cout ”尚未编码!” endl; return; BTNodeT searchNode = root; for (int i = 0; i decodeString。length(); i+) if (decodeStringi != 0 decodeStringi != 1) cout 所给码格式不正确! rChild = NULL) T value = searchNode element; for (i

17、nt j = 0; j s.length(); j+) if (value = weightArrayj) cout sj; break; 实 验 报 告searchNode = root; if (decodeStringi = 0) searchNode = searchNode lChild; if (decodeStringi = 1) searchNode = searchNode rChild; if (searchNode lChild = NULL & searchNode rChild = NULL) T value = searchNode - element; for (

18、int j = 0; j s。length(); j+) if (value = weightArrayj) cout sj; break; cout endl;template 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 字符 si ”的权重是” weightArrayi , 哈弗曼编码是” codeArrayi

19、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”); code(t - rChild, rs); template class THfmTree CreateHfmTree (T *w,int n) PrioQueue HfmTreeT pq(n); / 空优先权队列 HfmTreeT x,y,z; / 空哈夫曼树 for (int i=0;in;i+)

20、/构造n棵只有一个结点的哈夫曼树 z。makeTree(wi,x,y); z.putW(wi); pq.Append(z); z。SetNull(); for (i=1;in;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); return z; void input(HfmTreeint p) cout s; weigh

21、tArray = new ints。length(); codeArray = new strings。length(); for (int i = 0; i s。length(); i+) cout ”请输入第” (i + 1) 个字符的权值:” weightArrayi; p = CreateHfmTree(weightArray, s.length(); /p。postOrder(); 实 验 报 告void createCode(HfmTreeint p) if (codeArray = NULL) cout ”树为空! endl;return; string c; p.code(c)

22、;void encode() if (codeArray = NULL) cout encodeString; cout n经过编码的码值为:; for (int i = 0; i encodeString.length(); i+) for (int j = 0; j s.length(); j+) if (sj = encodeStringj) cout codeArrayj; break; cout endl;void main() bool flag = true; HfmTreeint 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: if (p != NULL) cout ”前序遍历:; p。preOrder(); cout endl;cout ”中序遍历:; p。inOrder(); cout endl; cout ”后序遍历:; p。postOrder(); cout endl; cout 广度优先遍历:

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1