-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHash_Trie.cpp
229 lines (215 loc) · 5.05 KB
/
Hash_Trie.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
struct TrieNode
{
bool isfinal;
char *data;
int childs;
TrieNode *nxt[256];
};
struct Trie
{
TrieNode *root;
}*pTrie;
class HashTrie
{
public:
char *crt_data;
int crt_data_len;
char *crt_search_data;
void trie_init_node(TrieNode *node);
Trie *trie_new();
void trie_delete(Trie *trie);
void trie_node_free(TrieNode *node);
void trie_del_data(Trie *trie, char *str);
int trie_add_nodes(TrieNode *node, char *str, int pos);
int trie_add(Trie *trie, char *str, char *data, int data_len);
void del_data(TrieNode *node, char *str, int pos);
int trie_search_node(TrieNode *node, char *str, int pos);
void trie_search(Trie *trie, char *str);
};
void HashTrie::trie_init_node(TrieNode *node)
{
for (int i = 0; i < 256; i++)
{
node->nxt[i] = 0;
}
node->childs = 0;
node->isfinal = 0;
node->data = 0;
}
Trie *HashTrie::trie_new()
{
Trie *trie = new Trie;
trie->root = new TrieNode;
trie_init_node(trie->root);
return trie;
}
void HashTrie::trie_node_free(TrieNode *node)
{
for (int i = 0; i < 256; i++)
{
if (node->nxt[i])
trie_node_free(node->nxt[i]);
}
free(node->data);
free(node);
}
void HashTrie::trie_delete(Trie *trie)
{
trie_node_free(trie->root);
free(trie);
}
int HashTrie::trie_add_nodes(TrieNode *node, char *str, int pos)
{
char ch = str[pos];
if (ch)
{
cout<<node->nxt[ch]<<endl;
if (node->nxt[ch] == 0)
{
node->nxt[ch] = new TrieNode;
if (node->nxt[ch] == NULL)
return 0;
trie_init_node(node->nxt[ch]);
node->childs++;
}
return trie_add_nodes(node->nxt[ch], str, pos + 1);
}
else
{
if (node->isfinal)
free(node->data);
node->isfinal = 1;
if (crt_data && crt_data_len)
{
node->data = new char [crt_data_len];
if (node->data == 0)
return 0;
node->data = crt_data;
}
else
{
node->data = NULL;
}
return 1;
}
}
int HashTrie::trie_add(Trie *trie, char *str, char *data, int data_len)
{
crt_data = data;
crt_data_len = data_len;
return trie_add_nodes(trie->root, str, 0);
}
void HashTrie::del_data(TrieNode *node, char *str, int pos)
{
char ch = str[pos];
if (ch)
{
if (node->nxt[ch])
{
del_data(node->nxt[ch], str, pos + 1);
if (node->nxt[ch]->isfinal == 0 && node->nxt[ch]->childs == 0)
{
free(node->nxt[ch]);
node->nxt[ch] = 0;
node->childs--;
}
}
}
else
{
if (node->isfinal)
{
node->isfinal = 0;
free(node->data);
node->data = 0;
}
}
}
void HashTrie::trie_del_data(Trie *trie, char *str)
{
del_data(trie->root, str, 0);
}
int HashTrie::trie_search_node(TrieNode *node, char *str, int pos)
{
char ch = str[pos];
if (ch)
{
if (node->nxt[ch])
return trie_search_node(node->nxt[ch], str, pos + 1);
else
return 0;
}
else
{
if (node->isfinal)
{
crt_search_data = node->data;
cout<<"Element found at string "<<str<<": ";
cout<<crt_search_data<<endl;
return 1;
}
else
return 0;
}
}
void HashTrie::trie_search(Trie *trie, char *str)
{
crt_search_data = 0;
if (trie_search_node(trie->root, str, 0))
{
return;
}
else
cout<<"Element not found in the trie"<<endl;
}
int main()
{
char data[100],str[100];
HashTrie ht;
Trie *root = ht.trie_new();
int len;
int choice;
while(1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Trie"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"1.Insert element into the Trie"<<endl;
cout<<"2.Search element from the Trie"<<endl;
cout<<"3.Delete element"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter data string to be inserted: ";
scanf("%s",data);
len = strlen(data);
cout<<"Enter key string at which data is inserted: ";
scanf("%s",str);
ht.trie_add(root, str, data, len);
break;
case 2:
cout<<"Enter key string at which data is searched: ";
scanf("%s",str);
ht.trie_search(root, str);
break;
case 3:
cout<<"Enter key string at which data to be deleted: ";
scanf("%s",str);
ht.trie_del_data(root, str);
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
break;
}
}
}