-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTDriver.cpp
97 lines (86 loc) · 3.08 KB
/
BSTDriver.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
#include <iostream>
#include <fstream>
#include <chrono>
#include <iomanip>
#include "BST.hpp"
using namespace std;
int main(int argc, char* argv[])
{
int testData[40000]; // all data will be stored in this array
float insert[400];
float search[400];
BST tree;
//search ... filling up arrays
srand(time(nullptr));
int values[100];
int searched[100];
int total = 100;
int trials = 0;
int count = 0;
if(argc < 2) // if there are less than 2 arguments (if no file name is specified)
{
cout << "Failed to open the file.";
return 0;
}
string filename = argv[1]; // the user will enter the file name after writing the ./a.out command on the terminal
ifstream myfile(filename); // creates an input file stream and opens the file
if(!myfile.is_open()) // if the file won't open
{
cout << "Failed to open the file.";
return 0;
}
else
{
string line = "";
auto startTimeInsert = chrono::high_resolution_clock::now();
for(int i = 0; i < 40000; i++) // loops through the array
{
getline(myfile,line,',');
testData[i] = stoi(line);
tree.addNode(testData[i]); // creating tree
if(i % 100 == 99); // 100 elements have been inserted
{
auto endTimeInsert = chrono::high_resolution_clock::now(); //marks end time after 100 elements have been added
double insertTime = (chrono::duration_cast<chrono::nanoseconds>(endTimeInsert-startTimeInsert).count()) * 1e-2;//(float)(endTimeInsert - startTimeInsert);
insert[count] = insertTime;
for(int j = 0; j < 100; j++)
{
values[j] = rand() % total; // values 0 - total
}
auto startTimeSearch = chrono::high_resolution_clock::now();
for(int j = 0; j < 100; j++)
{
tree.searchKey(values[j]);
}
auto endTimeSearch = chrono::high_resolution_clock::now();
double searchTime = (chrono::duration_cast<chrono::nanoseconds>(endTimeSearch-startTimeSearch).count()) * 1e-2;
// if(count < 40000)
// {
// cout << insertTime << endl;
// }
// double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
total += 100;
search[count] = searchTime;
count++;
startTimeInsert = chrono::high_resolution_clock::now(); // resets clock
}
}
}
ofstream outFile;
string outPut = argv[2];
outFile.open(outPut, ios::out | ios::app);
for(int i = 0; i < 400; i++)
{
outFile << endl << insert[i] << endl;
}
outFile.close();
ofstream outFile2;
string outPut2 = argv[3];
outFile2.open(outPut2, ios::out | ios::app);
for(int i = 0; i < 400; i++)
{
outFile2 << endl << search[i] << endl;
}
outFile2.close();
return 0;
}