'Toxin
2012-03-13 16:11:04
Hey guys, so I'm working on an come c++ stuff once again, and I am having some troubles with my task. I'm doing BST(Binary Search Trees) now, and asked to create a few functions with the BST which has been defined. Anyway…
The whole point of this is to understand the difference between procedural programming and object-oriented programming. The BST nodes contain two data types, a student identification number, and some information about the student. The BST MUST be sorted by the identification number. I am asked to invoke the following methods for both types of programming :
1) insert - takes user information(identification number & information) and inserts in the BST.
2) remove - takes in an identification number
3) lookup - takes in an identification number and returns the matching string with the identification number, if not returns an empty string
4) print - prints the identification number and information to the console in ascending order by identification number
Alright, so that's it. At a quick glance, as I thought… Everything seemed easy. Now… I have started writing out these programs. The first one is going to be done using a procedural BST, and here's what I have so far :
And here's the code I have for the second method of impelmentation, using Object-Oriented BST's :
Now, if anyone has any idea about how to do anything or is willing to give me a helping hand. I would really appreciate it. I believe I have set up the classes in both cases properly, now it's just the creation of the 4 methods which I am left to do. So yeah, hit me up with a PM, reply, or anything really for that matter, because at this point I'm just lost and don't know where to proceed.
Thanks,
Toxin
The whole point of this is to understand the difference between procedural programming and object-oriented programming. The BST nodes contain two data types, a student identification number, and some information about the student. The BST MUST be sorted by the identification number. I am asked to invoke the following methods for both types of programming :
1) insert - takes user information(identification number & information) and inserts in the BST.
2) remove - takes in an identification number
3) lookup - takes in an identification number and returns the matching string with the identification number, if not returns an empty string
4) print - prints the identification number and information to the console in ascending order by identification number
Alright, so that's it. At a quick glance, as I thought… Everything seemed easy. Now… I have started writing out these programs. The first one is going to be done using a procedural BST, and here's what I have so far :
struct Node {
int uw_id;
string info;
Node* left;
Node* right;
};
typedef Node* BST;
void init(BST& root);
string lookup(BST& root, int uw_id);
void insert(BST& root, int uw_id, string info);
void remove(BST& root, int uw_id);
void print(const BST& root);
int main (int argc, char* argv[]) {
if(argc != 2) {
cerr << "Usage: " << argv[0] << " <file name>" << endl;
exit(1);
}
ifstream fin(argv[1]);
if(fin.fail()) {
cerr << "Failed to open input file " << argv[1] << endl;
exit(1);
}
BST root;
init(root);
int uw_id;
while(fin >> uw_id) {
string info;
getline(fin, info);
insert(root, uw_id, info);
}
print(root);
const int N = 3;
int uw_ids[N] = { 1234, 9012, 5555 };
for(int i=0; i<N; i++)
cout << "lookup " << uw_ids[i] << " " << lookup(root, uw_ids[i]) << endl;
remove(root, 5678);
print(root);
}
And here's the code I have for the second method of impelmentation, using Object-Oriented BST's :
class Node {
public:
int uw_id;
string info;
Node* left;
Node* right;
Node(int uw_id, const string& info);
};
class BST {
public:
BST();
~BST();
void insert(int uw_id, const string& info);
string lookup(int uw_id) ;
void remove(int uw_id);
void print();
private:
Node* root;
};
int main (int argc, char* argv[]) {
if(argc != 2) {
cerr << "Usage: " << argv[0] << " <file name>" << endl;
exit(1);
}
ifstream fin(argv[1]);
if(fin.fail()) {
cerr << "Failed to open input file " << argv[1] << endl;
exit(1);
}
BST root;
int uw_id;
while(fin >> uw_id) {
string info;
getline(fin, info);
root.insert(uw_id, info);
}
root.print();
const int N = 3;
int uw_ids[N] = { 1234, 9012, 5555 };
for(int i=0; i<N; i++)
cout << "lookup " << uw_ids[i] << " " << root.lookup(uw_ids[i])
<< endl;
root.remove(5678);
root.print();
}
Now, if anyone has any idea about how to do anything or is willing to give me a helping hand. I would really appreciate it. I believe I have set up the classes in both cases properly, now it's just the creation of the 4 methods which I am left to do. So yeah, hit me up with a PM, reply, or anything really for that matter, because at this point I'm just lost and don't know where to proceed.
Thanks,
Toxin