Huffman encoding (data structure)

xiaoxiao2021-03-06  35

#include

#include

#include

Class Data {

PUBLIC:

CHAR CH;

Data () {

CH = NULL;

}

}

Typedef class huffman {

PUBLIC:

Data Data;

Int weight;

INT PARENT, LCHILD, RCHILD;

HUFFMAN () {

Weight = pent = lchild = rChild = null;

}

* Huffmantree;

Typedef char ** huffmancode;

/ / Select two small weight data in an array

Void SELECT (Int * TW, INT N, INT & S1, INT & S2)

{

/ / Establish a temporary variable TEMP_W to store the smallest Weight

INT TEMP_W, I = 1;

While (! tw [i])

{

i ;

}

TEMP_W = TW [I];

S1 = I;

// First, I define S1 first.

For (; i <= n; i )

{

IF (Temp_W> TW [I])

{

IF (TW [i]! = null)

{

TEMP_W = TW [I];

S1 = i;

} // ifin

} // if

} // for

i = 1;

While (! TW [i] || i == S1)

{

i ;

}

TEMP_W = TW [I];

S2 = I;

For (; i <= n; i )

{

IF (i == S1)

Goto loop;

IF (Temp_W> TW [I])

{

IF (TW [I])

{

TEMP_W = TW [I];

S2 = I;

} // ifin

LOOP :;

} // if

} // for

/ / Put your weight to S1

IF (TW [S1]> TW [S2])

{

TEMP_W = S1;

S1 = S2;

S2 = TEMP_W;

} // if

// After obtaining two small locations, scratch the right to the standby TEMP_ARRAY_W position, indicating that has been accessed.

TW [S1] = TW [S2] = NULL;

TW [0] = TW [0] -2;

}

Void Initialization (HuffmanTree & HT, Huffmancode & HC, INT N)

{

// give the file a number of tags, put it in the HT [0] line

HT [0] .weight = n;

HT [0] .DATA.CH = 3;

INT I;

// Establish a power group for assisting build trees

INT * TEMP_ARRAY_W = New Int [2 * n];

// The right to initialize the number of 0

For (i = 1; i <= 2 * n -1; i )

TEMP_ARRAY_W [I] = 0;

For (i = 1; i <= n; i )

{

Cout << "Input Character:";

CIN >> HT [I] .data.ch;

IF (ht [i] .data.ch == '/ 32')

HT [i] .data.ch = '|';

Cout << "input rights:";

CIN >> HT [I] .weight;

// Temp_array_w [i] = HT [i] .weight;

// record a number of units that are not available

TEMP_ARRAY_W [0] = i;

} // for

// Establish a sign S1 and S2

Int S1, S2;

S1 = S2 = NULL;

// Establish a Huffman tree

For (i = n 1; i <= 2 * n - 1; i )

{

SELECT (Temp_Array_W, I-1, S1, S2);

COUT << S1 << "// << S2 << endl;

HT [i] .weight = TEMP_ARRAY_W [I] = HT [S1] .weight HT [S2] .weight;

HT [i] .lchild = S1;

HT [i] .rchild = S2;

HT [S1] .parent = HT [S2] .parent = i;

HT [i] .data.ch = '#';

} // for

Cout << "/ tdata / tweight / tparent / tlchild / trchild" << ENDL;

For (i = 1; i <= 2 * n - 1; i )

COUT << "/ t" << HT [i] .data.ch << "/ t" << HT [i] .weight << "/ t" << ht [i] .parent << "/ t "<< HT [i] .lchild <<" / t "<< HT [i] .rchild << endl;

// Establish a tree after print output to file humtree

OFSTREAM HUMTREEOUT ("Humtree.dll");

// Record the number to the file

HumtreeOut << HT [0] .weight << Endl;

For (i = 1; i <= 2 * n -1; i )

{

HUMTREEOUT << HT [i] .data.ch << "/ n" << HT [i] .weight << "/ n" << HT [i] .parent << "/ n" << HT [i ] .lchild << "/ n" << HT [i] .rchild << endl;

}

Humtreeout.close ();

} // void

// Give character encoding through the tree, and input the encoded into the codefile

Void Encoding (Huffmancode & HC, HUFFMANTREE & HT, INT N, IFSTREAM & TOBETRANFILE)

{

Char * cd = new char [n];

CD [N-1] = '/ 0';

INT i = 1;

For (; i <= n; i )

{

INT start = N -1;

INT C, F;

For (c = i, f = HT [i] .parent; f! = 0; c = f, f = HT [f] .parent)

IF (ht [f] .lchild == C)

CD [- start] = '0'; ELSE

CD [- start] = '1';

HC [i] = new char [n-start];

STRCPY (HC [I], & CD [Start]);

}

DELETE CD;

CHAR TEMP_FILE_CH;

OFSTream Codeout ("Codefile.txt", iOS :: ATE); // ios :: ATE indicates an Open mode, which is continued after the file

While (! TOBETRANFILE.EOF ()) // Once the file is at the end, the EOF () function returns a non-zero value.

{

TOBETRANFILE.GET (TEMP_FILE_CH);

For (i = 1; i <= n; i )

{

IF (Temp_File_Ch == HT [i] .data.ch) // If equally, use encoding replacement

CodeOut << HC [i];

}

}

Codeout.close ();

}

Void Decoding (Huffmantree & HT, IFStream & Codefile, Int N)

{

OFSTREAM TEXTOUT ("TextFile.txt", iOS :: ATE);

CHAR TEMP_CODE_CH;

INT TEMP_NUM = 2 * n - 1;

While (! codefile.eof ())

{

Codefile.get (TEMP_CODE_CH);

IF (Temp_code_CH == '1')

IF (! HT [Temp_num] .rchild)

{

TextOut << HT [Temp_num] .data.ch;

TEMP_NUM = 2 * n - 1;

}

Else

{

Temp_num = HT [TEMP_NUM] .rchild;

IF (! HT [Temp_num] .rchild) // In fact, a child is empty, representing a leaf node

{

TextOut << HT [Temp_num] .data.ch;

TEMP_NUM = 2 * n - 1;

}

}

Else

IF (! HT [Temp_num] .lchild)

{

TextOut << HT [Temp_num] .data.ch;

TEMP_NUM = 2 * n - 1;

}

Else

{

Temp_num = HT [TEMP_NUM] .lchild;

IF (! HT [Temp_num] .lchild) // In fact, a child is empty, representing a leaf node

{

TextOut << HT [Temp_num] .data.ch;

TEMP_NUM = 2 * n - 1;

}

}

}

Textout.close ();

Cout << "has been translated into text.txt file" << endl;

}

// build a new tree through the existing Humtree.dll

Bool CreatneWhum (Huffmantree & HT, INT & N)

{

Char * ch_name = new char [30];

/ / Store characters read from the file

INT TEMP_N;

CHAR TEMP_CH;

INT i = 1;

COUT << "please input the file name:"

CIN >> CH_NAME;

ifstream huffmantreein (ch_name);

if (Huffmantreein.fail ())

{

Huffmantreein.close ();

Cout << "You can't open the file << Endl; return false;

}

// huffmantree ht_temp = ht;

// huffmantreein.getLine (Temp_Line, 9);

HuffMANTreein >> Temp_n; // a word is entry

Huffmantreein.get (TEMP_CH);

// huffmantreein.seekg (1);

HT = New Huffman [2 * Temp_n];

// delete HT_TEMP;

// assign the HT assignment by reading the data in the file

/ *

For (; i <20; i )

{

Huffmantreein.get (TEMP_CH);

Cout << temp_ch;

}

// * /

// *

INT j = 1;

While (! huffmantreein.eof ())

{

IF (i% 5 == 1)

{

HUFFMANTREEIN >> HT [J] .DATA.CH;

Huffmantreein.get (TEMP_CH); // Used to recycle the carriage return

i ;

}

IF (i% 5 == 2)

{

HUFFMANTREEIN >> HT [J] .weight;

Huffmantreein.get (TEMP_CH);

i ;

}

IF (i% 5 == 3)

{

HUFFMANTREEIN >> HT [J] .parent;

Huffmantreein.get (TEMP_CH);

i ;

}

IF (i% 5 == 4)

{

HUFFMANTREEIN >> HT [J] .lchild;

Huffmantreein.get (TEMP_CH);

i ;

}

IF (i% 5 == 0)

{

HUFFMANTREEIN >> HT [J] .rchild;

Huffmantreein.get (TEMP_CH);

i ;

}

// i Self-add 5 multiple times J

IF (i% 5 == 1)

J ;

/ / Prevent the input last positioning symbol

IF (i> (2 * temp_n -1) * 5)

Break;

} // while

// * /

// Read the tree from the specified file

Huffmantreein.close ();

N = TEMP_N;

Return True;

}

Void PrintCode ()

{

Char * name_ch = new char [51];

Ifstream filein ("codefile.txt");

OFSTREAM Fileout ("Codeprin.txt", iOS :: ATE);

IF (filein.fail ())

Cout << "file opens an error!" << Endl;

While (! filein.eof ())

{

Filein.getLine (name_ch, 51);

COUT << name_ch << endl;

Fileout << name_ch << Endl;

}

Filein.close ();

Fileout.close ();

}

Void trepprint ()

{

}

Void titalprint () {

COUT << "/ t ============================================ ============== "<< endl; cout << endl;

Cout << "/ t = / t / t / t / t / t =" << ENDL;

Cout << Endl;

COUT << "/ t = / t / ti: initial a huffman Tree / T / T =" << ENDL;

Cout << Endl;

Cout << "/ t = / t / te: Encodeing the data / t / t / t =" << ENDL;

Cout << Endl;

Cout << "/ t = / t / td: decodeing the data / t / t / t =" << end1

Cout << Endl;

Cout << "/ t = / t / tq: quit / t / t / t / t / t =" << ENDL;

Cout << Endl;

COUT << "/ t ============================================ ============== "<< endL;

}

// put all the functions of several operations to one, the main function main is only called the following operation function.

Void computehuffman ()

{

/ / Establish a variable for temporary input storage options

CHAR TEMP_INPUT = NULL;

TitalPrint ();

// Record to give a few encodings

INT n = 0;

// First, the main interface is until you quit.

While (Temp_input! = 'q')

{

// Establish an operation variable

CHAR TEMP_CHOISE = 0;

Huffmantree HT;

Huffmancode HC;

// Use Switch / Case to determine which option is pressed

COUT << "Choose Which Do you select ..." << endl;

CIN >> TEMP_INPUT;

Switch (temp_input)

{

Case 'I':

COUT << "How Many Numbers Need to Be Initialized:";

CIN >> N;

// Establish an array storage data

HT = New Huffman [2 * n]; INITIALIZATION (HT, HC, N);

Break;

Case 'E':

{

// *

COUT << "INPUTING The Humffmantree from The (f) Ile OR (M) Emory:";

CIN >> TEMP_CHOISE;

Switch (Temp_choise)

{

Case 'f':

IF (! CreatneWhum (HT, N))

CONTINUE;

Break;

Case 'M':

// do nothing

Break;

DEFAULT:

COUT << "" please pess f or m ... "<< endl;

CONTINUE;

} // switch

// * /

COUT << "Translating In (f) ILE OR (P) RESING:"

CIN >> TEMP_CHOISE;

Switch (Temp_choise)

{

Case 'f':

Break;

Case 'P':

{

Cout << "Due to its energy limit, only 80 characters can now be entered now." << Endl;

Char * TEMP_PRESS_WORD = New Char [81];

CIN >> TEMP_PRESS_WORD;

/ / Can override the previous TOBETRAN.TXT file

OFSTREAM CODEFILEOUT ("TOBETRAN.TXT");

Codefileout << Temp_press_word;

Break;

}

}

HC = New char * [n 1];

IFStream TobetRanfile ("TOBETRAN.TXT");

Encoding (HC, HT, N, TOBETRANFILE);

TOBETRANFILE.CLOSE ();

For (INT i = 1; i <= n; i )

COUT << HT [i] .data.ch << "<-> << HC [i] << endl;

}

// Use the established Huffman tree (if you do not read from the file humtree),

The body in // TOBETRAN is encoded, and then the result is inch into the CodeFile.txt file.

Break;

Case 'd':

{

/ / Can be translated by different trees

COUT << "INPUTING The Humffmantree from The (f) Ile OR (M) Emory:";

CIN >> TEMP_CHOISE;

Switch (Temp_choise)

{

Case 'f':

{

IF (! CreatneWhum (HT, N))

CONTINUE;

HC = New char * [n 1];

/ *

IFStream TobetRanfile ("TOBETRAN.TXT");

Encoding (HC, HT, N, TOBETRANFILE);

TOBETRANFILE.CLOSE ();

* /

}

Break;

Case 'M':

// do nothing

Break;

DEFAULT:

COUT << "" please pess f or m ... "<< endl;

CONTINUE;

} // switch

IFStream Codefile ("Codefile.txt"); Decoding (HT, Codefile, N);

Codefile.Close ();

}

// Translate the code in the document codefile using the created HAFFMAN tree. Result Inch TEXTFILE.TXT

Break;

Case 'P':

PRINTCODE ();

// Print code, 50 per line, and write this character form to Codeprin

Break;

Case 'T':

// Print Huffman tree, will have displayed in an intuitive HUFFMAN tree in an intuitive manner, and the HUFFMAN tree in this character

// into TreePrint

Break;

Case 'Q':

Return;

DEFAULT:

COUT << "please inputin" << endl;

CONTINUE;

} // switch

/ / Go back to the main menu

TitalPrint ();

} // while

} // computerhuffman

void main ()

{

Computehuffman ();

}

转载请注明原文地址:https://www.9cbs.com/read-78099.html

New Post(0)