The design of a Havermann encoding and decoding system requires the following:
b--achievements: Read into the character set and the frequency of each character, establish Huffman tree.
t--traversal: The first order and the middle sequence traverse the binary tree.
e--generated code: Based on the Huffman tree has been built, generating the Huffman code of each character.
c--Encoding: Enter any string consisting of characters in the character set, encode with the generated Huffman encoding, display the encoded result, and save the input string and its encoded results in disk files Textfile.txt and codefile.txt respectively.
d--decoding: Read into the Codefile.txt, using the established Huffman tree decoding, and the decoding results into the disk file Result.txt.
p--Print: The screen displays the file textfile.txt,codefile.txt,result.txt.
x--exit.
Tip: Modify the binary tree node class Btnode in the textbook, add a parent domain that points to the parent, and modify the function of the binary tree class Maketree set the value of the field. By traversing the Havelock Mann Tree, Production give birth to each leaf
Huffman coding of sub-nodes. When traversing a leaf node, the path from that leaf node to the root determines the encoding of the character represented by that leaf node.
Forgot to initialize the debug one night, by the way to review the file operation. The code uses the priority queue class and the two-fork tree class.
Implementation code:
#include "iostream" #include "Cstdio" #include "CString" #include "algorithm" #include "Cassert" #include "fstream" using namespace Std;template <class t>class prioqueue{public:prioqueue (int msize = 0); ~prioqueue () {delete []q;} BOOL IsEmpty () const {return n = = 0;}//Priority queue is empty return Truebool Isfull () const {return n = = maxSize;}/Priority ranks full return truevoid Ap Pend (const t& x); The priority queue adds a value of x to the element void Serve (t& x); The highest priority element in the popup queue in the priority queue and assigns the value to Xprivate:void adjustdown (int r, Int j); Adjust the void adjustup (int j) downwards; adjust void Print upward (); t* q;int N, maxsize;/* data */};template <class t>void prioqueue<t>::P rint () {for (int i = 0; i < n; ++i) cout << q[i] << "\ t"; cout << Endl;} Template <class t>prioqueue<t>::P rioqueue (int msize) {maxSize = Msize;n = 0;q = new T[maxsize];} Template <class t>void prioqueue<t>::adjustup (int j) {int i = j; T tmp = q[i];while (i > 0 && tmp < q[(i-1)/2]) {Q[i] = q[(i-1)/2];i = (i-1)/2;} Q[i] = tmp;} TemPlate <class t>void prioqueue<t>::append (const t& x) {ASSERT (! Isfull ()); q[n++] = x; Adjustup (n-1);} Template <class t>void Prioqueue<t>::serve (t& x) {x = q[0];q[0] = Q[--n]; Adjustdown (0, n-1);} Template <class t>void prioqueue<t>::adjustdown (int r, int j) {int child = 2 * R + 1; T tmp = Q[r];while (Child <= j) {if (Child < J && Q[child] > q[child + 1]) child++;if (tmp <= q[child]) b reak;q[(CHILD-1)/2] = Q[child];child = 2 * child + 1;} q[(CHILD-1)/2] = tmp;} Template <class t>struct btnode{/* data */btnode () {lchild = Rchild = NULL;} Btnode (const T &x, const char &y) {element = X;ch = Y;lchild = Rchild = parent = Null;memset (z,-1, sizeof (z));} Btnode (const t& x, const char &y, btnode<t>* L, btnode<t>* r) {element = X;ch = Y;lchild = L;rchild = R;parent = Null;memset (z,-1, sizeof (z));} T element; btnode<t>* lchild, *rchild, *parent;char ch;int val, z[100];}; Template <class T>claSS Binarytree{public:binarytree () {root = NULL; i =-1;} BOOL IsEmpty () const; To determine whether it is empty, is to return truevoid Clear (); Remove all nodes and become empty binary tree bool Root (t& x) const; If the binary tree is empty, then X is the value of the root and returns true btnode<t>* root (); int Size (); int count () {return count (root);} void Maketree (const t& x, const char &y, binarytree<t>& left, binarytree<t>& right); Constructs a binary tree, the root value is X, left & right is a subtree of void Breaktree (t& x, binarytree<t>&, binarytree<t>& right); Split two fork tree into three parts, X is the value of the root, left & right is subtree void preorder (void (*visit) (t& x)); First-order traversal of Binary tree void inorder (void (*visit) (t& x)); The middle sequence traverses the binary tree void Postorder (void (*visit) (t& x)); Sequential traversal of binary tree void Create_code (); Generate code void Create_code_out (); Output code void code (); coded void Compile (); decoding void Print (); btnode<t>* root;/* Data */private:int i;void Clear (btnode<t>* T); int Size (btnode<t> *t); Returns the number of binary tree nodes int count (btnode<t> *t); Returns a binary tree the number of nodes with only one child void preorder (VOID (*visit) (t &x), btnode<t> *t), void inorder (void (*visit) (t &x), btnode<t> *t); void Postorder (void (*visit) (T &x), btnode<t> *t); void Create_code (btnode<t> *t); void Create_code_out (btnode<t> *t); void Code (btnode<t> *t); void Compile (btnode<t> *t); void make (Btnode<t> *t, char a);}; Template <class t>void Visit (T &x) {cout << x << ' t ';} Template <class t>btnode<t>* Binarytree<t>::root () {return Root;} Template <class t>bool binarytree<t>::root (T &x) const{if (root) {x = root-Element;return true;} return false;} Template <class t>void binarytree<t>::clear (btnode<t>* t) {if (t) {Clear (T-lchild); Clear (T-rchild); cout << "Delete" << T-element << "..." << endl;delete t;}} Template <class t>void binarytree<t>::maketree (const t& x, const char &y, binarytree<t> & Left, Binarytree<t> &right) {if (root | | &left = = &right) Return;root = new Btnode<t> (x, Y, Left.root, right.root); if (left.root! = Right.root) {left.root, parent = root;right.root, parent = root;left.root, val = 0;right.root, val = 1 ;} Left.root = Right.root = NULL;} Template <class t>void Binarytree<t>::breaktree (t& x, binarytree<t>& left, BinaryTree<T >& 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;} Template <class t>void binarytree<t>::P reorder (void (*visit) (t& x)) {cout << "First order traversal:" << Endl Preorder (Visit, root); cout << Endl;} Template <class t>void binarytree<t>::P reorder (void (*visit) (t& x), btnode<t>* T) {if (t) {Visit (t Element); Preorder (Visit, T-lchild); Preorder (Visit, T, rchild);}} Template <class t>void Binarytree<t>:: Inorder (void (*visit) (t& x)) {cout << "middle order traversal is:" << Endl;inorder (Visit, root); cout << Endl;} Template <class t>void binarytree<t>::inorder (void (*visit) (t& x), btnode<t>* T) {if (t) {inorder ( Visit, T-lchild); Visit (t-Element); Inorder (Visit, T-rchild);}} Template <class t>void binarytree<t>::P ostorder (void (*visit) (t& x)) {cout << post-order traversal as: "<< Endl Postorder (Visit, root); cout << Endl;} Template <class t>void binarytree<t>::P ostorder (void (*visit) (t& x), btnode<t>* T) {if (t) { Postorder (Visit, T-lchild); Postorder (Visit, T-rchild); Visit (t-Element);}} Template <class t>int binarytree<t>::size () {return Size (root);} Template <class t>int binarytree<t>::size (btnode<t> *t) {if (!t) return 0;return Size (T-lchild) + S Ize (T-rchild) + 1;} Template <class t>int binarytree<t>::count (btnode<t> *t) {if (!t) return 0;if (((T-&Gt Lchild) && (!t-rchild)) | | (!t-Lchild) && (T-rchild)) Return 1;return count (T-lchild) + count (t-rchild);} Template <class t>class hfmtree:public binarytree<t>{public:operator T () const{return weight;} T GETW () {return weight;} void Putw (const T &x) {weight = x;} void SetNull () {binarytree<t>::root = NULL;} Private:t weight;}; Template <class t>hfmtree<t> creathfmtree (T w[], char q[], int n) {prioqueue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data Structure Experiment 2 (design Havermann coding and decoding system)