-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cc
130 lines (112 loc) · 2.96 KB
/
trie.cc
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
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
#define DIGITS_SIZE 10
struct Node {
bool is_end;
int prefix_count;
struct Node* child[DIGITS_SIZE];
Node() : prefix_count(0), is_end(false)
{}
};
class Trie {
public:
int _elements;
int numFoundElements;
int numNotFoundElements;
double timeTotal;
double timeInsert;
double timeFind;
Trie() {
head = new Node();
_elements = 0;
numFoundElements = 0;
numNotFoundElements = 0;
timeTotal = 0.0;
timeInsert = 0.0;
timeTotal = 0.0;
}
bool find(unsigned int k) {
double t1 = clock();
bool f = find_trie(k);
double t2 = (clock() - t1)/double(CLOCKS_PER_SEC);
timeTotal += t2;
timeFind += t2;
return f;
}
void insert(unsigned int k) {
double t1 = clock();
insert_trie(k);
double t2 = (clock() - t1)/double(CLOCKS_PER_SEC);
timeTotal += t2;
timeInsert += t2;
}
void printResults() {
cout << endl << endl << " ---- Trie Results ----" << endl << endl; cout << "find(k): average search time:\t" << double(timeTotal)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): total search time:\t" << timeFind << endl;
cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
cout << "insert(k): average insertion time:\t" << double(timeTotal)/(_elements) << endl;
cout << "insert(k): total insertion time:\t" << timeInsert << endl;
cout << "insert(k): number of elements:\t" << _elements << endl;
}
private:
Node *head;
void insert_trie(unsigned int k) {
string word = to_string(k);
Node *current = head;
current -> prefix_count++;
bool isNewElement = false;
for (int i = 0; i < word.length(); ++i) {
int digit = (int)word[i] - (int)'0';
if (current -> child[digit] == NULL) {
isNewElement = true;
current -> child[digit] = new Node();
}
current -> child[digit] -> prefix_count++;
current = current -> child[digit];
}
if (isNewElement) ++_elements;
current -> is_end = true;
}
bool find_trie(unsigned int k) {
string word = to_string(k);
Node *current = head;
for (int i = 0; i < word.length(); ++i) {
int digit = (int)word[i] - (int)'0';
if (current -> child[digit] == NULL) {
++numNotFoundElements;
return false;
}
current = current -> child[digit];
}
if (current -> is_end) ++numFoundElements;
else ++numNotFoundElements;
return current -> is_end;
}
};
int main(int argc, char* argv[]) {
string dict_file, query_file;
if (argc != 3) {
cout << "Usage: dict_file query_file" << endl;
return 0;
} else {
dict_file = argv[1];
query_file = argv[2];
}
cout << "Diccionari:\t" + dict_file << endl;
cout << "Queries:\t" + query_file << endl;
Trie t;
fstream dict(dict_file, ios_base::in);
unsigned int a;
while (dict >> a) {
t.insert(a);
}
fstream query(query_file, ios_base::in);
while (query >> a) {
t.find(a);
}
t.printResults();
}