Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. // BST.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <iterator>
  10.  
  11. using namespace std;
  12.  
  13. class EmployeeInfo {
  14. int ID;
  15. string name;
  16.  
  17. public:
  18. EmployeeInfo(int ID, string name) {
  19. this->ID = ID;
  20. this->name = name;
  21. }
  22.  
  23. int getId() {
  24. return ID;
  25. }
  26.  
  27. string getName() {
  28. return name;
  29. }
  30.  
  31. friend bool operator<= (const EmployeeInfo &e1, const EmployeeInfo &e2);
  32. friend bool operator> (const EmployeeInfo &e1, const EmployeeInfo &e2);
  33. };
  34.  
  35. bool operator<= (const EmployeeInfo &e1, const EmployeeInfo &e2) {
  36. return e1.ID <= e2.ID;
  37. }
  38.  
  39. bool operator> (const EmployeeInfo &e1, const EmployeeInfo &e2) {
  40. return e1.ID > e2.ID;
  41. }
  42.  
  43. class BSTNode {
  44. BSTNode* left;
  45. BSTNode* right;
  46. EmployeeInfo* info;
  47.  
  48. public:
  49. BSTNode(EmployeeInfo* info) {
  50. this->info = info;
  51. left = NULL;
  52. right = NULL;
  53. }
  54.  
  55. EmployeeInfo* getInfo() {
  56. return info;
  57. }
  58.  
  59. BSTNode* getLeft() {
  60. return left;
  61. }
  62.  
  63. BSTNode* getRight() {
  64. return right;
  65. }
  66.  
  67. void setLeft(BSTNode* node) {
  68. this->left = node;
  69. }
  70.  
  71. void setRight(BSTNode* node) {
  72. this->right = node;
  73. }
  74.  
  75. };
  76.  
  77. class BST {
  78. BSTNode* root;
  79.  
  80. public:
  81. BST() {
  82. root = NULL;
  83. }
  84.  
  85. void addNode(BSTNode* node) {
  86. if (root == NULL) {
  87. root = node;
  88. }
  89. else {
  90. BSTNode* cur = root;
  91. while (cur != NULL && ((*node).getInfo() <= (*cur).getInfo() || (*node).getInfo() > (*cur).getInfo())) {
  92. if ((*node).getInfo() <= (*cur).getInfo()) {
  93. //cout << "going left" << endl;
  94. BSTNode* check = (*cur).getLeft();
  95. if (check == NULL) {
  96. (*cur).setLeft(node);
  97. }
  98. cur = check;
  99. }
  100. else {
  101. BSTNode* check = (*cur).getRight();
  102. if (check == NULL) {
  103. (*cur).setRight(node);
  104. }
  105. cur = check;
  106. }
  107. }
  108. }
  109. }
  110.  
  111. string findById(int ID) {
  112. if (root == NULL) {
  113. return "Employee: " + to_string(ID) + " was NOT found because the BST is empty.";
  114. }
  115.  
  116. BSTNode* cur = root;
  117. while (cur != NULL) {
  118. EmployeeInfo curInfo = (*(*cur).getInfo());
  119. if (curInfo.getId() == ID) {
  120. return "Employee: " + to_string(ID) + " is " + curInfo.getName();
  121. }
  122. else if (ID < curInfo.getId()) {
  123. cur = (*cur).getLeft();
  124. }
  125. else {
  126. cur = (*cur).getRight();
  127. }
  128. }
  129.  
  130. return "Employee: " + to_string(ID) + " was NOT found in the BST.";
  131. }
  132.  
  133. BSTNode* getRoot() {
  134. return root;
  135. }
  136. };
  137.  
  138. void help() {
  139. cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  140. cout << "|| Commands: ||" << endl;
  141. cout << "|| ||" << endl;
  142. cout << "|| '[ID number]' ||" << endl;
  143. cout << "|| - Searches for the ID in the BST ||" << endl;
  144. cout << "|| 'help' ||" << endl;
  145. cout << "|| - Show this help menu ||" << endl;
  146. cout << "|| 'exit' ||" << endl;
  147. cout << "|| - Exit the program ||" << endl;
  148. cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  149. };
  150.  
  151. void run() {
  152. BST bst = BST();
  153.  
  154. ifstream inFile;
  155. inFile.open("C:/users/user/desktop/employees.txt");
  156.  
  157. if (!inFile) {
  158. cerr << "Unable to open file employees.txt";
  159. exit(1); // call system to stop
  160. }
  161.  
  162. string line;
  163. int num = 0;
  164.  
  165. while (getline(inFile, line)) {
  166. istringstream iss(line);
  167. int id;
  168. string firstName, lastName;
  169. while (iss >> id >> firstName >> lastName) {
  170. //cout << to_string(id) + " " + firstName + " " + lastName << endl;
  171. EmployeeInfo* e = new EmployeeInfo(id, firstName + " " + lastName);
  172. BSTNode* n = new BSTNode(e);
  173. bst.addNode(n);
  174. }
  175. }
  176.  
  177. string input;
  178. while (true) {
  179. cout << "Please enter an ID to search for, or type help or exit:" << endl;
  180. getline(cin, input);
  181. if (input == "exit") {
  182. return;
  183. }
  184. else if (input == "help") {
  185. help();
  186. }
  187. else {
  188. cout << bst.findById(stoi(input)) << endl;
  189. }
  190. }
  191. };
  192.  
  193. int main()
  194. {
  195. cout << "====================================================================" << endl;
  196. cout << "|| Welcome to my Employee Tree! ||" << endl;
  197. cout << "====================================================================" << endl;
  198. run();
  199. }
  200.  
  201. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  202. // Debug program: F5 or Debug > Start Debugging menu
  203.  
  204. // Tips for Getting Started:
  205. // 1. Use the Solution Explorer window to add/manage files
  206. // 2. Use the Team Explorer window to connect to source control
  207. // 3. Use the Output window to see build output and other messages
  208. // 4. Use the Error List window to view errors
  209. // 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
  210. // 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