-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman new.cpp
More file actions
96 lines (91 loc) · 1.48 KB
/
huffman new.cpp
File metadata and controls
96 lines (91 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
#define max 256
typedef struct
{
int freq;
int right;
int left;
}Node;
Node nodes[2*max]={0};
typedef struct
{
int arr[max];// stores node indices.
int len;
}mq;
void init(mq *q)
{
q->len=0;
}
int isEmpty(mq *q)
{
return (q->len==0);
}
void insert(mq *q, int ndIndex)
{
int p,c,e,l=q->len;
e=nodes[ndIndex].freq;
c=l;
p=(c-1)/2;
while(c>0 && e<nodes[q->arr[p]].freq)
{
q->arr[c]=q->arr[p];
c=p;
p=(c-1)/2;
}
q->arr[c]=ndIndex;
q->len++;
}
int remove(mq *q)
{
int minIndex=q->arr[0];
int c=1,p=0,l=q->len; // c=child
int e=nodes[q->arr[l-1]].freq;
if(c+1<l && nodes[q->arr[c+1]].freq < nodes[q->arr[c]].freq)
c=2;
while(c<l && nodes[q->arr[c]].freq<e)
{
q->arr[p]=q->arr[c];
p=c;
c=2*p+1;
if(c+1<l && nodes[q->arr[c+1]].freq < nodes[q->arr[c]].freq)
c++;
}
q->arr[p]=q->arr[l-1];
q->len--;
return minIndex;
}
void FreqOfChar(char *fname)
{
FILE *fp;
int e;
fp=fopen(fname,"rb");
while(1)
{
e=fgetc(fp);
if(e==EOF)
break;
nodes[e].freq++;
}
fclose(fp);
}
int main()
{
int i,b;
mq q;
init(&q);
char fname[]="c:\\users\\lenovo\\desktop\\alice in wonderland.txt";
FreqOfChar(fname);
for(i=0;i<max;i++)
if(nodes[i].freq!=0)
printf("%d=%d\n",i,nodes[i].freq);
printf("\nAfter Sorting\n");
for(i=0;i<max;i++)
if(nodes[i].freq!=0)
insert(&q,i);
while(!isEmpty(&q))
{
b=remove(&q);
printf("%d=%d\n",b,nodes[b].freq);
}
}