-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
161 lines (156 loc) · 5.36 KB
/
Copy pathmain.cpp
File metadata and controls
161 lines (156 loc) · 5.36 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
#include <iostream>
#include <fstream>
#include <chrono>
#include "UnsortedArray.h"
#include "SortedArray.h"
#include "BinarySearchTree.h"
#include "HashTable.h"
#include "AVLTree.h"
using namespace std;
using namespace std::chrono;
int countWords(ifstream &ifs)
{
string temp;
int i = 0;
while (ifs >> temp)
{
i++;
}
return i;
}
bool deleteSpecialChars(string & temp)
{
int j;
for (j=0;j<temp.length();j++)
{
if ((temp[j] >= 'A' && temp[j] <= 'Z'))
temp[j]+=32;
if ((temp[j] < 'a' || temp[j] > 'z') && (temp[j] < 'A' || temp[j] > 'Z'))
{
temp.erase(temp.begin() + j);
j-=1;
}
}
if (!temp.empty())
return true;
else
return false;
}
int main()
{
HashTable HT;
BinarySearchTree BST;
SortedArray SA;
UnsortedArray UA;
AVLTree AVL;
string* a;
string* q;
string temp;
long long int i,count=0;
ifstream ifs;
ofstream ofs;
string filename="small-file.txt";
ifs.open(filename);
// Count the number of words in the next, taking into factor the words that might come from a possible split
if (ifs.is_open())
{
count = countWords(ifs);
ifs.close();
ifs.open(filename);
}
// Read the words and assign them to the string pointer.
if (ifs.is_open())
{
a = new string[count];
cout << "File opened.";
for (i = 0; ifs >> temp; i++)
{
// Check for special characters or split words
if (deleteSpecialChars(temp))
{
// If the string isn't empty after deleting all the characters assign it to the string pointer.
a[i] = temp;
} else
{
// If it's empty reduce the number of words by one.
i--;
}
}
auto start = high_resolution_clock::now();
count = i;
for (i = 0; i < count; i++)
{
SA.Insert(a[i]);
BST.Insert(a[i]);
AVL.Insert(a[i]);
HT.Insert(a[i]);
UA.Insert(a[i]);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
int searchsize = 1000;
(srand(time(nullptr)));
q = new string[searchsize];
for (i=0;i<searchsize;i++)
{
q[i]=a[rand()%count];
}
ofs.open("output5.txt");
if (ofs.is_open())
{
ofs << "The insertion took " << duration.count() << " milliseconds." << endl;
// Search in the unsorted array
start = high_resolution_clock::now();
for (i = 0; i < searchsize; i++)
{
if (UA.Search(q[i])!=-1)
ofs << "Found the word \"" << q[i] << "\" " << UA.getCount(q[i]) << " times in the unsorted array." << endl;
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
ofs << "The search of the unsorted array took " << duration.count() << " milliseconds." << endl;
// Search in the sorted array
start = high_resolution_clock::now();
for (i = 0; i < searchsize; i++)
{
if (SA.Search(q[i])!=-1)
ofs << "Found the word \"" << q[i] << "\" " << SA.getCount(q[i]) << " times in the sorted array." << endl;
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
ofs << "The search of the sorted array took " << duration.count() << " milliseconds." << endl;
// Search in the binary search tree array
start = high_resolution_clock::now();
for (i = 0; i < searchsize; i++)
{
if (BST.Search(q[i])!=-1)
ofs << "Found the word \"" << q[i] << "\" " << BST.getCount(q[i]) << " times in the BST." << endl;
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
ofs << "The search of the binary search tree took " << duration.count() << " milliseconds." << endl;
// Search in the AVL tree
start = high_resolution_clock::now();
for (i = 0; i < searchsize; i++)
{
if (AVL.Search(q[i])!=-1)
ofs << "Found the word \"" << q[i] << "\" " << AVL.getCount(q[i]) << " times in the AVL tree." << endl;
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
ofs << "The search of the AVL tree took " << duration.count() << " milliseconds." << endl;
// Search in the hash table
start = high_resolution_clock::now();
for (i = 0; i < searchsize; i++)
{
if (HT.Search(q[i])!=-1)
ofs << "Found the word \"" << q[i] << "\" " << HT.getCount(q[i]) << " times in the hash table." << endl;
}
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
ofs << "The search of the hashtable took " << duration.count() << " milliseconds." << endl;
}
}
else
cout << "File error.";
}