-
Notifications
You must be signed in to change notification settings - Fork 497
/
trie.cpp
130 lines (115 loc) · 3.51 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <cstring>
using namespace std;
// Determine size of array
#define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
// Alphabet size (# of symbols)
#define ALPHABET_SIZE 26
// Convert char to int value
// only 'a' to 'z' are allowed chars
#define CHAR_TO_INT(c) ((int)c - (int)'a')
// TrieNode class
class TrieNode {
private:
// Children of every Node
class TrieNode *children[ALPHABET_SIZE];
// Status of Leaf Node
bool isLeaf;
public:
// Default Constructor
TrieNode() {
this->isLeaf = false;
for (int i = 0; i < ALPHABET_SIZE; ++i)
this->children[i] = NULL;
}
// Get leaf node status
bool get_leaf_status() { return isLeaf; }
// Set leaf node status
void set_leaf_status(bool status) { this->isLeaf = status; }
// Get Child status
// Check if it is null or not
bool get_child_status(int index) {
if (!(this->children[index]))
return true;
return false;
}
// Initialize the child node, at specific index
void initialize_child(int index) {
this->children[index] = new TrieNode();
}
// Get child node at specific index
class TrieNode *get_children(int index) {
return this->children[index];
}
// Insert into the Trie, Implemetation is below
void insert(string key);
// Search into the Trie, Implementation is below
bool search(string key);
};
/* Insert into the Trie
* args :
* key : String to insert into Trie
* Time Complexity: O(len(key))
*/
void TrieNode::insert(string key) {
// Determines the length of the key
int length = key.length();
// Temp variable for Crawling into the Trie
class TrieNode *pCrawl = this;
for (int level = 0; level < length; ++level) {
// determine the index of the child node to use
int index = CHAR_TO_INT(key[level]);
/* Check status of that child node
* If it is empty, them fill it
* If it is present, them use this as the next root
*/
if (pCrawl->get_child_status(index))
pCrawl->initialize_child(index);
pCrawl = pCrawl->get_children(index);
}
// set the last node status as leaf node
pCrawl->set_leaf_status(true);
};
/* Searches into the Trie
* args:
* key : string to search into Trie
* Time complexity : O(len(key))
*/
bool TrieNode::search(string key) {
// determines the length of the key
int length = key.length();
// Temp variable for crawling into the Trie
class TrieNode *pCrawl = this;
for (int level = 0; level < length; ++level) {
// determine index of the child node to use
int index = CHAR_TO_INT(key[level]);
/* Check status of the child node
* If it is empty, them return false i.e. key not present into Trie
* If it is present, them start crawling from that node
*/
if (pCrawl->get_child_status(index))
return false;
pCrawl = pCrawl->get_children(index);
}
// Check that the last node reached is leaf node and not null as well
return (pCrawl != NULL && pCrawl->get_leaf_status());
};
// Driver function
int main() {
// keys to insert into Trie
char keys[][8] = {"the", "a", "there", "answer", "any",
"by", "bye", "their"};
// Output the status of key
char output[][32] = {"Not present in trie", "Present in trie"};
// Root of Trie (Crawling starts from root, insertion as well)
class TrieNode *root = new TrieNode();
// Insertion into Trie
for (unsigned long i = 0; i < ARRAY_SIZE(keys); ++i)
root->insert(keys[i]);
// Output of searched keys
cout << output[root->search("the")] << endl;
cout << output[root->search("these")] << endl;
cout << output[root->search("thaw")] << endl;
cout << output[root->search("their")] << endl;
return 0;
}