-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie2.c
119 lines (107 loc) · 2.73 KB
/
trie2.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<stdio.h>
#include<stdlib.h>
#define ALPHABET_SIZE 26
struct TrieNode{
struct TrieNode *children[100];
int isEndOfWord;
};
struct TrieNode* createNode(){
struct TrieNode *newNode = (struct TrieNode*)malloc(sizeof(struct TrieNode));
for (int i = 0; i < ALPHABET_SIZE; i++) {
newNode->children[i] = NULL;
}
newNode->isEndOfWord = 0;
}
void insert(struct TrieNode *root, const char* word){
struct TrieNode *current = root;
while(*word){
int index = *word - 'a';
if(!current->children[index]){
current->children[index] = createNode();
}
current = current->children[index];
word++;
}
current->isEndOfWord = 1;
}
int isLeafNode(struct TrieNode *node){
for (int i = 0; i < ALPHABET_SIZE; i++) {
if(node->children[i]){
return 0;
}
}
return 1;
}
int delete(struct TrieNode *root, const char* word){
struct TrieNode *current = root;
struct TrieNode *path[100];
int pathIndex = 0;
while(*word){
int index = *word - 'a';
if(!current->children[index]){
return 0;
}
path[pathIndex++] = current;
current = current->children[index];
word++;
}
if(!current->isEndOfWord){
return 0;
}
current->isEndOfWord = 0;
if(isLeafNode(current)){
for (int i = pathIndex-1; i >=0; i--) {
int index = word[i] - 'a';
free(path[i]->children[index]);
path[i]->children[index] = NULL;
if(!isLeafNode(path[i])){
break;
}
}
}
return 1;
}
int main(){
struct TrieNode *root = createNode();
int op;
while(op!=3){
printf("1.Insert\n2.Delete\n3.Exit\nEnter your operation: ");
scanf("%d",&op);
if(op==1){
printf("Enter the word to insert: ");
char word[100];
scanf("%s",word);
insert(root,word);
}
else if(op==2){
printf("Enter the word to delete: ");
char word_delete[100];
scanf("%s",word_delete);
printf("Delete %s: %s",word_delete,delete(root, word_delete)?"Success":"Not Found");
}
else if(op==3){
break;
}
}
}
// 1.Insert
// 2.Delete
// 3.Exit
// Enter your operation: 1
// Enter the word to insert: apple
// 1.Insert
// 2.Delete
// 3.Exit
// Enter your operation: 1
// Enter the word to insert: mango
// 1.Insert
// 2.Delete
// 3.Exit
// Enter your operation: 2
// Enter the word to delete: app
// Delete app: Not Found1.Insert
// 2.Delete
// 3.Exit
// Enter your operation: 2
// Enter the word to delete: apple
// Delete apple: Success1.Insert