Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. class node
  6. {
  7. public:
  8. int info;
  9. node *parent;
  10. node *left;
  11. node *right;
  12.  
  13. node();
  14. };
  15.  
  16. node :: node()
  17. {
  18. left = NULL;
  19. right = NULL;
  20. }
  21.  
  22. class tree : node
  23. {
  24. public:
  25. tree();
  26. ~tree();
  27.  
  28. node *root;
  29. node *aux;
  30.  
  31. void menu();
  32.  
  33. private:
  34. void destruct(node*);
  35. bool insert_new(node*,int);
  36. bool remove(node*,int);
  37. int count(node*,int);
  38. node* search(node*,int);
  39. void print(node*);
  40. node* predecessor(node*);
  41. node* successor(node*);
  42.  
  43. };
  44.  
  45. tree :: tree()
  46. {
  47. root = NULL;
  48. }
  49.  
  50. void tree :: destruct(node *root)
  51. {
  52. aux = root;
  53.  
  54. if (aux != NULL)
  55. {
  56. if (aux->left != NULL)
  57. destruct(aux->left);
  58. if (aux->right != NULL)
  59. destruct(aux->right);
  60.  
  61. delete aux;
  62. }
  63.  
  64. }
  65.  
  66. tree :: ~tree()
  67. {
  68. destruct(root);
  69. }
  70.  
  71. int tree :: count(node* aux, int cont)
  72. {
  73. if(aux != NULL)
  74. {
  75. cont++;
  76. cont = count(aux->left, cont);
  77. cont = count(aux->right, cont);
  78. }
  79. return cont;
  80. }
  81.  
  82.  
  83. bool tree :: insert_new(node *aux, int value)
  84. {
  85. node *data;
  86.  
  87. if (aux == NULL) // case 1: tree is empty
  88. {
  89. data = new node;
  90. data->info = value;
  91. data->parent = NULL;
  92. root = data;
  93. return true;
  94. }
  95. else
  96. {
  97. if (value > aux->info) // case 2: value is greater than current node
  98. {
  99. if (aux->right != NULL)
  100. insert_new(aux->right, value);
  101. else
  102. {
  103. data = new node;
  104. data->info = value;
  105. aux->right = data;
  106. data->parent = aux;
  107. return true;
  108. }
  109. }
  110.  
  111. if (value < aux->info) // case 3: value is smaller than current node
  112. {
  113. if (aux->left != NULL)
  114. insert_new(aux->left,value);
  115. else
  116. {
  117. data = new node;
  118. data->info = value;
  119. aux->left = data;
  120. data->parent = aux;
  121. return true;
  122. }
  123. }
  124. }
  125.  
  126. }
  127.  
  128. void tree :: print(node *aux) //in-order traversal
  129. {
  130. if (aux == NULL)
  131. printf("tree is empty.");
  132. else
  133. {
  134. if (aux->left != NULL)
  135. print(aux->left);
  136.  
  137. printf(" %d ",aux->info);
  138.  
  139. if (aux->right != NULL)
  140. print(aux->right);
  141.  
  142. }
  143. }
  144.  
  145.  
  146. node* tree :: search(node *aux, int value)
  147. {
  148.  
  149. if (aux == NULL) // case 1: no elements in tree
  150. {
  151. printf("tree is empty.");
  152. getch();
  153. return NULL;
  154. }
  155.  
  156. if (aux->info == value) // case 2: value searched is the root
  157. {
  158. printf("value %d found, it is the root",value);
  159. return aux;
  160. }
  161.  
  162. if (value > aux->info)
  163. { printf("e maior que %d\n",aux->info);
  164. if (aux->right!= NULL)
  165. {printf("proximo a direita nao e nulo\n");
  166. if (aux->right->info == value) // value searched is in right sub-tree
  167. {
  168. printf("value %d found, father is %d",value,aux->info);
  169. getch();
  170. return aux;
  171. }
  172. else
  173.  
  174. if (value > aux->right->info || value < aux->right->info)
  175. search(aux->right, value);
  176. else
  177. {
  178. printf("value %d not found.",value);
  179. getch();
  180. return NULL;
  181. }
  182.  
  183.  
  184. }
  185. else //aux->right == NULL
  186. {
  187. printf("value %d not found.",value);
  188. getch();
  189. return NULL;
  190. }
  191. }
  192.  
  193. if (value < aux->info)
  194. {printf("e menor que %d\n",aux->info);
  195. if (aux->left != NULL)
  196. {printf("proximo a esquerda nao e nulo\n");
  197. if (aux->left->info == value)
  198. {
  199. printf("value %d found, father is %d",value,aux->info);
  200. getch();
  201. return aux;
  202. }
  203. else
  204.  
  205. if (value < aux->left->info || value > aux->left->info)
  206. search(aux->left, value);
  207.  
  208.  
  209. }
  210. else //aux->left == NULL
  211. {
  212. printf("value %d not found.",value);
  213. getch();
  214. return NULL;
  215. }
  216. }
  217.  
  218.  
  219. } //end of function
  220.  
  221. void tree :: menu()
  222. {
  223. printf("\n\n\t\t\t[data structures] :: binary search tree\n\n");
  224.  
  225. int op;
  226. int value;
  227.  
  228. printf("\n\n\tenter the desired option:");
  229.  
  230. printf("\n\n\t %c1: insert a node in the tree.",175);
  231. printf("\n\t %c2: remove a node from the tree",175);
  232. printf("\n\t %c3: show tree (in-order traversal)",175);
  233. printf("\n\t %c4: search for a node",175);
  234. printf("\n\t %c5: count nodes",175);
  235. printf("\n\t %c6: quit",175);
  236.  
  237.  
  238.  
  239.  
  240. while (op != 6)
  241. {
  242. printf("\n\n\t\t%c",175);
  243. scanf("%d",&op);
  244.  
  245. switch (op)
  246. {
  247. case 1: printf("\n\n\t\tenter the desired number to insert in the tree.");
  248. printf("\n\n\t\t%c ",175);
  249. scanf("%d",&value);
  250. insert_new(root, value);
  251. system("cls");
  252. menu();
  253. break;
  254. case 2: printf("\n\n\t\tenter the desired number to remove from the tree.");
  255. printf("\n\n\t\t%c ",175);
  256. scanf("%d",&value);
  257. remove(root,value);
  258. system("cls");
  259. menu();
  260. break;
  261. case 3: print(root);
  262. getch();
  263. system("cls");
  264. menu();
  265. break;
  266. case 4: printf("\n\n\t\tenter the desired number to be searched.");
  267. printf("\n\n\t\t%c ",175);
  268. scanf("%d",&value);
  269. search(root, value);
  270. getch();
  271. system("cls");
  272. menu();
  273. break;
  274. case 5: printf("%d",count(root,0));
  275. case 6: exit(0);
  276. default: system("cls");
  277. menu();
  278. }
  279. }
  280.  
  281. }
  282.  
  283. node* tree :: predecessor(node *start) //predecessor is the right-most node on the left sub-tree
  284. {
  285. // start must be the child on the left of the node whose predecessor is being looked for
  286.  
  287. if (start->right != NULL)
  288. return predecessor(start->right);
  289. else
  290. return start;
  291. }
  292.  
  293. node* tree :: successor(node *start) //successor is the left-most node on the right sub-tree
  294. {
  295. // start must be the child on the right of the node whose successor is being looked for
  296.  
  297. if (start->left != NULL)
  298. return successor(start->left);
  299. else
  300. return start;
  301.  
  302. }
  303.  
  304. bool tree :: remove(node* address,int value)
  305. {
  306. aux = search(address,value); //receives address from father of node to be removed
  307. printf("%d",aux->info);
  308. //return 0;
  309.  
  310. if (aux == NULL) // case 1: tree is empty
  311. return false;
  312.  
  313. else
  314.  
  315. if (aux->info == value) // case 2: value to be removed is the root
  316. {
  317. if (aux->right == NULL && aux->left == NULL) // root has no children
  318. {
  319. delete aux;
  320. root = NULL;
  321. return true;
  322. }
  323.  
  324. if (aux->right == NULL && aux->left != NULL) // root has a child in the left sub-tree
  325. {
  326. root = aux->left;
  327. delete aux;
  328. }
  329.  
  330. if (aux->right != NULL && aux->left == NULL) // root has a child in the right sub-tree
  331. {
  332. root = aux->right;
  333. delete aux;
  334. }
  335.  
  336. if (aux->right != NULL && aux->left != NULL) // root has children on both nodes
  337. {
  338.  
  339. node *temp = successor(aux->right);
  340. printf("sucessor e' %d",temp->info);getch();
  341.  
  342. int pivot = temp->info;
  343. aux->info = pivot; // copy value of successor to node to be removed
  344. printf("\npivot e' %d",pivot);
  345.  
  346. remove(temp->parent,pivot); // call function recursively to remove the same node but in a different sub-tree
  347. }
  348. }
  349.  
  350. else
  351. {
  352. if (value > aux->info) // case 2: node to be removed is in right sub-tree
  353. {
  354. if (aux->right->right == NULL && aux->right->left == NULL) //node has no children
  355. {
  356. delete aux->right;
  357. aux->right = NULL;
  358. return true;
  359. }
  360.  
  361. else
  362.  
  363. if (aux->right->right == NULL && aux->right->left != NULL) // node has a child in the left sub-tree
  364. {
  365. node *temp = aux->right; // store the child of the node to be removed
  366. aux->right = aux->right->left; // point the father to the child of the node to be removed
  367. delete temp;
  368. return true;
  369. }
  370.  
  371. else
  372.  
  373. if (aux->right->right != NULL && aux->right->left == NULL) // node has a child in the right sub-tree
  374. {
  375. node *temp = aux->right;
  376. aux->right = aux->right->right;
  377. delete temp;
  378. return true;
  379. }
  380.  
  381. if (aux->right->right != NULL && aux->right->left != NULL) // node has children on both sub-tress
  382. {
  383. node *temp = successor(aux->right);
  384. printf("sucessor e' %d",temp->info);getch();
  385.  
  386. int pivot = temp->info;
  387. aux->info = pivot; // copy value of successor to node to be removed
  388. printf("\npivot e' %d",pivot);
  389.  
  390. remove(temp->parent,pivot); // call function recursively to remove the same node but in a different sub-tree
  391. }
  392.  
  393.  
  394. } // end of value > aux->info
  395.  
  396. if (value < aux->info) // case 4: node to be removed is in left sub-tree
  397. {
  398. if (aux->left->left == NULL && aux->left->right == NULL) // node has no children
  399. {
  400. delete aux->left;
  401. aux->left = NULL;
  402. return true;
  403. }
  404.  
  405. else
  406.  
  407. if (aux->left->right != NULL && aux->left->left == NULL) // node has a child in right sub-tree
  408. {
  409. node *temp = aux->left; // store the child of the node to be removed
  410. aux->left = aux->left->right; // point the father to the child of the node to be removed
  411. delete temp;
  412. return true;
  413. }
  414.  
  415. else
  416.  
  417. if (aux->left->right == NULL && aux->left->left != NULL) // node has a child in left sub-tree
  418. {
  419. node *temp = aux->left; // store the child of the node to be removed
  420. aux->left = aux->left->left; // point the father to the child of the node to be removed
  421. delete temp;
  422. return true;
  423. }
  424.  
  425. if (aux->left->right != NULL && aux->left->left != NULL) // node has children on both sub-tress
  426. {
  427. node *temp = successor(aux->right);
  428. printf("sucessor e' %d",temp->info);getch();
  429.  
  430. int pivot = temp->info;
  431. aux->info = pivot; // copy value of successor to node to be removed
  432. printf("\npivot e' %d",pivot);
  433.  
  434. remove(temp->parent,pivot); // call function recursively to remove the same node but in a different sub-tree
  435. }
  436.  
  437.  
  438. } //end of value < aux->info
  439. }
  440.  
  441. }
  442.  
  443. int main (void)
  444. {
  445.  
  446. tree bst;
  447.  
  448. bst.menu();
  449.  
  450. system("pause>null");
  451.  
  452. return 0;
  453.  
  454. }
Add Comment
Please, Sign In to add comment