-
Notifications
You must be signed in to change notification settings - Fork 0
/
myTrie.c
168 lines (150 loc) · 4.34 KB
/
myTrie.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
#ifndef _MY_TRIE_
#define _MY_TRIE_
#define NUM_LETTERS ((int)26)
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <math.h>
typedef enum
{ FALSE = 0, TRUE = 1 } boolean;
int num_heads = 0;
char *word;
typedef struct node
{
char letter;
long unsigned int count;
int is_end;
int num_child;
struct node *children[NUM_LETTERS];
} node;
int getletter (char c)
{
int numletter = -1;
if (c <= 'z' && c >= 'a')
numletter = c - 'a';
else if (c <= 'Z' && c >= 'A')
numletter = c - 'A';
else if (c == '\t' || c == '\n' || c == '\r' || c == ' ' || c=='\0' )
return -1;
else if(c==EOF)
return -3;
else
return -2;
return numletter;
}
node ** build_trie (node **first, int(*index)(char))
{
while (TRUE)
{
int c = (*index)(getchar());
if (c == -1 || c==-2){
continue;
}
else if(c==-3)
return first;
if (first[c] == NULL)
{
first[c] = (node *) calloc (1,sizeof (node));
first[c]->letter ='a'+c;
first[c]->num_child = 0;
first[c]->is_end = FALSE;
num_heads++;
}
node * temp = &(*(first[c]));
int c2 = (*index)(getchar());
while (c2 != -1)
{
if (c2 == -2) {
char let = getchar();
c2 = (*index)(let);
continue;
}
else if (c2==-3)
break;
if (temp->children[c2] == NULL)
{
temp->num_child++;
temp->children[c2] = (node *) calloc (1,sizeof (node));
temp->children[c2]->is_end = FALSE;
temp->children[c2]->letter = 'a'+c2;
}
temp = &(*(temp->children[c2]));
char let = getchar();
c2 = (*index)(let);
}
temp->count += 1;
temp->is_end = TRUE;
}
return first;
}
void printTrieUp (node ** head, int l, int childs)
{
int rest_child = childs;
for (int i = 0; i < NUM_LETTERS && rest_child > 0; i++)
{
if (head[i] != NULL)
{
if (childs-rest_child>0)
l--;
char c = head[i]->letter;
word=(char*)realloc(word,(l+1)*sizeof(char));
if(word==NULL)
exit(1);
word[l] = c;
l++;
rest_child--;
if (head[i]->is_end == TRUE)
printf ("%.*s %lu\n", l, word, head[i]->count);
if (head[i]->num_child > 0)
printTrieUp ((head[i]->children), l, head[i]->num_child);
free(head[i]);
}
}
}
void printTrieDown (node ** head, int l, int childs)
{
int rest_child = childs;
for (int i = NUM_LETTERS - 1; i >= 0 && rest_child > 0; i--)
{
if (head[i] != NULL)
{
if (childs - rest_child > 0)
l--;
char c = head[i]->letter;
word=(char*)realloc(word,(l+1)*sizeof(char));
if(word==NULL)
exit(1);
word[l] = c;
l++;
rest_child--;
if (head[i]->num_child > 0)
printTrieDown ((head[i]->children), l,head[i]->num_child);
if (head[i]->is_end == TRUE)
printf ("%.*s %lu\n", l, word, head[i]->count);
free(head[i]);
}
}
}
int main (int argc, char* argv[])
{
node ** first = (node **) calloc (NUM_LETTERS, sizeof (node *));
if (first == NULL )
{
printf ("error");
exit (1);
}
build_trie (first,&getletter);
word = (char *) malloc (sizeof(char));
if(word==NULL)
exit(1);
if(argc==1 )
printTrieUp (first, 0, num_heads);
else if(argc==2)
{
if(strcmp (argv[1], "r")==0)
printTrieDown(first, 0, num_heads);
}
free(word);
free (first);
}
#endif