#include
#include
#include
#include
#include
using namespace std;
The class Node
{
Public:
The Node ();
~ the Node ();
char ch;
The Node (int w, char c) : ch (c), weight (w)
{
Left=right=next=NULL;
IsReal=true;
}
Private:
Char code;
int weight;
Node * next;
The Node * left;
Node * right;
Bool isReal.
Friend class HaFuMan;
};
Node: : Node ()
{
Left=right=next=NULL;
IsReal=true;
}
The class HaFuMan
{
Public:
HaFuMan (char *);
~ HaFuMan ();
Bool printEnCode (char * ch)
{
Node * temp=new Node;
Int len=strlen (ch);
If (len==0) return true;
For (char * p=ch; p
Temp - & gt; Ch=* p;
if (! PrintPathCode (temp))
{
Cout<& lt;" . The Error... "& lt;
}
}
return true;
};
Bool printPathCode (Node * s)
{
Node * b=nd - & gt; Next;
Node * St [10];
Node * p;
Int flag, top=1;
Do
{
While (b!=NULL)
{
top++;
St [top]=b;
B=b - & gt; The left;
}
P=0;
flag=1;
While (top!=1 & amp; & Flag)
{
B=St [top];
If (b - & gt; Right==p)
{
If (b - & gt; IsReal& & B - & gt; Ch==s - & gt; Ch)
{
for (int i=1; I & lt;=top; I++)
Cout
return true;
}
The else
{
Top -;
P=b;
}
}
The else
{
B=b - & gt; Right;
flag=0;
}
}
}
While (top!=1);
return false;
}
Bool isCharExits (Node *);
Void printDeCode (char *);
Void the pre (Node *);
Void mil (Node *);
Node * getStartNode ()
{
Return nd - & gt; Next;
}
Private:
Node * nd;
Void deleteNode (Node *);
Void insertNext (Node * temp)
{
Node * pre=nd;
Node * cur=nd - & gt; Next;
While (cur!=NULL& & Temp - & gt; Weight> Cur - & gt; Weight)
{
The pre=cur;
Cur=cur - & gt; Next;
}
Temp - & gt; Next=pre - & gt; Next;
The pre - & gt; Next=temp;
}
Void initTree ()
{
Node * temp;
While (nd - & gt; Next!=NULL)
{
If (nd - & gt; Next - & gt; Next==NULL)
{
If (nd - & gt; Next - & gt; IsReal==false)
{
Nd - & gt; Next - & gt; Code='1';
}
return;
}
Temp=new Node;
Temp - & gt; IsReal=false;
Temp - & gt; Weight=nd - & gt; Next - & gt; weight;
Temp - & gt; Left=nd - & gt; Next;
Nd - & gt; Next - & gt; Code='1';
Nd - & gt; Next=nd - & gt; Next - & gt; Next;
If (nd - & gt; Next!=NULL)
{
Temp - & gt; Weight +=nd - & gt; Next - & gt; weight;
Temp - & gt; Right=nd - & gt; Next;
Nd - & gt; Next - & gt; Code='0';
Nd - & gt; Next=nd - & gt; Next - & gt; Next;
}
InsertNext (temp);
}
}
Int getChar (char * ch);
};
Int HaFuMan: : getChar (char * ch)
{
Node * temp=nd - & gt; Next;
Int len=strlen (ch);
for (int i=0; I & lt; Len + 1; I++)
{
If (temp - & gt; Left!=NULL& & Temp - & gt; Left - & gt; Code==ch [I])
{
Temp=temp - & gt; The left;
continue;
}
If (temp - & gt; Right!=NULL& & Temp - & gt; Right - & gt; Code==ch [I])
{
Temp=temp - & gt; Right;
continue;
}
If (temp - & gt; Left==NULL& & Temp - & gt; Right==NULL)
{
Cout
}
}
}
Bool HaFuMan: : isCharExits (Node * s)
{
Node * b=nd - & gt; Next;
Node * St [10];
Node * p;
Int flag, top=1;
Do
{
While (b!=NULL)
{
top++;
St [top]=b;
B=b - & gt; The left;
}
P=0;
flag=1;
While (top!=1 & amp; & Flag)
{
B=St [top];
If (b - & gt; Right==p)
{
If (b - & gt; IsReal& & B - & gt; Ch==s - & gt; Ch)
{
return true;
}
The else
{
Top -;
P=b;
}
}
The else
{
B=b - & gt; Right;
flag=0;
}
}
}
While (top!=1);
return false;
}
Void HaFuMan: : printDeCode codes (char *)
{
Int len=strlen (codes);
Int k=0;
While (k
K +=getChar (& amp; Codes [k]);
}
Cout
HaFuMan: : HaFuMan (char * path)
{
Nd=new Node;
Freopen (path, "r", stdin);
int count=0;
Cin> The count.
int weight;
Char chtemp;
for (int i=0; I & lt; The count. I++)
{
Cin> Chtemp> weight;
InsertNext (new Node (weight, chtemp));
}
InitTree ();
}
HaFuMan: : ~ HaFuMan ()
{
DeleteNode (getStartNode ());
}
Void HaFuMan: : deleteNode (Node * nd)
{
//if (nd)
//{
//deleteNode (nd - & gt; Left);
//deleteNode (nd - & gt; Right);
//delete nd;
//}
}
Void HaFuMan: : pre (Node * p)
{
If (p!=NULL)
{
If (p - & gt; IsReal)
CoutThe else
Cout<& lt;" @ ";
The pre (p - & gt; Left);
The pre (p - & gt; Right);
}
}
Void HaFuMan: : mil (Node * p)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull