The realization of Huffman tree coding

Source: Internet
Author: User
Tags printf

#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,


Char **whatletter,


int **weight,int *count);


void Huffmancoding (Huffmantree *ht,


Huffmancode *HC,


int *weight,


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 ("\ n");


outputweight (Data,strlen (Data),


&whatletter,


&weight,


&count);


huffmancoding (&ht,&hc,weight,count);


printf ("Letter Weight code\n");


for (i=0;i<count;i++)


   {


printf ("%c", Whatletter[i]);


printf ("%d", weight[i]);


printf ("%s\n", hc[i+1]);


   }


printf ("\ n");


Getch ();


return 0;


}


void Huffmancoding (Huffmantree *ht,


Huffmancode *HC,


int *weight,


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++)


   {


(*HT) [i]. parent=0;


(*HT) [I].rchild=0;


(*HT) [I].lchild=0;


(*HT) [i]. Weight= (*weight);


weight++;


   }


for (i=count+1;i<=totallength;i++)


   {


(*HT) [i]. weight=0;


(*HT) [i]. parent=0;


(*HT) [I].lchild=0;


(*HT) [I].rchild=0;


   }


/*///////////////////create huffmantree////////////////*/


for (i=count+1;i<=totallength;++i)


   {


Select ((*ht), I-1,&S1,&S2);


(*HT) [S1]. parent=i;


(*HT) [S2]. parent=i;


(*HT) [I].lchild=s1;


(*HT) [I].rchild=s2;


(*HT) [i]. 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)


   {


start=count-1;


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]= (char*) malloc ((Count-start) *sizeof (char));


strcpy ((*HC) [I],&cd[start]);


     }


   }


}


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++)


   {


if (Ht[i]. parent==0)


     {


if (temp1==0)


       {


Temp1=ht[i]. Weight;


(*S1) =i;


       }


Else


       {


if (temp2==0)


         {


Temp2=ht[i]. Weight;


(*S2) =i;


if (TEMP2<TEMP1)


           {


TEMP3=TEMP2;


TEMP2=TEMP1;


Temp1=temp3;


temp3= (*S2);


(*S2) = (*S1);


(*S1) =temp3;


           }


         }


Else


         {


if (Ht[i]. WEIGHT<TEMP1)


           {


TEMP2=TEMP1;


Temp1=ht[i]. Weight;


(*S2) = (*S1);


(*S1) =i;


           }


if (Ht[i]. Weight>temp1&&ht[i]. WEIGHT<TEMP2)


{


Temp2=ht[i]. Weight;


(*S2) =i;


           }


         }


       }


     }


   }


}


int lookfor (char *str,char letter,int count)


{


int i;


for (i=0;i<count;i++)


   {


if (str[i]==letter) return i;


   }


return-1;


}


void Outputweight (char *data,int Length,


Char **whatletter,


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++)


{


if (i==0)


     {


Letter[0]=data[i];


lettercount[0]=1;


allcount++;


     }


Else


     {


index=lookfor (Letter,data[i],allcount);


if (index==-1)


       {


Letter[allcount]=data[i];


lettercount[allcount]=1;


allcount++;


       }


Else


       {


lettercount[index]++;


       }


     }


   }


for (i=0;i<allcount;i++)


   {


Sum=sum+lettercount[i];


   }


(*weight) = (int*) malloc (Allcount);


(*whatletter) = (char*) malloc (Allcount);


for (i=0;i<allcount;i++)


   {


persent= (float) lettercount[i]/(float) Sum;


(*weight) [i]= (int) (1000*persent);


(*whatletter) [i]=letter[i];


   }


(*count) =allcount;


}

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.