This is the code implementation of the Haffman tree in my undergraduate course. Design hafuman encoder and decoder
Problem description: Information Communication Using the Harman encoding can greatly improve the channel utilization, shorten the information transmission time, and reduce the transmission cost. However, this requires the sender to use an encoding system to pre-encode the transmitted data, and the receiver to decode the transmitted data. For duplex channels (channels that can transmit information in two directions), each end requires a complete encoding/decoding system. Try to compile a coding/decoding system for such an information receiving and transmitting station.
Basic requirements: Construct the Huffman tree based on the Occurrence Frequency of characters in a character file, compile the Huffman encoding, encode the given character file, generate the encoding file, and decode the given encoding file, generate character files. (Binary encoding is required) English files. Binary representation of encoding to generate binary encoding files.
# Pragma once # ifndef huffman_h # define huffman_h # include <iostream> # include <string> using namespace STD; //................. h file ............ class node // class for storing characters and character frequencies {public: intsign; charch; intfrequence; node (): frequence (0), sign (0) {} node (charx, int f = 0) {CH = x; frequence = f;} node & operator = (const node & N) {CH = n. ch; frequence = n. frequence; return * This;}; // ---------------------------------------------------- The following is the encoding class ------------------------------- class hufftree; Class huffnode {friend class hufftree; public: huffnode * Next, * leftchild, * rightchild, * parent; intcode [100]; //....... encoding .................... // intdata ;//.............. weight ....................... // charch; // character // huffnode (huffnode * pnext = NULL, huffnode * Left = NULL, huffnode * Right = NULL) {next = pnext; leftchild = left; rightchild = right ;}//.......... initial data member Flowers .......... // huffnode (intitem, char a, huffnode * pnext = NULL, huffnode * Left = NULL, huffnode * Right = NULL) {DATA = item; CH = A; next = pnext; leftchild = left; rightchild = right ;}; class hufftree {public: nodearray [100]; // ...... store an array of single character objects ....................... // huffnode * head, * huffroot; hufftree (); voidinsert (int x, char );//................ insert ................... // voidconnect (huffnode * P );//..... link a contact to a single-chain table .... .... // Huffnode * min ();///...................... find the contact with the smallest frequency voidfrequence (char line []); // calculate the frequency of each character voidsethufftree (); //.................. build the problem ....................... voidinorder (huffnode * r );//????????? Voidoutput (); // --------------------------------------- output function --- voiddelet (huffnode * P ); //........................... the pointer is disconnected and ---------- voidhuffcode (); // construct the Huffman code voidoutputcode (); // ------------------------------------------------- output code ----------- voidsortcode (); //............ output encoding information voidincode (char Cha []); // information encapsulation // voidtranscode (); // decoding }; # endif ////////////////////////////////////// /////////// //////////////////////////////////////// //////////////////////////////////////// //// // # Include "stdafx. H "# include" Huffman. H "# include <iostream> # include <fstream> using namespace STD; extern int number ;//.......... length of the input characters ................ // extern int A [100]; //... input character file encoding array ...... // extern int maxsize ;//........ encoding length of the input character file ............. // int count; // used to record the length of a single character int Len (char ch []) // used to calculate the length of a string {Inti (0), num (0); While (CH [I]! = NULL) {num ++; I ++;} number = num; returnnum;} hufftree: hufftree () // {huffnode * newnode = new huffnode (); head = newnode; head-> next = NULL;} void hufftree: insert (int x, chara) // ===---------------------------- insert function ------------------------- {huffnode * P, * pre; P = head-> next; If (P = NULL) {huffnode * newnode = new huffnode (X, A, null, null); head-> next = newnode; huffroot = newnode;} else {// huffnode (INT item, char, Huffnode * pnext = NULL, huffnode * Left = NULL, huffnode * Right = NULL) // {DATA = item; CH = A; next = pnext; leftchild = left; rightchild = right;} huffnode * newnode = new huffnode (X, A, null); While (p) {pre = P; P = p-> next ;} pre-> next = newnode; huffroot = newnode;} void hufftree: connect (huffnode * P) // link the leaf node of the Harman tree into a single-chain table (tail inserting) {huffnode * t = head, * pre; while (t) {pre = T; t = T-> next;} Pre-> next = P; p-> next = NULL;} huffnode * Hufftree: min () // --------------------------------- find the contact with the smallest frequency. {If (Head-> next = NULL) {cout <"the file is blank !.. "<Endl; returnnull;} else {intmin = 1000; huffnode * P = head-> next; huffnode * t; while (P! = NULL) {If (min> P-> data) {min = p-> data; t = P;} p = p-> next;} delet (t ); //....... disconnect after finding the smallest contact ........... // returnt ;}} void hufftree: frequence (char line []) // calculate the frequency of each character {char ch [100]; for (INT x = 0; x <Len (CH); X ++ )//..... len (CH) = Number .......... // {ch [x] = line [X];} Int J (0), length (0); int I, K; int size = Len (CH ); for (k = 0; k <size; k ++) {If (CH [k]! = NULL) {array [J]. ch = CH [k]; // ...... store a single character and calculate the frequency ................. // for (I = 0; I <size; I ++) {If (CH [I]! = NULL) {If (array [J]. ch = CH [I]) {++ array [J]. frequence; ch [I] = NULL ;}} J ++ ;}length = J; Count = length; // cout <"length =" <length <Endl;/* for (I = 0; I <length; I ++) {cout <"the" <I <"thchar is" <array [I]. ch <"The frequence is" <array [I]. frequence <Endl;} */} voidhufftree: sethufftree () // --------------------------------- create a user tree pipeline {huffnode * PT = head-> next; while (PT) {huffnode * P = mi N (); huffnode * t = min (); If (t = NULL) // The Last vertex // {insert (P-> data), null );} else {insert (P-> Data + T-> data), null);} huffroot-> leftchild = P; huffroot-> rightchild = T; p-> parent = huffroot; If (T! = NULL) {T-> parent = huffroot;} Pt = head-> next; If (Pt = NULL) {huffroot-> parent = pt; head-> next = NULL ;}}// -------------------------- create a single-connection table in the middle-order traversal of the harfman tree ------------------- void hufftree: inorder (huffnode * r) {If (R! = NULL) {inorder (R-> leftchild); // cout <r-> data; if (R-> leftchild = NULL & R-> rightchild = NULL) {connect (r); // cout <r-> CH ;} inorder (R-> rightchild );}}//..................... -output function ................................. // void hufftree: output () {huffnode * P = head-> next; // head point while (P) {cout <p-> CH <"" <p-> data <Endl; P = p-> next ;}} void hufftree :: delet (huffnode * P) // ---------------------- disconnect the pointer ---------- --- {Huffnode * t = head-> next, * pre = head; If (t = NULL) {cout <"all of the elements have been deleted !.. "<Endl; exit (1) ;}else {While (T! = P) {pre = T; t = T-> next;} Pre-> next = p-> next;} void hufftree: huffcode () // construct the Huffman encoding {huffnode * P = head-> next, * t; while (P! = NULL )//........ traverse a single-connection table (leaf node )............. // {int I = 99; t = P ;//??????? While (t-> parent! = NULL )//.......... put the encoding of each leaf node in the node array ..................... // {If (t-> parent-> leftchild = T) {P-> code [I] = 0; I --;} else {P-> code [I] = 1; I --;} t = T-> parent;} p = p-> next ;}} voidhufftree: outputcode () // ---------------------- encoding of each character -------------------- {huffnode * P = head-> next; intj (0); While (P) {// cout <"\ nthe char" <p-> CH <"'s Huffman codeis"; for (INTI = 0; I <100; I ++) {If (p-> code [I] = 0 | p-> code [I] = 1) {// cout <p-> code [I]; A [J] = p-> code [I]; //....... put the character encoding to an array .............. // J ++;} p = p-> next;} maxsize = J ;//.... the maximum value of a [I] character encoding ..... //} void hufftree: sortcode ()//............ output encoding information {cout <"After encoding:" <Endl; ofstreamoutfile2 ("file2.txt", IOS: Binary); If (! Outfile2) {cout <"Open file2.txterror! "<Endl; exit (1) ;}for (INT m = 0; m <count; m ++) {huffnode * P = head-> next; // single-chain table // while (p) {If (array [M]. ch = p-> CH) {// cout <"\ nthechar" <p-> CH <"'s Huffman code is"; for (INTI = 0; I <100; I ++) {If (p-> code [I] = 0 | p-> code [I] = 1) {outfile2.write (char *) & P-> code [I], sizeof (p-> code [I]) ;}} P = p-> next ;}} outfile2.close (); ifstreaminfile2 ("file2.txt", IOS: In | IOs: Binary); If (! Infile2) {cout <"Open file2.txterror! "<Endl; exit (1);} infile2.read (" file2.txt ", 100); While (p) {cout <p-> code [I]} infile2.close (); */cout <Endl;} void hufftree: incode (char Cha []) // information encapsulation // {intj = 0; // cout <"Number" <Len (CHA) <Endl;/* For (intk = 0; k <number; k ++) // output code // {cout <Cha [k];} */For (intm = 0; m <number; m ++) {huffnode * P = head-> next; while (p) {If (CHA [m] = p-> CH) {// cout <"\ nthe char" <p-> CH <"'s Huffman codeis"; for (INTI = 0; I <100; I ++) {If (p-> code [I] = 0 | p-> code [I] = 1) {// cout <p-> code [I]; A [J] = p-> code [I]; // ...... encode the character file to be entered in the array ..................... // J ++ ;}}p = p-> next ;}} maxsize = J ;}void hufftree: transcode () // decoding {ofstreamoutfile3 ("file3.txt"); If (! Outfile3) {cout <"Open file3.txterror! "<Endl; exit (1) ;}huffnode * P = huffroot; // the information after decoding is :"; while (p) {if (a [I] = 0) P = p-> leftchild; else P = p-> rightchild; if (p-> leftchild = NULL & P-> rightchild = NULL) {// cout <p-> CH; outfile3.put (p-> CH ); P = huffroot ;//......... return the Harf root contact ............. //} I ++; if (I> = maxsize) break;} outfile3.close (); ifstreaminfile3 ("file3.txt", IOS: In); char C; If (! Infile3) cout <"Open error! "<Endl; while (infile3.get (c) cout <C; infile3.close ();} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////.................. .. main ()................. // # include "stdafx. H "# include <iostream> # include <fstream> # include <string> # include" Huffman. H "using namespace STD; int number = 0; // String Length int A [100]; // used to store the Encoding Code array int maxsize = 0; // used to record the encoding length extern int count; // used to record the character length hufftree list; charch [100]; void Qidong () {Inti; ifstream infile ("file1.txt", IOS: In); If (! Infile) {cout <"Open file1.txt error! "<Endl; exit (1) ;}for (I = 0; I <100; I ++) {infile> CH [I]; if (CH [I] = '') & (CH [I + 1] ='') break;} infile. close () ;}void display () {for (INT I = 0; I <count; I ++) {list. insert (list. array [I]. frequence, list. array [I]. ch );//....... chain characters into a linked list ....... //} int main () {char op; intflag = 1; while (FLAG) {cout <Endl <"select:" <Endl; cout <"menu" <Endl; cout <"1 _ start the file to be encoded" <Endl; cout <"2 _ encoding" <Endl; cout <"3 _ output decoded text <Endl; cout <"4 _ exit program" <Endl; cout <Endl; CIN> op; Switch (OP) {Case '1 ': cout <"Start the file to be encoded:" <Endl; Qidong (); //................... call the character file .......... // list. frequence (CH );//......... calculate the frequency of each character ........ // display (); // list. output (); break; Case '2': cout <"encode the file:" <Endl; List. sethufftree ();//............ create a user tree ...................... // list. inorder (list. huffroot );//......... link A leaf node to a single-chain table ............... /// L Ist. output (); list. huffcode ();//......... construct the Huffman encoding ....... // list. sortcode ();//............. output code ............ // break; Case '3': cout <"encoded file:" <Endl; List. incode (CH); // information of the code corresponding to the compiled character file // list. transcode ();//....... output decoding information ........... // cout <Endl; break; Case '4': Flag = 0; cout <"after the program ends, press any key to exit" <Endl; break; default: cout <"input error. Please enter it again! "<Endl ;}} system (" pause "); return0 ;}
Welcome!