Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BST.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iterator>
- using namespace std;
- class EmployeeInfo {
- int ID;
- string name;
- public:
- EmployeeInfo(int ID, string name) {
- this->ID = ID;
- this->name = name;
- }
- int getId() {
- return ID;
- }
- string getName() {
- return name;
- }
- friend bool operator<= (const EmployeeInfo &e1, const EmployeeInfo &e2);
- friend bool operator> (const EmployeeInfo &e1, const EmployeeInfo &e2);
- };
- bool operator<= (const EmployeeInfo &e1, const EmployeeInfo &e2) {
- return e1.ID <= e2.ID;
- }
- bool operator> (const EmployeeInfo &e1, const EmployeeInfo &e2) {
- return e1.ID > e2.ID;
- }
- class BSTNode {
- BSTNode* left;
- BSTNode* right;
- EmployeeInfo* info;
- public:
- BSTNode(EmployeeInfo* info) {
- this->info = info;
- left = NULL;
- right = NULL;
- }
- EmployeeInfo* getInfo() {
- return info;
- }
- BSTNode* getLeft() {
- return left;
- }
- BSTNode* getRight() {
- return right;
- }
- void setLeft(BSTNode* node) {
- this->left = node;
- }
- void setRight(BSTNode* node) {
- this->right = node;
- }
- };
- class BST {
- BSTNode* root;
- public:
- BST() {
- root = NULL;
- }
- void addNode(BSTNode* node) {
- if (root == NULL) {
- root = node;
- }
- else {
- BSTNode* cur = root;
- while (cur != NULL && ((*node).getInfo() <= (*cur).getInfo() || (*node).getInfo() > (*cur).getInfo())) {
- if ((*node).getInfo() <= (*cur).getInfo()) {
- //cout << "going left" << endl;
- BSTNode* check = (*cur).getLeft();
- if (check == NULL) {
- (*cur).setLeft(node);
- }
- cur = check;
- }
- else {
- BSTNode* check = (*cur).getRight();
- if (check == NULL) {
- (*cur).setRight(node);
- }
- cur = check;
- }
- }
- }
- }
- string findById(int ID) {
- if (root == NULL) {
- return "Employee: " + to_string(ID) + " was NOT found because the BST is empty.";
- }
- BSTNode* cur = root;
- while (cur != NULL) {
- EmployeeInfo curInfo = (*(*cur).getInfo());
- if (curInfo.getId() == ID) {
- return "Employee: " + to_string(ID) + " is " + curInfo.getName();
- }
- else if (ID < curInfo.getId()) {
- cur = (*cur).getLeft();
- }
- else {
- cur = (*cur).getRight();
- }
- }
- return "Employee: " + to_string(ID) + " was NOT found in the BST.";
- }
- BSTNode* getRoot() {
- return root;
- }
- };
- void help() {
- cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- cout << "|| Commands: ||" << endl;
- cout << "|| ||" << endl;
- cout << "|| '[ID number]' ||" << endl;
- cout << "|| - Searches for the ID in the BST ||" << endl;
- cout << "|| 'help' ||" << endl;
- cout << "|| - Show this help menu ||" << endl;
- cout << "|| 'exit' ||" << endl;
- cout << "|| - Exit the program ||" << endl;
- cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- };
- void run() {
- BST bst = BST();
- ifstream inFile;
- inFile.open("C:/users/user/desktop/employees.txt");
- if (!inFile) {
- cerr << "Unable to open file employees.txt";
- exit(1); // call system to stop
- }
- string line;
- int num = 0;
- while (getline(inFile, line)) {
- istringstream iss(line);
- int id;
- string firstName, lastName;
- while (iss >> id >> firstName >> lastName) {
- //cout << to_string(id) + " " + firstName + " " + lastName << endl;
- EmployeeInfo* e = new EmployeeInfo(id, firstName + " " + lastName);
- BSTNode* n = new BSTNode(e);
- bst.addNode(n);
- }
- }
- string input;
- while (true) {
- cout << "Please enter an ID to search for, or type help or exit:" << endl;
- getline(cin, input);
- if (input == "exit") {
- return;
- }
- else if (input == "help") {
- help();
- }
- else {
- cout << bst.findById(stoi(input)) << endl;
- }
- }
- };
- int main()
- {
- cout << "====================================================================" << endl;
- cout << "|| Welcome to my Employee Tree! ||" << endl;
- cout << "====================================================================" << endl;
- run();
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement