-
Notifications
You must be signed in to change notification settings - Fork 368
/
trie.c
184 lines (145 loc) · 4.07 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
// Path: C/Trees/trie.c
// C program to implement trie.
// A prefix trie is an ordered tree data structure used in the representation of a set of strings over a finite alphabet set,
// which allows efficient storage of words with common prefixes.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// Trie node
struct TrieNode
{
// 26 lowercase alphabets
struct TrieNode *children[26];
bool isEndOfWord;
};
// Creates a new Trie node
struct TrieNode *createNode()
{
struct TrieNode *newNode = (struct TrieNode *)malloc(sizeof(struct TrieNode));
newNode->isEndOfWord = false;
for (int i = 0; i < 26; i++)
{
newNode->children[i] = NULL;
}
return newNode;
}
// Inserts a word into the Trie
void insert(struct TrieNode *root, const char *word)
{
struct TrieNode *currentNode = root;
for (int i = 0; word[i] != '\0'; i++)
{
int ind = word[i] - 'a';
if (currentNode->children[ind] == NULL)
{
currentNode->children[ind] = createNode();
}
currentNode = currentNode->children[ind];
}
currentNode->isEndOfWord = true;
}
// Searches for a word in the Trie
bool search(struct TrieNode *root, const char *word)
{
struct TrieNode *currentNode = root;
for (int i = 0; word[i] != '\0'; i++)
{
int ind = word[i] - 'a';
if (currentNode->children[ind] == NULL)
{
return false;
}
currentNode = currentNode->children[ind];
}
return (currentNode != NULL && currentNode->isEndOfWord);
}
// Checks if a Trie node has any children
bool hasChildren(struct TrieNode *node)
{
for (int i = 0; i < 26; i++)
{
if (node->children[i] != NULL)
{
return true;
}
}
return false;
}
// Deletes a word from the Trie
bool deleteWord(struct TrieNode *root, const char *word, int level)
{
if (root == NULL)
{
return false;
}
if (word[level] == '\0')
{
if (root->isEndOfWord)
{
root->isEndOfWord = false;
if (!hasChildren(root))
{
free(root);
root = NULL;
}
return true;
}
return false;
}
int ind = word[level] - 'a';
bool deleted = deleteWord(root->children[ind], word, level + 1);
if (deleted && !hasChildren(root->children[ind]))
{
free(root->children[ind]);
root->children[ind] = NULL;
}
return deleted;
}
// Deletes the entire Trie
void deleteTrie(struct TrieNode *root)
{
if (root == NULL)
{
return;
}
for (int i = 0; i < 26; i++)
{
if (root->children[i] != NULL)
{
deleteTrie(root->children[i]);
}
}
free(root);
}
// Program starts here
int main()
{
struct TrieNode *root = createNode();
// Inserting words into the Trie
insert(root, "apple");
insert(root, "banana");
insert(root, "car");
insert(root, "cat");
insert(root, "cart");
insert(root, "candid");
printf("Words: apple, banana, car, cat, cart, candid are Inserted.\n\n");
printf("Search results:\n");
// Searching for words in the Trie
printf("apple: %s\n", search(root, "apple") ? "Found" : "Not found");
printf("banana: %s\n", search(root, "banana") ? "Found" : "Not found");
printf("care: %s\n", search(root, "care") ? "Found" : "Not found");
printf("cat: %s\n", search(root, "cat") ? "Found" : "Not found");
printf("cart: %s\n", search(root, "cart") ? "Found" : "Not found");
printf("dog: %s\n", search(root, "dog") ? "Found" : "Not found");
// Deleting words from the Trie
deleteWord(root, "banana", 0);
printf("\nAfter deleting 'banana':\n");
printf("banana: %s\n", search(root, "banana") ? "Found" : "Not found");
deleteWord(root, "car", 0);
printf("\nAfter deleting 'car':\n");
printf("car: %s\n", search(root, "car") ? "Found" : "Not found");
printf("cart: %s\n", search(root, "cart") ? "Found" : "Not found");
// Deleting the entire Trie
deleteTrie(root);
return 0;
}