-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrie.cpp
234 lines (201 loc) · 5.07 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Value
{
string key;
int gridx;
int gridy;
string color;
int val1;
float val2;
};
struct trieNode {
trieNode* child[26];
bool endKey;
Value* record;
trieNode() : endKey(false), record(nullptr) { //constructor for the function
for (int i=0;i<26;i++){ //everytime it makes a new trieNode, this code happens
child[i]=nullptr;
}
}
};
class Data
{
public:
Data();
Data(int sizeIn);
void Add(Value* record);
void Delete(string key);
void LookUp(string key);
void numberOfKey();
private:
int keyCount;
trieNode* trieTable;
void trieAdd(Value* record);
void trieDelete(string key);
Value trieLookUp(string key);
void trieNumberOfKey();
};
//constructor
Data::Data()
{
keyCount=0;
trieTable = new trieNode();
}
// public
void Data::Add(Value* record)
{
trieAdd(record);
}
void Data::Delete(string key)
{
trieDelete(key);
}
void Data::LookUp(string key)
{
Value record = trieLookUp(key);
if (!record.key.empty())
{
cout << "Key (" << key <<") found!" << endl;
cout << " gridx: " << record.gridx << endl;
cout << " gridy: " << record.gridy << endl;
cout << " color: " << record.color << endl;
cout << " val1: " << record.val1 << endl;
cout << " val2: " << record.val2 << endl;
}
else
cout << "Key (" << key <<") not found!" << endl;
}
void Data::numberOfKey()
{
trieNumberOfKey();
}
// private
void Data::trieAdd(Value* record)
{
trieNode* current = trieTable;
for (char c : record->key){
if (current->child[c-'a']==nullptr){ //"c" is the character, - 'a' gets you the askii values.
trieNode* newNode = new trieNode();
current->child[c-'a']=newNode;
}
current=current->child[c-'a'];
}
if (current->endKey==true){ //if a record already exists
current->record=record;
} else {
current->record=record;
keyCount++;
current->endKey=true;
}
}
void Data::trieDelete(string key)
{
trieNode* current = trieTable;
int i=0;
for (char c : key){
if (current->child[c-'a']==nullptr){
cout << "Key ("<< key << ") not found in list" << endl;
return;
}
current=current->child[c-'a'];
}
if (current->endKey==false){
cout << "Key ("<< key << ") not found in list" << endl;
} else {
cout << "Key (" << key << ") removed from list" << endl;
current->endKey=false;
delete current->record;
current->record=nullptr;
keyCount--;
}
}
Value Data::trieLookUp(string key)
{
trieNode* current = trieTable;
for (char c : key){
if (current->child[c-'a']==nullptr){
return {};
}
current=current->child[c-'a'];
}
if (current->endKey==true){
return *current->record;
} else {
return {};
}
}
void Data::trieNumberOfKey()
{
cout << "Total key entry: " << keyCount << endl;
}
int main(int argc, char *argv[])
{
ifstream file;
if (argc < 2)
{
cout << "Error: file not provided!" << endl;
return 1;
}
file.open(argv[1]);
Data dataObject;
string key;
string data;
while(getline(file, key, ','))
{
Value* newValue = new Value();
newValue->key=key;
getline(file, data, ',');
newValue->gridx = stoi(data);
getline(file, data, ',');
newValue->gridy = stoi(data);
getline(file, newValue->color, ',');
getline(file, data, ',');
newValue->val1 = stoi(data);
getline(file, data);
newValue->val2 = stof(data);
dataObject.Add(newValue);
}
file.close();
dataObject.numberOfKey();
// Action file if provided
if (argc == 3)
{
file.open(argv[2]);
string action;
while(getline(file, action, ' '))
{
Value* newValue = new Value();
if (action == "add")
{
getline(file, data, ',');
newValue->key = data;
getline(file, data, ',');
newValue->gridx = stoi(data);
getline(file, data, ',');
newValue->gridy = stoi(data);
getline(file, newValue->color, ',');
getline(file, data, ',');
newValue->val1 = stoi(data);
getline(file, data);
newValue->val2 = stof(data);
dataObject.Add(newValue);
}
else if (action == "lookup")
{
getline(file, data);
dataObject.LookUp(data);
}
else if (action == "delete")
{
getline(file, data);
dataObject.Delete(data);
}
}
file.close();
dataObject.numberOfKey();
}
return 0;
}