-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhuffman.c
More file actions
executable file
·87 lines (65 loc) · 2.14 KB
/
huffman.c
File metadata and controls
executable file
·87 lines (65 loc) · 2.14 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
#include "heap.h"
#include "tree.h"
#include "code.h"
#define BUFFER_SIZE 1000000
int debug_heap = 0;
int debug_huffman_tree = 0;
/****************************************************************************************
*
* Main function: parses parameters
* open and load input file
* create the heap
* builds the huffman tree
* generate huffman code
*
*****************************************************************************************/
int main(int argc, char **argv)
{
FILE *in_file;
char buffer[BUFFER_SIZE];
int input_size = 0;
int counting_bin[256];
int i;
if (argc != 2)
{
fprintf(stderr, "Error: usage: %s [file-name]\n", argv[0]);
return -1;
}
// Open the input file for read
in_file = fopen(argv[1], "rb");
if (!in_file)
{
fprintf(stderr, "Error: invalud input file: %s\n", argv[1]);
return -1;
}
// Read the input and place the input in the buffer
input_size = fread(buffer, sizeof(char), BUFFER_SIZE, in_file);
// Close the input file for read
fclose(in_file);
// Check for empty file
if (input_size <= 0)
return 0;
// Counting the frequency of each character
for (i = 0; i < 256; i++)
counting_bin[i] = 0;
for (i = 0; i < input_size; i++)
counting_bin[(int)buffer[i]]++;
// Initialize and creating the heap
heapInit();
for (i = 0; i < 256; i++)
if (counting_bin[i])
HeapInsert(i, NULL, counting_bin[i]);
// Insert end of file marker
HeapInsert(0, NULL, 0);
// Print the heap
heapPrint();
// build the buffman tree
build_huffman_tree();
// Print the huffman tree
print_huffman_tree(huffman_tree, 0);
// initialize and generate code based on the huffman tree
init_code();
gen_code(huffman_tree, 0);
print_code();
return 0;
}