哈夫曼树的编码实现

#include 
#include 
#include 
#include   
using namespace std;
typedef struct{
  char data;
  int weight;
  int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char ** HuffmanCode;
void Select(HuffmanTree &HT,int n,int m) 
{
  HuffmanTree p=HT;
  int tmp;
  for(int j=n+1;j<=m;j++)
  {
       int tag1,tag2,s1,s2;
       tag1=tag2=256;
  for(int x=1;x<=j-1;x++)  
  { 
       if(p[x].parent==0&&p[x].weights2)  //将选出的两个节点中的序号较小的始终赋给s1
{
  tmp=s1;
  s1=s2;
  s2=tmp;
}
    p[s1].parent=j;
p[s2].parent=j;
p[j].lchild=s1;
p[j].rchild=s2;
p[j].weight=p[s1].weight+p[s2].weight;
  }
}
void HuffmanCoding(HuffmanTree &HT,int n,char *w1,int*w2)
{  
   int m=2*n-1;
   if(n<=1)   return;
   HT=new HTNode[m+1];
   HuffmanTree p=HT;
   for(int i=1;i<=m;i++)
   { 
       p[i].weight=p[i].parent=p[i].lchild=p[i].rchild=0; 
   }
   for(int i=1;i<=n;i++)
   {
       p[i].data=w1[i-1];
  p[i].weight=w2[i];
       p[i].parent=p[i].lchild=p[i].rchild=0;
   }
  
  Select(HT,n,m);
  ofstream outfile("hfmTree.txt");        //生成hfmTree文件
  for (int i=1;i<=m;i++)
  {
     outfile<>n;
     return n;
}
int main()
{  
   int n;
   int Array[100];
   char cArray[100];
   HuffmanTree HT;
   cout<<"输入n个字符:";
   cin.getline(cArray,100);
   n=strlen(cArray);
   cout<<"一共"<>Array[i];
   int tag;
   int x=menu();
   while(1)
   {
      switch (x)  
      {
          case 1:HuffmanCoding(HT,n,cArray,Array);
          break;
          case 2:Encoding(HT,n);
          break;
          case 3:Decoding(HT,n);
          break;
          case 4:Print();
          break;
          case 0:
          cout<<"结束"<>x;  
    }
} 

你可能感兴趣的:(哈夫曼树的编码实现)