forked from BartoszMilewski/Okasaki
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
68 lines (61 loc) · 1.71 KB
/
Copy pathtrie.cpp
File metadata and controls
68 lines (61 loc) · 1.71 KB
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
#include<bits/stdc++.h>
#define fr(siz,i) for(int i=0;i<siz;i++)
#define frr(siz,i,a) for(int i=a;i<siz;i++)
#define ff(siz) for(int i=0;i<siz;i++)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair< ll , ll >
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<ll>
#define vvl vector< vl >
const int maxn=(int)(2e5+5);
const ll mod=(ll)(1e9+7);
//ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
struct trie_node{
char data;
bool isEnd;
map<char,trie_node*> children;
};
trie_node * newNode(char data,bool isEnd) {
trie_node* temp = new trie_node;
temp->data = data;
temp->isEnd = isEnd;
return temp;
}
bool insert(trie_node* head, string data) {
trie_node* currentNode = head;
bool wasThere = true;
for(int i=0;i<data.length();i++) {
if(currentNode->children[data[i] - 'a'] == NULL) {
bool isEnd = ( i == data.length()-1 );
trie_node* temp = newNode(data[i],isEnd);
currentNode->children[data[i] - 'a'] = temp;
wasThere = false;
}
currentNode = currentNode->children[data[i] - 'a'];
}
return wasThere;
}
bool find(trie_node* head, string data, bool prefix) {
trie_node* currentNode = head;
bool found = true;
for(int i=0;i<data.length();i++) {
if(currentNode->children[data[i] - 'a'] == NULL) {
return false;
}
currentNode = currentNode->children[data[i] - 'a'];
}
return prefix ? true : currentNode->isEnd ;
}
int main() {
trie_node * head = newNode('*',true);
cout<<insert(head,"sasta_achar");
cout<<insert(head,"sasta_achar");
cout<<insert(head,"sasta_pickle");
cout<<"string"<<find(head,"don",false);
cout<<"\nprefix don"<<find(head,"don",true);
return 0;
}