-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.c
209 lines (197 loc) · 6.27 KB
/
Trie.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (C) 2010 University of Tartu
* Authors: Reina Käärik, Siim Orasmaa, Kristo Tammeoja, Jaak Vilo
* Contact: siim . orasmaa {at} ut . ee
*
* This file is part of Generalized Edit Distance Tool.
*
* Generalized Edit Distance Tool is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Generalized Edit Distance Tool is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Generalized Edit Distance Tool.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "Trie.h"
// create new TrieNode
TrieNode *newTrieNode(wchar_t a){
TrieNode *newNode;
newNode = (TrieNode*)malloc(sizeof(TrieNode));
if(newNode == NULL)
abort();
newNode->label = a;
newNode->replacement = NULL;
newNode->nextNode = NULL;
newNode->prevNode = NULL;
newNode->rightNode = NULL;
return newNode;
}
// create new Trie
Trie *createTrie(){
Trie *trieRoot;
trieRoot = (Trie*)malloc(sizeof(Trie));
if(trieRoot == NULL)
abort();
trieRoot->firstNode = NULL;
return trieRoot;
}
// Create new EndNode
EndNode *newEndNode(wchar_t *repl, double weight){
EndNode *endNode;
endNode = (EndNode*)malloc(sizeof(EndNode));
if(endNode == NULL)
abort();
endNode->edit = (wchar_t*)copy_wchar_t(repl, wchar_len(repl));
endNode->value = weight;
endNode->nextEN = NULL;
return endNode;
}
// Add new replacement to the TrieNode
int addEndNode(TrieNode *node, wchar_t *string, double value){
EndNode *endN;
int i;
if(node->replacement == NULL){
node->replacement = newEndNode(string, value);
return 0;
}
endN = node->replacement;
while(1){
i = 0;
while(1){
if(string[i] == endN->edit[i]){
/* If replacement strings are matching, we will only
* preserve the lowest weight, as preserving the higher
* weight is unnecessary: while calculating edit distance,
* higher weight will be left out anyway.
*/
if(string[i] == L'\0'){
if(endN->value > value)
endN->value = value;
return 0;
}
/* not in end yet, compare the next letter */
i++;
}
// letters did not match
else break;
}
if(endN->nextEN == NULL) break;
else endN = endN->nextEN;
}
endN->nextEN = newEndNode(string, value);
return 0;
}
// Adds string1 to trie, while expanding only downwards (in depth) via nextNode links
int addToTrieDepth(TrieNode *node, wchar_t *string1, int strLen1, wchar_t *string2, double value){
TrieNode *tmp;
/* more than 1 letters to add */
if(strLen1 > 1){
node->nextNode = newTrieNode(*string1);
node->nextNode->prevNode = node;
return addToTrieDepth(node->nextNode, string1+1, strLen1-1, string2, value);
}
/* only 1 letter to add */
else{
tmp = newTrieNode(*string1);
tmp->prevNode = node;
node->nextNode = tmp;
tmp->replacement = newEndNode(string2, value);
}
return 0;
}
// Adds transformation (string1 => string2 : value) to the trie t
int addToTrie(Trie *t, wchar_t *string1, int strLen1, wchar_t *string2, double value){
TrieNode *tmp;
/* if the trie is empty ... */
if(t->firstNode == NULL){
t->firstNode = newTrieNode(*string1);
tmp = t->firstNode;
if((strLen1-1) == 0)
tmp->replacement = newEndNode(string2, value);
else
addToTrieDepth(tmp, string1+1, strLen1-1, string2, value);
return 0;
}
tmp = t->firstNode;
TrieNode *prev;
prev = NULL;
/* find common prefix from the trie */
while(strLen1 > 0){
/* if the left side already exists, add replacement to its ending node */
if(strLen1 == 1 && tmp->label == *string1){
addEndNode(tmp,string2, value);
return 0;
}
/* if the label matches, proceed to the next level in depth */
else if(tmp->label == *string1 && tmp->nextNode != NULL){
prev = tmp;
tmp = tmp->nextNode;
string1 = string1 + 1;
strLen1--;
}
/* if we are at the end of known prefix, expand */
else if(tmp->label == *string1 && tmp->nextNode == NULL){
addToTrieDepth(tmp, string1+1,strLen1-1, string2, value);
return 0;
}
/* if labels did not match, check the next node in the same depth level ...*/
else if(tmp->rightNode != NULL)
tmp = tmp->rightNode;
else break;
}
/* if common prefix was not found or was incomplete, a new branch must be started */
tmp->rightNode = newTrieNode(*string1);
tmp->rightNode->prevNode = prev;
tmp = tmp->rightNode;
if(strLen1 == 1)
addEndNode(tmp,string2, value);
else
addToTrieDepth(tmp, (string1+1),strLen1-1, string2, value);
return 0;
}
// Releases memory under endnode list
void freeEndNode(EndNode *endNode){
EndNode *tmp;
tmp = endNode;
while (tmp != NULL){
EndNode *next;
next = tmp->nextEN;
if (tmp->edit != NULL){
free(tmp->edit);
}
free(tmp);
tmp = next;
}
}
// Releases memory under trie node and all of its children & siblings
void freeTrieNode(TrieNode *node){
TrieNode *tmp;
tmp = node;
while (tmp != NULL){
if (tmp->replacement != NULL){
freeEndNode(tmp->replacement);
}
if (tmp->rightNode != NULL){
freeTrieNode(tmp->rightNode);
}
TrieNode *next;
next = tmp->nextNode;
free(tmp);
tmp = next;
}
}
// Releases memory under trie and all of its children
void freeTrie(Trie *trie){
if (trie->firstNode != NULL){
freeTrieNode(trie->firstNode);
}
free(trie);
}