Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <string>
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class EmployeeInfo
  8.  
  9. {
  10.  
  11. public:
  12.  
  13. int id;
  14.  
  15. string name;
  16.  
  17. };
  18.  
  19. struct node
  20.  
  21. {
  22.  
  23. EmployeeInfo info;
  24.  
  25. node *left;
  26.  
  27. node *right;
  28.  
  29. };
  30.  
  31. class btree
  32.  
  33. {
  34.  
  35. public:
  36.  
  37. btree();
  38.  
  39. ~btree();
  40.  
  41. void insert(EmployeeInfo key);
  42.  
  43. node *search(int key);
  44.  
  45. void destroy_tree();
  46.  
  47. private:
  48.  
  49. void destroy_tree(node *leaf);
  50.  
  51. void insert(EmployeeInfo key, node *leaf);
  52.  
  53. node *search(int key, node *leaf);
  54.  
  55. node *root;
  56.  
  57. };
  58.  
  59. btree::btree()
  60.  
  61. {
  62.  
  63. root=NULL;
  64.  
  65. }
  66.  
  67. void btree::destroy_tree(node *leaf)
  68.  
  69. {
  70.  
  71. if(leaf!=NULL)
  72.  
  73. {
  74.  
  75. destroy_tree(leaf->left);
  76.  
  77. destroy_tree(leaf->right);
  78.  
  79. delete leaf;
  80.  
  81. }
  82.  
  83. }
  84.  
  85. void btree::insert(EmployeeInfo key, node *leaf)
  86.  
  87. {
  88.  
  89. if(key.id< leaf->info.id)
  90.  
  91. {
  92.  
  93. if(leaf->left!=NULL)
  94.  
  95. insert(key, leaf->left);
  96.  
  97. else
  98.  
  99. {
  100.  
  101. leaf->left=new node;
  102.  
  103. leaf->left->info=key;
  104.  
  105. leaf->left->left=NULL; //Sets the left child of the child node to null
  106.  
  107. leaf->left->right=NULL; //Sets the right child of the child node to null
  108.  
  109. }
  110.  
  111. }
  112.  
  113. else if(key.id>=leaf->info.id)
  114.  
  115. {
  116.  
  117. if(leaf->right!=NULL)
  118.  
  119. insert(key, leaf->right);
  120.  
  121. else
  122.  
  123. {
  124.  
  125. leaf->right=new node;
  126.  
  127. leaf->right->info=key;
  128.  
  129. leaf->right->left=NULL; //Sets the left child of the child node to null
  130.  
  131. leaf->right->right=NULL; //Sets the right child of the child node to null
  132.  
  133. }
  134.  
  135. }
  136.  
  137. }
  138.  
  139. node *btree::search(int key, node *leaf)
  140.  
  141. {
  142.  
  143. if(leaf!=NULL)
  144.  
  145. {
  146.  
  147. if(key==leaf->info.id)
  148.  
  149. return leaf;
  150.  
  151. if(key<leaf->info.id)
  152.  
  153. return search(key, leaf->left);
  154.  
  155. else
  156.  
  157. return search(key, leaf->right);
  158.  
  159. }
  160.  
  161. else return NULL;
  162.  
  163. }
  164.  
  165. node *btree::search(int key)
  166.  
  167. {
  168.  
  169. return search(key, root);
  170.  
  171. }
  172.  
  173. void btree::destroy_tree()
  174.  
  175. {
  176.  
  177. destroy_tree(root);
  178.  
  179. }
  180.  
  181. void btree::insert(EmployeeInfo key)
  182.  
  183. {
  184.  
  185. if(root!=NULL)
  186.  
  187. insert(key, root);
  188.  
  189. else
  190.  
  191. {
  192.  
  193. root=new node;
  194.  
  195. root->info=key;
  196.  
  197. root->left=NULL;
  198.  
  199. root->right=NULL;
  200.  
  201. }
  202.  
  203. }
  204.  
  205. int main()
  206.  
  207. {
  208.  
  209. EmployeeInfo info[8];
  210.  
  211. btree *bt = new btree();
  212.  
  213. info[0].id = 1021;
  214.  
  215. info[0].name = "John Williams";
  216.  
  217. info[1].id = 1057;
  218.  
  219. info[1].name = "Bill witherspoon";
  220.  
  221. info[2].id = 2487;
  222.  
  223. info[2].name = "Jennifer Twain";
  224.  
  225. info[3].id = 3769;
  226.  
  227. info[3].name = "Sophia Lancaster";
  228.  
  229. info[4].id = 1017;
  230.  
  231. info[4].name = "Debbie Reece";
  232.  
  233. info[5].id = 1275;
  234.  
  235. info[5].name = "George McMullan";
  236.  
  237. info[6].id = 1899;
  238.  
  239. info[6].name = "Ashley Smith";
  240.  
  241. info[7].id = 4218;
  242.  
  243. info[7].name = "Josh Plemmons";
  244.  
  245. int i;
  246.  
  247. for(i=0;i<8;i++)
  248.  
  249. {
  250.  
  251. bt->insert(info[i]);
  252.  
  253. }
  254.  
  255. int id;
  256.  
  257. cout<<"enter an ID number : ";
  258.  
  259. cin>>id;
  260.  
  261. if(bt->search(id)==NULL)
  262.  
  263. cout<<id<<" ID not found"<<endl;
  264.  
  265. else
  266.  
  267. cout<<id<<" ID found."<<endl<<"Employee name is : "<<(bt->search(id))->info.name;
  268.  
  269. return 1;
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement