-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tree.cc
155 lines (131 loc) · 4.34 KB
/
test_tree.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
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
/*
TITLE: test_tree.cc
NAME : INGMAR FJOLLA
PROFESSOR: ANITA RAJA
HW 2 Question 2 Part 2b
This wil act as the main file for part2b of our homework.
We have to be able to get the expected output in the form
2: <integer>
3a: <float>
3b: <float>
4a: <integer> 4b: <float> 5a: <integer> 5b: <float> 6a: <integer> 6b: <float> 6c: <float>
the methods will be to correctly count our recursive functions and do the math right!
*/
#include "avl_tree_p2b.h"
#include "sequence_map.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
namespace
{
// @db_filename: an input database filename.
// @seq_filename: an input sequences filename.
// @a_tree: an input tree of the type TreeType. It is assumed to be
// empty.
template <typename TreeType>
void TestTree(const string &db_filename, const string &seq_filename, TreeType &a_tree)
{
// Code for running Part2(b)
//start with opening file
//and getting rid of first few lines
ifstream ourfile;
ourfile.open(db_filename);
string garbage_lines;
for(auto i =0 ; i<10; i++)
{
getline(ourfile,garbage_lines);
}
//now get the rest
string lines;
while(getline(ourfile,lines))
{
stringstream parsing;
parsing.str(lines);
string an_enzyneacronym; //string for the enzyme
getline(parsing, an_enzyneacronym, '/'); //works by stopping at the first instance of slash
string an_recognitionsequence; //string for recognition sequence //sometimes more than one
//second while loop
while(getline(parsing,an_recognitionsequence, '/'))
{
if(an_recognitionsequence.size() >0)
{
SequenceMap our_avl_sequence(an_recognitionsequence, an_enzyneacronym);
a_tree.insert(our_avl_sequence);
}
}
//cout<<lines;
}
//now we have strings and we create SequenceMap object to insert into tree
/**************
end of parsing and populating tree
************/
//number 2 after parser
//a_tree.printTree(); DEBUG LINE
cout << "2: " << a_tree.numberOfNodes() << "\n";
//number 3a for avg depth
cout << "3a: " << a_tree.avgDepth() << "\n";
//number 3b
cout<<"3b: " << a_tree.avgDepthRatio()<<"\n";
//number 4
//Open file and store it in an ifstream object
ifstream sequences_file(seq_filename);
string entire_line_2;
int total_queries = 0;
//Loop through every line in the file
while(getline(sequences_file, entire_line_2))
{
//Create sequence map object
SequenceMap temp_map(entire_line_2, "");
if((temp_map == a_tree.find(temp_map)))
{ //Every call of find() will count the number of recursive calls
total_queries++; //Increment total number of successful queries
}
}
//4a shows total number of seucseful queries found
cout << "4a: " << total_queries << "\n";
///4b
// Prints the average number of recursion calls, i.e. #total number of
// recursion calls / number of queries.
cout<<"4b: "<< float(a_tree.getRecursiveFinds() / total_queries) << endl;
///part 5
string trash;
a_tree.makerecursionzero();
ifstream sequences_file_2(seq_filename);
string entire_line_3 = "";
int successful_removes = 0;
while( getline(sequences_file_2, entire_line_3) )
{
SequenceMap temp_map_1(entire_line_3, "");
if( a_tree.contains(temp_map_1) )
{
a_tree.remove(temp_map_1);
successful_removes++;
}
//go to every other sequence
getline(sequences_file_2, trash);
}
cout << "5a: " << successful_removes << "\n";
//Number 5b - print the average number of recursive calls for remove()
//float average_number_of_remove_calls = a_tree.getRecursiveRemoves() / successful_removes;
cout << "5b: " << float(a_tree.getRecursiveRemoves() / successful_removes) << "\n";
cout << "6a: " << a_tree.numberOfNodes() << endl;
cout << "6b: " << a_tree.avgDepth() << endl;
cout << "6c: " << a_tree.avgDepthRatio() << endl;
}
} // namespace
int
main(int argc, char **argv) {
if (argc != 3) {
cout << "Usage: " << argv[0] << " <databasefilename> <queryfilename>" << endl;
return 0;
}
const string db_filename(argv[1]);
const string seq_filename(argv[2]);
cout << "Input file is " << db_filename << ", and sequences file is " << seq_filename << endl;
// Note that you will replace AvlTree<int> with AvlTree<SequenceMap>
AvlTree<SequenceMap> a_tree;
TestTree(db_filename, seq_filename, a_tree);
return 0;
}