-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
executable file
·119 lines (98 loc) · 2.05 KB
/
helpers.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
#include"structs.h"
#include<stdio.h>
FNODE *makefnode(float pr,char ch)
{
FNODE *t = (FNODE *)malloc(sizeof(FNODE));
t->prob = pr;
t->symbol = ch;
t->father = NULL;
t->left = NULL;
t->right = NULL;
return t;
}
QNODE *findMin(QNODE *l,QNODE *r)
{
QNODE *mid = NULL;
QNODE *t = l;
float s1=0,s2=0;
s1 = l->fnptr->prob;
t = l->next;
while(t != r->next)
{
s1 -= t->fnptr->prob;
t = t->next;
}
mid = l;
t = l->next;
while(t != r)
{
s2 = s1 + 2*(t->fnptr->prob);
if(mod(s2) <= mod(s1)) mid = t;
//else return mid;
s1 = s2;
t = t->next;
}
return mid;
}
void saveWord(int pre,FNODE *rt)
{
if(pre == 1)
{
cbook[(int)rt->symbol] = (char *)malloc(2*sizeof(char));
cbook[(int)rt->symbol][0] = '0';
cbook[(int)rt->symbol][1] = '\0';
return;
}
char tem[MAX_CODE_LENGTH] = "";
sprintf(tem,"%d",pre);
cbook[(int)rt->symbol] = (char *)malloc(sizeof(char)*(strlen(tem)+1));
int i;
for(i = 1;i < strlen(tem);i++) cbook[(int)rt->symbol][i-1] = tem[i];
cbook[(int)rt->symbol][i-1] = '\0';
printf("sym is %c with code %s\n",rt->symbol,cbook[(int)rt->symbol]);
return;
}
float mod(float n)
{
if(n >= 0) return n;
else return -1*n;
}
float sqr(float n)
{
return n*n;
}
void clrmem()
{
int i;
for(i = 0;i < 257;i++)
if(cbook[i] != NULL)
free(cbook[i]);
clrTree(root);
QNODEPTR t = q1.front;
clrqueue(t);
return;
}
void clrqueue(QNODEPTR t)
{
//QNODEPTR t = q.front;
if(t) return;
clrqueue(t->next);
free(t);
}
void clrTree(FNODEPTR r)
{
if(r == NULL)
return;
else if(r->left != NULL){
clrTree(r->left);
r->left = NULL;
}
else if(root->right != NULL){
clrTree(r->right);
r->right = NULL;
}
else{
free(r);
return;
}
}