Complete DB code following the instructions:
Implement the DB and use the unittest_db() function to test it. DO NOT CHANGE THIS CODE
FOR YOUR SUBMISSION, as it will be used to test your code against the answers (with different
input files).
a) The student ID should be the base class's key member.
b) The student ID should be automatically assigned such that the ID/key should be n if the student
is the nth student to join the school. If the student later leaves (i.e., deleted from the BST), the ID
does NOT get reassigned to another student. Thus, the student ID of the last student to join the
school should reflect the TOTAL number of students that have joined this school since its
reception (regardless of whether some have left).
5. Test your code against the provided input and output files.
a) The provided answer for the BST unit test is in "unittest_ans_t100_s100.txt". The s100 refers to
the seed of 100 (-s 100), and t100 refers to the number of elements to add to the BST (-t 100).
b) The provided answer for the DB is in "students_1_ans.txt" for the "students_1.txt" input file.
6. Make sure your code has no memory leaks (using valgrind).
7. Your code should work beyond the provided unit tests. That is, even if it does work for all the
given tests, if the code has an identifiable bug (i.e., by reading the source code), points WILL be
deducted.
For example, if I were to change
unittest_bst(num_test, seed, cout, 5); ->
unittest_bst(num_test, seed, cout, 100);
it should still work.
db.h
#ifndef DB_H_
#define DB_H_
#include <iostream>
#include "bst.h"
using namespace std;
class SNode : public Node {
private:
string first;
string last;
unsigned int age;
static unsigned int num_students;
public:
// Constructors and destructor
SNode();
SNode(string f_, string l_, unsigned int a_);
~SNode();
// public interface
void change_first(string f_);
void change_last(string l_);
string get_first();
string get_last();
unsigned int get_age();
void print_info(ostream& to);
};
#endif
main.cc
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <getopt.h>
#include "bst.h"
#include "db.h"
using namespace std;
const char* const short_opt = ":ht:s:i:o:";
const struct option long_opt[] = {
{"help", 0, NULL, 'h'},
{"test", 1, NULL, 't'},
{"seed", 1, NULL, 's'},
{"input", 1, NULL, 'i'},
{"output", 1, NULL, 'o'},
{NULL, 0, NULL, 0}
};
void usage(char* argv);
void unittest_bst(unsigned int n, unsigned int seed, ostream& to,
unsigned int remain);
bool read_db(const string file_name, vector<string>& firsts,
vector<string>& lasts, vector<unsigned int>& ages);
void create_db(BST& db, const vector<string> f_, const vector<string> l_,
const vector<unsigned int> a_);
bool unittest_db(string ifile_name, string ofile_name);
/* Main function
*/
int main(int argc, char **argv)
{
int c = 0;
string test = "0";
string seed_str = "0";
string ifile_name;
string ofile_name;
if(argc < 2) {
usage(argv[0]);
return 0;
}
whil.
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Complete DB code following the instructions Implement the D.pdf
1. Complete DB code following the instructions:
Implement the DB and use the unittest_db() function to test it. DO NOT CHANGE THIS CODE
FOR YOUR SUBMISSION, as it will be used to test your code against the answers (with different
input files).
a) The student ID should be the base class's key member.
b) The student ID should be automatically assigned such that the ID/key should be n if the student
is the nth student to join the school. If the student later leaves (i.e., deleted from the BST), the ID
does NOT get reassigned to another student. Thus, the student ID of the last student to join the
school should reflect the TOTAL number of students that have joined this school since its
reception (regardless of whether some have left).
5. Test your code against the provided input and output files.
a) The provided answer for the BST unit test is in "unittest_ans_t100_s100.txt". The s100 refers to
the seed of 100 (-s 100), and t100 refers to the number of elements to add to the BST (-t 100).
b) The provided answer for the DB is in "students_1_ans.txt" for the "students_1.txt" input file.
6. Make sure your code has no memory leaks (using valgrind).
7. Your code should work beyond the provided unit tests. That is, even if it does work for all the
given tests, if the code has an identifiable bug (i.e., by reading the source code), points WILL be
deducted.
For example, if I were to change
unittest_bst(num_test, seed, cout, 5); ->
unittest_bst(num_test, seed, cout, 100);
it should still work.
db.h
#ifndef DB_H_
#define DB_H_
#include <iostream>
#include "bst.h"
using namespace std;
class SNode : public Node {
private:
string first;
string last;
unsigned int age;
static unsigned int num_students;
public:
// Constructors and destructor
SNode();
SNode(string f_, string l_, unsigned int a_);
~SNode();
// public interface
void change_first(string f_);
3. string seed_str = "0";
string ifile_name;
string ofile_name;
if(argc < 2) {
usage(argv[0]);
return 0;
}
while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
switch(c) {
case 'h':
usage(argv[0]);
return 0;
case 't':
test = optarg;
break;
case 's':
seed_str = optarg;
break;
case 'i':
ifile_name = optarg;
break;
case 'o':
ofile_name = optarg;
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
// Unit Test
int num_test = stoi(test);
if(num_test > 0) {
int seed = stoi(seed_str);
unittest_bst(num_test, seed, cout, 5);
}
// Check if input and output files were specified
if(ifile_name.empty() || ofile_name.empty()) {
usage(argv[0]);
return -1;
}
if(!unittest_db(ifile_name, ofile_name)) {
return -1;
4. }
return 0;
}
/* Print information on how to properly use this program
*/
void usage(char* call)
{
cout << "Usage: " << call << endl;
cout << "Options:" << endl;
cout << "t-h or --help Display this information" << endl;
cout << "t-t or --test n BST Unit Test with n elements" << endl;
cout << "t-s or --seed s Rand # seed value" << endl;
cout << "t-i or --input ifname Input file" << endl;
cout << "t-o or --output ofname Output file" << endl;
}
void unittest_bst(unsigned int n, unsigned int seed, ostream& to,
unsigned int remain)
{
to << "Unit Test for baseline BST implementation" << endl;
srand(seed);
vector<int> keys(n);
for(unsigned int i = 0; i < keys.size(); i++) {
keys[i] = rand() % 100;
}
int num_keys = keys.size();
/* or you can specify keys manually */
/* Make sure this is commented out for the final submission */
#if 0
keys[0] = 15;
keys[1] = 10;
keys[2] = 20;
keys[3] = 8;
keys[4] = 12;
keys[5] = 16;
keys[6] = 25;
num_keys = 7;
keys.resize(num_keys);
keys.shrink_to_fit();
#endif
#if 0
keys[0] = 100;
5. keys[1] = 1;
keys[2] = 150;
keys[3] = 101;
keys[4] = 23;
keys[5] = 47;
num_keys = 6;
keys.resize(num_keys);
keys.shrink_to_fit();
#endif
// Unsorted numbers
to << "Input: " << endl;
for(int i = 0; i < num_keys; i++) {
to << keys[i] << " ";
}
to << endl;
// Sorted numbers should look like...
vector<int> tmp = keys;
sort(tmp.begin(), tmp.end());
to << "Sorted input: " << endl;
for(int i = 0; i < num_keys; i++) {
to << tmp[i] << " ";
}
to << endl;
// insert_node() test
Node** nodes = new Node*[num_keys];
BST my_BST;
for(int i = 0; i < num_keys; i++) {
nodes[i] = new Node(keys[i]);
my_BST.insert_node(nodes[i]);
}
// tree_min() and tree_max() test
to << "----------" << endl;
to << "BST min: " << my_BST.tree_min()->get_key() << endl;
to << "BST max: " << my_BST.tree_max()->get_key() << endl;
to << "----------" << endl;
// walk() test
to << "----------" << endl;
to << "BST walk" << endl;
my_BST.walk(to);
to << "----------" << endl;
// get_pred() and get_succ() test
6. to << "----------" << endl;
to << "Predecessor/Successor" << endl;
for(int i = 0; i < num_keys; i++) {
to << nodes[i]->get_key();
if(my_BST.get_pred(nodes[i]) != nullptr) {
to << " pred: " << my_BST.get_pred(nodes[i])->get_key();
} else {
to << " pred: " << "none";
}
if(my_BST.get_succ(nodes[i]) != nullptr) {
to << " succ: " << my_BST.get_succ(nodes[i])->get_key() << endl;
} else {
to << " succ: " << "none" << endl;;
}
}
to << "----------" << endl;
// tree_search() test
to << "----------" << endl;
to << "tree_search() with fake keys" << endl;
for(int i = 0; i < num_keys; i++) {
Node* tmp = my_BST.tree_search(keys[i] + 1);
if(tmp != nullptr) {
to << "Found " << tmp->get_key() << endl;
} else {
to << keys[i] + 1 << " was not found" << endl;
}
}
to << "----------" << endl;
// tree_search() and delete_node() test
to << "----------" << endl;
to << "tree_search() and delete_node()" << endl;
for(unsigned int i = 0; i < num_keys - remain; i++) {
to << "---- deleting " << keys[i] << "-----" << endl;
Node* tmp = my_BST.tree_search(keys[i]);
if(tmp != nullptr) {
my_BST.delete_node(tmp);
my_BST.walk(to);
} else {
to << keys[i] << " was not found" << endl;
}
}
8. vector<unsigned int> ages;
if(!read_db(ifile_name, first_names, last_names, ages)) {
return false;
}
BST student_db;
create_db(student_db, first_names, last_names, ages);
ofstream outFile(ofile_name);
outFile << "-------------------------------------------------" << endl;
student_db.walk(outFile);
outFile << "-------------------------------------------------" << endl;
outFile << "First student ever: " << endl;
student_db.tree_min()->print_info(outFile);
outFile << "Last student to join: " << endl;
student_db.tree_max()->print_info(outFile);
unsigned int n = 36;
outFile << "Information on student: " << n << endl;
student_db.tree_search(n)->print_info(outFile);
outFile << "Removing student...";
student_db.delete_node(student_db.tree_search(n));
SNode* tmp = (SNode*) student_db.tree_search(n);
if(tmp == nullptr) {
outFile << "student successfully removed" << endl;
} else {
outFile << "error removing student" << endl;
}
outFile << "-------------------------------------------------" << endl;
student_db.walk(outFile);
outFile << "-------------------------------------------------" << endl;
outFile.close();
return true;
}
COMPLETE db.cc
#include "db.h"
// ---------------------------------------------
// SNode class
// Default constructor
// TODO: Implement this
// BASIC function header is provided for so that the code will compile
// The actual function header may be different
SNode::SNode()
{
}
9. // Constructor
// TODO: Implement this
// BASIC function header is provided for so that the code will compile
// The actual function header may be different
SNode::SNode(string f_, string l_, unsigned int a_)
{
}
// Destructor
SNode::~SNode()
{
// TODO: Implement this
}
unsigned int SNode::num_students = 0;
// Public interface
void SNode::change_first(string f_)
{
// TODO: Implement this
}
void SNode::change_last(string l_)
{
// TODO: Implement this
}
string SNode::get_first()
{
// TODO: Implement this
}
string SNode::get_last()
{
// TODO: Implement this
}
unsigned int SNode::get_age()
{
// TODO: Implement this
}
// Print information about the student
// do not change this
void SNode::print_info(ostream& to)
{
to << "Student ID: " << this->get_key()
<< "tFirst: " << this->first
<< "tLast: " << this->last
<< "tAge: " << this->age << endl;