/*HUFFMAN CODING*/
#include<stdio.h>
int n;
char c[10];
char s[10];
float p[10];

void shift()
{
int i,z,j,k,n,w;
float f[10];
for(j=n-1;j>0;j--)
{
i=j;
n=p[i]+p[i-1];
while(n>=p[i-3])
i--;
f[i+1]=n;
w=i;
for(k=i+2;k<=j-1;k++)
f[k]=p[k-1];
for(;w>=0;w--)
f[w]=p[w];
for(z=0;z<=k;z++)
printf("%f",f[z]);
}
}
void cum()
{
int no[10],i;
float pro[10],sum;
sum=0;
for(i=0;i<n;i++)
{
no[i]=strlen(c[i]);
pro[i]=no[i]*p[i];
sum=sum+pro[i];
}
printf("Length of code is:%f",sum);
}

void main()
{
int i;
printf("Enter the number of symbols:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the symbol:");
scanf("%s",&s[i]);
printf("Enter the probab:");
scanf("%f",&p[i]);
printf("Enter the code:");
scanf("%s",&c[i]);
}
printf("\nsymbol probaility code");
for(i=0;i<n;i++)
printf("%s%f%s",s[i],p[i],c[i]);
shift();
cum();
}
/****************OUTPUT*********************/
Enter the number of symbols:6
Enter the symbol:a
Enter the probab:0.4
Enter the code:11
Enter the symbol:b
Enter rhe probab:0.2
Enter the code:00
Enter the symbol:c
Enter the probab:0.1
Enter the code:000
Enter the symbol:d
Enter the probab:0.1
Enter the code:001
Enter the symbol:e
Enter the probab:0.1
Enter the code:011
Enter the symbol:f
Enter the probab:0.1
Enter the code:111

symbol probaility code
a      0.4        11 
b      0.2        00 
c      0.1        000
d      0.1        001 
e      0.1        011
f      0.1        111

0.4
0.2
0.2
0.1
0.1

0.4
0.2
0.2
0.2

0.4
0.4
0.2

0.6
0.4

1.0

Lenght of code is:2.4