-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_Zacharias_4_2016smalyala.c
More file actions
103 lines (95 loc) · 1.69 KB
/
encode_Zacharias_4_2016smalyala.c
File metadata and controls
103 lines (95 loc) · 1.69 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
96
97
98
99
100
101
102
103
//
// Torbert, 16 Sept 2015
//
#include <stdio.h>
#include <stdlib.h>
//
typedef struct Node
{
char symbol ;
//
int frequency ;
//
struct Node* left ;
struct Node* right ;
//
} TreeNode ;
void sortArr(TreeNode* arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
if ((arr[i]->frequency) < (arr[j]->frequency))
{
TreeNode* a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
}
//
int main( int argc , char* argv[] )
{
//
TreeNode* t = (TreeNode*)malloc( sizeof(TreeNode) );
//
(*t).symbol = 'A' ; // === t -> symbol = 'A';
t -> frequency = 7 ;
t -> left = NULL ;
t -> right = NULL ;
//
//printf( "%c\n" , t->symbol ) ;
//printf( "%d\n" , t->frequency ) ;
//
int freq[256] = {0};
int numbytes = 0;
char ch;
FILE* fin = fopen("encodeIN.txt" , "r" ) ;
FILE* out = fopen("encodeOUT.txt", "w");
while (1)
{
numbytes = fread(&ch, sizeof(char), 1, fin);
if (numbytes == 0) break;
freq[(int)ch]++;
}
TreeNode* arr[256];
int i;
int j = 0;
for (i = 0; i < 256; i++)
{
if (freq[i] != 0)
{
ch = (char)i;
t = (TreeNode*)malloc( sizeof(TreeNode) );
//
(*t).symbol = ch ; // === t -> symbol = 'A';
t -> frequency = freq[i] ;
t -> left = NULL ;
t -> right = NULL ;
arr[j] = t;
j++;
}
}
sortArr(arr, j);
j--;
while (j > 0)
{
t = (TreeNode*)malloc( sizeof(TreeNode) );
t -> symbol = '*' ;
t -> frequency = (arr[j]->frequency + arr[j-1]->frequency) ;
t -> left = arr[j-1] ;
t -> right = arr[j] ;
arr[j-1] = t;
j--;
sortArr(arr, j);
}
printf( "%s\n", "outside");
fclose(fin);
return 0;
}
//
// end of file
//