-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
107 lines (91 loc) · 4.44 KB
/
main.cpp
File metadata and controls
107 lines (91 loc) · 4.44 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
#include <iostream>
#include <fstream>
#include <chrono>
#include "UnsortedArray.h"
#include "SortedArray.h"
#include "BinarySearchTree.h"
#include "AVLTree.h"
#include "HashTable.h"
using namespace std;
using namespace std::chrono;
//Μέθοδος που μετράει το χρόνο που χρειάζεται για την αναζήτηση λέξεων σε κάθε δομή δεδομένων
//Χρήση βιβλιοθήκης <chrono>.
template<class X>
void timeNeededForSearch(string *q, X dataStructure, string structure, int numberOfWords) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < numberOfWords; i++) {
int times = dataStructure->search(q[i]);
if (times == 1) {
cout << "The word: '" << q[i] << "' is shown once in " << structure << endl;
continue;
} else {
cout << "The word: '" << q[i] << "' is shown " << times << " times in " << structure << endl;
}
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << endl << "The time needed for the search in " << structure << " was: " << duration.count() << " microseconds" << endl;
}
//Μέθοδος που μετράει το χρόνο που χρειάζεται για την αναζήτηση λέξεων στη δομή AVLTree
void timeForAVL(string *q, AVLTree dataStructure, int numberOfWords, Node *root){
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < numberOfWords; i++) {
int times = dataStructure.search(root, q[i]);
if (times == 1) {
cout << "The word: '" << q[i] << "' is shown once in AVL Tree" << endl;
continue;
} else {
cout << "The word: '" << q[i] << "' is shown " << times << " times in AVL Tree "<< endl;
}
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << endl << "The time needed for the search in AVL Tree was: " << duration.count() << " microseconds" << endl;
}
int main() {
string filename = "small-file.txt"; //ονομα αρχειου αναγνωσης
string word; //κάθε λέξη του αρχείου
fstream file;
file.open(filename.c_str());
string *q = new string[1000]; //Σύνολο Q τυχαίων λέξεων που ζητείται
int qIndex = 0;
Node *rt = nullptr; //κόμβος-ρίζα για τη δομή AVL Tree
//Δημιουργία κάθε δομής δεδομένων
UnsortedArray *Array = new UnsortedArray();
SortedArray *sArray= new SortedArray();
BinarySearchTree *BST = new BinarySearchTree();
AVLTree *AVL = new AVLTree();
HashTable *HASH = new HashTable();
//διάβασμα κάθε λέξης από το αρχείο και μετατροπή της σε κατάλληλη μορφή
while (file >> word) {
int len = word.size();
//βγάζουμε τα σημεία στίξης
for (int i = 0; i < len; i++) {
word[i] = tolower(word[i]); //μετατροπή όλων των γραμμάτων σε πεζά
if (!(word[i] >= 'a' && word[i] <= 'z')) {
word.erase(i--, 1);
len = word.size();
}
}
//δημιουργία συνόλου Q
if (rand() % 2 && qIndex < 1000) {
q[qIndex] = word;
qIndex++;
}
//εισαγωγή λέξεων από το αρχείο στις 5 δομές
Array->insert(word);
sArray->insert(word);
BST->insert(word);
rt = AVL->insert(rt,word);
HASH->insert(word);
}
//Αναζήτηση όλων των λέξεων του συνόλου Q σε κάθε δομή. Εμφαζίνεται ο χρόνος που χρειάστηκε για την εκτέλεση
//αναζήτησης της κάθε δομής & πόσες φορές εμφανίζεται η κάθε λέξη
timeNeededForSearch(q, Array, "Unsorted Array", qIndex); //UnsortedArray
timeNeededForSearch(q, sArray, "Sorted Array", qIndex); //SortedArray
timeNeededForSearch(q, BST, "Binary Search Tree", qIndex); //BinarySearchTree
timeForAVL(q, *AVL, qIndex, rt); //AVLTree
timeNeededForSearch(q, HASH, "HashTable", qIndex); //HashTable
file.close(); //κλέισιμο αρχείου
return 0;
}