-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cpp~
114 lines (107 loc) · 2.21 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
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
struct TrieNode {
bool leaf;
string word;
TrieNode *next[3];
TrieNode()
{
leaf=false;
word="";
next[0]=next[1]=next[2]=NULL;
}
};
TrieNode* insert_string(TrieNode *root, string s, string word)
{
if(root)
{
int i=0;
TrieNode *temp = root;
while(i<s.size() && temp->next[s[i]-'0'])
temp=temp->next[s[i++]-'0'];
temp->next[s[i]-'0'] = insert_string(NULL, s.substr(i+1), word);
return temp;
}
else
{
if(s[0]=='\0')
{
TrieNode *temp = new TrieNode;
temp->leaf=true;
temp->word=word;
return temp;
}
else
{
TrieNode *temp = new TrieNode;
temp->next[s[0]-'0'] = insert_string(NULL, s.substr(1), word);
return temp;
}
}
}
void trieTraversal(TrieNode *root, vector<string> &sol)
{
if(root)
{
cout<<"In Trie Traversal\n";
if(root->leaf)
{
sol.push_back(root->word);
cout<<root->word<<"\n";
}
else
{
trieTraversal(root->next[0], sol);
trieTraversal(root->next[1], sol);
trieTraversal(root->next[2], sol);
}
}
}
vector<string> findByPrefix(TrieNode *root, string s)
{
vector<string> sol;
int i;
for(i=0;i<s.size();++i)
{
if(root->next[s[i]-'0'])
root=root->next[s[i]-'0'];
else return sol;
}
trieTraversal(root,sol);
return sol;
}
TrieNode* insert_string_helper(TrieNode *root, string s)
{
string word= s + "2";
return insert_string(root, s, word);
}
void print_string(vector<string> &s)
{
for(int i=0;i<(int)s.size();++i)
cout<<s[i]<<"\n";
}
int main()
{
TrieNode *root = NULL;
root = insert_string_helper(root,"111000011");
root = insert_string_helper(root,"1101000100");
// cout<<root<<" "<<root->next[0]<<" "<<root->next[1]<<"\n";
vector<string> op1 = findByPrefix(root,"11");
print_string(op1);
/*root = insert_string_helper(root,"11");
root = insert_string_helper(root,"0100011");
root = insert_string_helper(root,"11001001");
root = insert_string_helper(root,"111000011");
root = insert_string_helper(root,"01011011");
vector<string> op1 = findByPrefix(root,"11");
vector<string> op2 = findByPrefix(root,"010");
vector<string> op3 = findByPrefix(root,"1111");
print_string(op1);
print_string(op2);
print_string(op3);
*/
return 0;
}