-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.c
178 lines (147 loc) · 3.24 KB
/
huffman.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "stdio.h"
#include "stdlib.h"
struct node
{
char data;
int freq;
struct node* left,*right;
};
struct node* intialize(int [],int []);
void build_heap(struct node** );
void min_heapify(struct node** ,int );
void swap(char* ,char* ,int* ,int* );
void huffman_tree(struct node** );
struct node* extract_min(struct node** );
void insert_node(struct node**, struct node*);
void print_values_huffman(struct node*,int [],int );
int size;
void main()
{
int i,arr[500];
int data[]={'c','f','d','a','e','b'};
int freq[]={10,80,13,5,14,20};
size=6;
printf("inital values:\n");
for(i=0;i<6;i++)
{
printf("%c %d\n",data[i],freq[i]);
}
struct node *root=intialize(data,freq);
printf("huffman tree codes:\n");
print_values_huffman(root,arr,0);
}
struct node* intialize(int data[],int freq[])
{
int i;
struct node**arr=(struct node**)malloc(size*sizeof(struct node)); //creating array of nodes
for(i=0;i<size;i++)
{
arr[i]=(struct node*)malloc(sizeof(struct node));
arr[i]->data=data[i];
arr[i]->freq=freq[i];
arr[i]->left=arr[i]->right=0;
}
build_heap(arr);
huffman_tree(arr);
return arr[0];
}
void print_values_huffman(struct node* root,int arr[],int i)
{
int j;
if(root->left)
{
arr[i]=0;
print_values_huffman(root->left,arr,i+1);
}
if(root->right)
{
arr[i]=1;
print_values_huffman(root->right,arr,i+1);
}
if(root->right==0 && root->left==0)
{
printf("%c %d ",root->data,root->freq);
for(j=0;j<i;j++)
{
printf("%d",arr[j]);
}
printf("\n");
}
}
void build_heap(struct node **arr)
{
int i;
for(i=(size-1)/2;i>=0;i--)
{
min_heapify(arr,i);
}
}
void min_heapify(struct node** arr,int i)
{
int l=2*i+1;
int r=2*i+2;
int min=i;
if(l<size && arr[l]->freq<arr[min]->freq)
min=l;
if(r<size && arr[r]->freq<arr[min]->freq)
min=r;
if(min!=i)
{
struct node *temp;
swap(&arr[i]->data,&arr[min]->data,&arr[i]->freq,&arr[min]->freq); //swapping values
temp=arr[i]->left; //swapping addresses
arr[i]->left=arr[min]->left;
arr[min]->left=temp;
temp=arr[i]->right; //swapping address
arr[i]->right=arr[min]->right;
arr[min]->right=temp;
min_heapify(arr,min);
}
}
void swap(char* id ,char* md,int* ifr,int* mfr) //id-> i data, md-> min data, ifr-> i frequency, mfr-> min frequency
{
int temp;
char ctemp;
temp = *ifr;
*ifr = *mfr;
*mfr = temp;
ctemp = *id;
*id = *md;
*md = ctemp;
}
struct node* extract_min(struct node** arr) //extracting the min from heap
{
struct node* min =(struct node*)malloc(sizeof(struct node));
min->data=arr[0]->data;
min->freq=arr[0]->freq;
min->left=arr[0]->left;
min->right=arr[0]->right;
arr[0]->data=arr[size-1]->data;
arr[0]->freq=arr[size-1]->freq;
arr[0]->right=arr[size-1]->right;
arr[0]->left=arr[size-1]->left;
size--;
min_heapify(arr,0);
return min;
}
void insert_node(struct node** arr,struct node* new_node)
{
size++;
arr[size-1]=new_node;
build_heap(arr);
}
void huffman_tree(struct node** arr)
{
char *temp="#";
while(size!=1)
{
struct node* min1=extract_min(arr);
struct node* min2=extract_min(arr);
struct node* new_node =(struct node*)malloc(sizeof(struct node));
new_node->freq=(min1->freq)+(min2->freq);
new_node->data=*temp;
new_node->left=min1;
new_node->right=min2;
insert_node(arr,new_node);
}
}