-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_function.cpp
More file actions
202 lines (149 loc) · 4.3 KB
/
trie_function.cpp
File metadata and controls
202 lines (149 loc) · 4.3 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
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
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define ll long long
#define node_size 256
fstream file,file_link;
vector <string> prefix;
class node_link{
public:
char longitude[25], latitude[25];
char id[15];
char admin_level[3];
ll next;
node_link()
{
next=-1;
}
};
class node{
public :
ll ptr[256];
ll nodePtr,nodePtr_end;
node()
{
for(int i=0;i<256;i++)
{ptr[i]=-1;}
nodePtr=-1;
nodePtr_end=-1;
}
};
void display_prefix(string s,int ind,ll curr_root){
if( ind>0 ){
prefix.clear();
ll curr_child=0;
for(int i=0;i<s.length();i++){
file.seekp(curr_root);file.seekg(curr_root);
node root;
file.read((char*)&root,sizeof(node));
if(root.ptr[s[i]-0]==-1 ){
return ;
}
else{
curr_child=root.ptr[s[i]-0];
}
curr_root=curr_child;
ind--;
// cout<<i<<":i "<<root.nodePtr<<" "<<ind<<endl;;
}
}
//cout<<s<<endl;
file.seekp(curr_root);file.seekg(curr_root);
node root;
file.read((char*)&root,sizeof(node));
if(root.nodePtr!=-1){
// prefix.push_back(s);
file_link.seekp(root.nodePtr);file_link.seekg(root.nodePtr);
node_link link_list;
// cout<<root.nodePtr<<" "<<root.nodePtr_end<<endl;;
while(1){
file_link.read((char*)&link_list,sizeof(node_link));
prefix.push_back(s);
if( link_list.next==-1 || abs(link_list.next)>30000000){break;}
// cout<<link_list.next<<endl;
file_link.seekp(link_list.next);file_link.seekg(link_list.next);
}
}
for(int i=0;i<node_size;i++){
if(root.ptr[i]==-1 ){
//return ;
}
else{
string si=s;
si+=(char)(0+i);
display_prefix(si,0,root.ptr[i] );
}
}
return;
}
node search_trie(string s){
ll curr_child=0,curr_root=0;
for(int i=0;i<s.length();i++){
file.seekp(curr_root);file.seekg(curr_root);
node root;
file.read((char*)&root,sizeof(node));
cout<<root.ptr[s[i]-0]<<endl;
if(root.ptr[s[i]-0]==-1 ){
node x;
return x;
}
else{
curr_child=root.ptr[s[i]-0];
if(i==s.length()-1){
node child;
file.seekp(curr_child);file.seekg(curr_child);
file.read((char*)&child,sizeof(node));
return child;
}
}
curr_root=curr_child;
}
}
vector <string> id_list;
vector <string> long_list;
vector <string> lat_list;
void node_with_nodeid(ll curr_root){
id_list.clear();
lat_list.clear();
file_link.seekp(curr_root);file_link.seekg(curr_root);
node_link link_list;
//cout<<root.nodePtr<<" "<<root.nodePtr_end<<endl;
while(1){
file_link.read((char*)&link_list,sizeof(node_link));
string s(link_list.id);
id_list.push_back(s);
string lo(link_list.longitude);
string la(link_list.latitude);
lat_list.push_back(la);
lat_list.push_back(lo);
if( link_list.next==-1){break;}
cout<<link_list.next<<endl;
file_link.seekp(link_list.next);file_link.seekg(link_list.next);
}
return ;
}
int main(){
long long curr_child=0,curr_root=0,m=0;
file.open("trie.txt",ios::app|ios::in|ios::out);
file_link.open("trie_link.txt",ios::app|ios::in|ios::out);
curr_root=0;
file.seekp(0,ios::end);
curr_child=file.tellp();
file.seekp(curr_root);file.seekg(curr_root);
// search
cout<<endl<<endl;
node x= search_trie("Modinagar");
cout<<x.nodePtr<<endl;
string temp="Mo"; //prefix search
display_prefix(temp,temp.length(),0);
for(int i=0;i<prefix.size();i++){
cout<<prefix[i]<<" "<<endl;
}
node_with_nodeid(x.nodePtr);
for(int i=0;i<lat_list.size();i++){
cout<<lat_list[i]<<" "<<endl;
}
file.close();
file_link.close();
return 0;
}