Data structure experiment-the implementation of Heman tree (C ++)

Source: Internet
Author: User

The source code is as follows:

------------------------------------------------

# Include <iostream>
# Include <string>
# Include <vector>
Using namespace STD;

 

Typedef struct hchar
{
Char _ char;
Int _ weight;
} Hchar, * wcharptr;

Typedef struct htnode
{
Hchar _ data;
Int _ parent, _ lchild, _ rchild;
} Htnode;

Typedef struct htcode
{
Char _ char;
String _ code;
} Htcode;

 

Class hftree
{
PRIVATE:
Vector Vector // Int _ root; // Root Node
Public:
// Hftree ();
//~ Hftree ();
Bool inputdata (istream & input );
Bool huffmancoding ();
String coding (string & chars );
String decoding (string & codes );
Int select (int n );
Bool putoutcode ();
};

Bool hftree: inputdata (istream & input)
{
Int N;
Input> N;
Htnode temp;
While (n --)
{
Input> temp. _ data. _ char;
Input> temp. _ data. _ weight;
Temp. _ lchild = 0;
Temp. _ parent = 0;
Temp. _ rchild = 0;
_ Ht. push_back (temp );
}
Return true;
}

Bool hftree: huffmancoding ()
{
// Create a Heman tree
If (_ Ht. Empty ())
{
Return false;
}
Int n = _ HT. Size ();
Int m = 2 * n-1;
_ Ht. Resize (m );
Int min1 = 0, min2 = 0;
For (INT I = N; I <m; ++ I)
{
Min1 = select (I );
Min2 = select (I );
_ HT [min1]. _ parent = I, _ HT [min2]. _ parent = I;
_ HT [I]. _ lchild = min1, _ HT [I]. _ rchild = min2;
_ HT [I]. _ data. _ Weight = _ HT [min1]. _ data. _ weight + _ HT [min2]. _ data. _ weight;
}
// Obtain the Heman encoding.
Htcode cdtemp;
String: iterator cdit = cdtemp. _ code. Begin ();
_ HC. Reserve (N );
For (INT I = 0; I <n; ++ I)
{
For (INT c = I, F = _ HT [I]. _ parent; F! = 0; C = F, F = _ HT [f]. _ parent)
{
If (_ HT [f]. _ lchild = C)
{
Cdtemp. _ code. insert (cdit, 1, '0 ');
}
Else
{
Cdtemp. _ code. insert (cdit, 1, '1 ');
}
}
Cdtemp. _ char = _ HT [I]. _ data. _ char;
_ HC. push_back (cdtemp );
Cdtemp. _ code. Clear ();
}
Return true;
}

Int hftree: Select (int n)
{
If (_ Ht. Empty ())
{
Return false;
}
Int min =-1;
For (INT I = 0; I <n; I ++)
{
If (_ HT [I]. _ parent = 0)
{
If (Min <0 | _ HT [I]. _ data. _ weight <_ HT [Min]. _ data. _ weight)
{
Min = I;
}
}
}
If (Min <0)
{
Return-1;
}
_ HT [Min]. _ parent =-1;
Return min;
}

Bool hftree: putoutcode ()
{
If (_ HC. Empty ())
{
Return false;
}
Vector While (hcit! = Hcitend)
{
Cout Hcit ++;
}
Return true;
}

String hftree: Coding (STD: string & chars)
{
If (_ HC. Empty ())
{
Return NULL;
}
Unsigned int sum = 0; // number of characters encoded unsigned int string: size ()
String codes; // code
String: iterator CIT = chars. Begin (), citend = chars. End ();
Vector For (; CIT! = Citend; CIT ++)
{
For (hcit = _ HC. Begin (); hcit! = Hcitend; hcit ++)
{
If (hcit-> _ char = * CIT)
{
Sum ++; // a character is encoded.
Codes + = hcit-> _ code;
}
}
}
If (sum <chars. Size ())
{
Return NULL;
}
Return codes;
}

String hftree: Decoding (STD: string & Codes)
{
If (_ HC. Empty ())
{
Return NULL;
}
Int I;
String chars;
String: iterator CIT = codes. Begin (), citend = codes. End ();
// Vector While (CIT! = Citend)
{
For (I = _ Ht. Size ()-1; _ HT [I]. _ lchild! = 0 | _ HT [I]. _ rchild! = 0 ;)
{
If (* CIT = '0 ')
{
CIT ++;
I = _ HT [I]. _ lchild;
}
Else if (* CIT = '1 ')
{
CIT ++;
I = _ HT [I]. _ rchild;
}
}
Chars. push_back (_ HT [I]. _ data. _ char );
}
Return chars;
}

Int main ()
{
Cout <"Enter data initialization:" <Endl;
Hftree temp;
Temp. inputdata (CIN );
Temp. huffmancoding ();
Temp. putoutcode ();
String chars, codes;
Cout <"input string:" <Endl;
Cin> chars;
Codes = temp. coding (chars );
Cout <codes <Endl;
Cout <"input encoding :";
Cin> codes;
Chars = temp. decoding (codes );
Cout <chars;
Return true;
}

 

 

// The initial test function is normal and no exception or error is found. You need to test it further ~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.