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