国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

霍夫曼樹編碼的實現

2019-09-06 23:33:33
字體:
來源:轉載
供稿:網友

                    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

typedef struct
{
   unsigned int Weight;
   unsigned int Parent;
   unsigned int lChild;
   unsigned int rChild;
}HTNode,*HuffmanTree;

typedef char **HuffmanCode;

int LookFor(char *str,char letter,int count);
void OutputWeight(char *Data,int Length,
/t/t  char **WhatLetter,
/t/t  int **Weight,int *Count);
void HuffmanCoding(HuffmanTree *HT,
/t/t   HuffmanCode *HC,
/t/t   int *Weight,
/t/t   int Count);
void Select(HuffmanTree HT,int Count,int *s1,int *s2);
int main()
{
   HuffmanTree HT;
   HuffmanCode HC;
   char Data[100];
   char *WhatLetter;
   int *Weight;
   int Count;
   int i;

   printf("Please input the line:");
   /* Example: aaaaaaaaaaaaaabcccccc*/
   scanf("%s",Data);
   printf("");

   OutputWeight(Data,strlen(Data),
/t/t &WhatLetter,
/t/t &Weight,
/t/t &Count);

   HuffmanCoding(&HT,&HC,Weight,Count);

   printf(" Letter Weight Code");
   for(i=0;i<Count;i++)
   {
/tprintf(" %c ",WhatLetter);
/tprintf("%d ",Weight);
/tprintf("%s",HC[i+1]);
   }
   printf("");
   getch();
   return 0;
}

void HuffmanCoding(HuffmanTree *HT,
/t/t   HuffmanCode *HC,
/t/t   int *Weight,
/t/t   int Count)
{
   int i;
   int s1,s2;
   int TotalLength;
   HuffmanTree p;
   char* cd;
   unsigned int c;
   unsigned int f;
   int start;

   if(Count<=1) return;
   TotalLength=Count*2-1;
   (*HT)=(HuffmanTree)malloc((TotalLength+1)*sizeof(HTNode));

   p=((*HT)++);
   for(i=1;i<=Count;i++)
   {
/t(*HT).Parent=0;
/t(*HT).rChild=0;
/t(*HT).lChild=0;
/t(*HT).Weight=(*Weight);
/tWeight++;
   }
   for(i=Count+1;i<=TotalLength;i++)
   {
/t(*HT).Weight=0;
/t(*HT).Parent=0;
/t(*HT).lChild=0;
/t(*HT).rChild=0;
   }
   /*///////////////////Create HuffmanTree////////////////*/
   for(i=Count+1;i<=TotalLength;++i)
   {
/tSelect((*HT),i-1,&s1,&s2);
      (*HT)[s1].Parent=i;
      (*HT)[s2].Parent=i;
      (*HT).lChild=s1;
      (*HT).rChild=s2;
      (*HT).Weight=(*HT)[s1].Weight+(*HT)[s2].Weight;
   }
   /*///////////////////Output Huffman Code///////////////*/
   (*HC)=(HuffmanCode)malloc((Count+1)*sizeof(char*));
   cd=(char*)malloc(Count*sizeof(char));
   cd[Count-1]='';
   for(i=1;i<=Count;++i)
   {
/tstart=Count-1;
/tfor(c=i,f=(*HT).Parent;f!=0;c=f,f=(*HT)[f].Parent)
/t{
/t    if((*HT)[f].lChild==c)
/t/tcd[--start]='0';
/t    else
/t/tcd[--start]='1';
/t    (*HC)=(char*)malloc((Count-start)*sizeof(char));
/t    strcpy((*HC),&cd[start]);
/t}
   }
}
void Select(HuffmanTree HT,int Count,int *s1,int *s2)
/*/(*s1) is smallest,(*s2) is smaller.*/
{
   int i;
   unsigned int temp1=0;
   unsigned int temp2=0;
   unsigned int temp3;
   for(i=1;i<=Count;i++)
   {
/tif(HT.Parent==0)
/t{
/t    if(temp1==0)
/t    {
/t/ttemp1=HT.Weight;
/t/t(*s1)=i;
/t    }
/t    else
/t    {
/t/tif(temp2==0)
/t/t{
/t/t    temp2=HT.Weight;
/t/t    (*s2)=i;
/t/t    if(temp2<temp1)
/t/t    {
/t/t/ttemp3=temp2;
/t/t/ttemp2=temp1;
/t/t/ttemp1=temp3;

/t/t/ttemp3=(*s2);
/t/t/t(*s2)=(*s1);
/t/t/t(*s1)=temp3;
/t/t    }
/t/t}
/t/telse
/t/t{
/t/t    if(HT.Weight<temp1)
/t/t    {
/t/t/ttemp2=temp1;
/t/t/ttemp1=HT.Weight;

/t/t/t(*s2)=(*s1);
/t/t/t(*s1)=i;
/t/t    }
/t/t    if(HT.Weight>temp1&&HT.Weight<temp2)
/t/t    {
/t/t/ttemp2=HT.Weight;
/t/t/t(*s2)=i;
/t/t    }
/t/t}
/t    }
/t}
   }
}

int LookFor(char *str,char letter,int count)
{
   int i;
   for(i=0;i<count;i++)
   {
/tif(str==letter) return i;
   }
   return -1;
}
void OutputWeight(char *Data,int Length,
/t/t  char **WhatLetter,
/t/t  int **Weight,int *Count)
{
   int i;
   char* Letter=(char*)malloc(Length);
   int* LetterCount=(int *)malloc(Length);
   int AllCount=0;
   int Index;
   int Sum=0;
   float Persent=0;

   for(i=0;i<Length;i++)
   {
/tif(i==0)
/t{
/t    Letter[0]=Data;
/t    LetterCount[0]=1;
/t    AllCount++;
/t}
/telse
/t{
/t    Index=LookFor(Letter,Data,AllCount);
/t    if(Index==-1)
/t    {
/t/tLetter[AllCount]=Data;
/t/tLetterCount[AllCount]=1;
/t/tAllCount++;
/t    }
/t    else
/t    {
/t/tLetterCount[Index]++;
/t    }
/t}
   }
   for(i=0;i<AllCount;i++)
   {
/tSum=Sum+LetterCount;
   }
   (*Weight)=(int*)malloc(AllCount);
   (*WhatLetter)=(char*)malloc(AllCount);

   for(i=0;i<AllCount;i++)
   {
/tPersent=(float)LetterCount/(float)Sum;
/t(*Weight)=(int)(1000*Persent);
/t(*WhatLetter)=Letter;
   }
   (*Count)=AllCount;
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邵阳县| 申扎县| 玉林市| 克什克腾旗| 镇安县| 崇仁县| 高密市| 永安市| 漳州市| 习水县| 林甸县| 德格县| 镇安县| 蕉岭县| 渑池县| 长泰县| 江华| 富顺县| 恩施市| 肃北| 福贡县| 浦县| 镇巴县| 湘潭县| 年辖:市辖区| 景德镇市| 陇南市| 文山县| 扶余县| 定安县| 青阳县| 会泽县| 沛县| 邳州市| 海丰县| 繁昌县| 安义县| 平潭县| 交城县| 罗平县| 芦溪县|