-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cpp
110 lines (109 loc) · 2.62 KB
/
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
#include <vector>
#include <iostream>
#include <string>
using namespace std;
#define ALPHA 26
struct TrieNode
{
struct TrieNode *child[ALPHA];
bool isEndOfKey;
};
struct TrieNode *newNode()
{
struct TrieNode *pNode = new TrieNode;
pNode->isEndOfKey = false;
for (int i = 0; i < ALPHA; i++)
{
pNode->child[i] = nullptr;
}
return pNode;
}
void insert(struct TrieNode *root, string key)
{
struct TrieNode *node = root;
for (int i = 0; i < key.size(); i++)
{
int index = key[i] - 'a';
if (!node->child[index])
node->child[index] = newNode();
node = node->child[index];
}
node->isEndOfKey = true;
}
bool search(struct TrieNode *root, string key)
{
struct TrieNode *node = root;
for (int i = 0; i < key.size(); i++)
{
if (!node->child[key[i] - 'a'])
return false;
node = node->child[key[i] - 'a'];
}
return node->isEndOfKey;
}
bool isEmpty(struct TrieNode *root)
{
for (int i = 0; i < ALPHA; i++)
{
if (root->child[i])
return false;
}
return true;
}
TrieNode *deleteKey(struct TrieNode *root, string key, int depth = 0)
{
if (!root)
return nullptr;
if (depth == key.size())
{
if (root->isEndOfKey)
root->isEndOfKey = false;
if (isEmpty(root))
{
delete root;
root = nullptr;
}
return root;
}
int index = key[depth] - 'a';
root->child[index] = deleteKey(root->child[index], key, depth + 1);
if (isEmpty(root) && !root->isEndOfKey)
{
delete root;
root = nullptr;
}
return root;
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
vector<string> keys = {"hello", "mihir", "endsem", "boring", "life", "beru"};
struct TrieNode *root = newNode();
for (auto key : keys)
{
insert(root, key);
}
char decision = 'y';
do
{
cout << "Enter key to be searched: ";
string srch;
cin >> srch;
bool isPresent = search(root, srch);
if (isPresent)
{
cout << srch << " is Present in Trie\n";
}
else
{
cout << srch << " is NOT Present in Trie\n";
}
cout << "Search more? (y / any character) ";
cin >> decision;
} while (decision == 'y' || decision == 'Y');
deleteKey(root, "endsem");
search(root, "endsem") ? cout << "Present\n" : cout << "Not Present\n";
return 0;
}