Advertisement
Guest User

Untitled

a guest
May 27th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. #include<queue>
  4.  
  5. template<typename T>
  6. struct Node
  7. {
  8. T info;
  9. Node<T> *left, *right, *parent;
  10. Node(const T c_info = T())
  11. {
  12. info = c_info;
  13. left = NULL;
  14. right = NULL;
  15. parent = NULL;
  16. }
  17. };
  18.  
  19. template<typename T>
  20. class BinaryTree
  21. {
  22. private:
  23. void RSD(Node<T> *aux) const
  24. {
  25. if (aux != NULL)
  26. {
  27. std::cout << aux->info << " ";
  28. RSD(aux->left);
  29. RSD(aux->right);
  30. }
  31.  
  32. }
  33. void SDR(Node<T> *aux) const
  34. {
  35. if (aux != NULL)
  36. {
  37. SDR(aux->left);
  38. SDR(aux->right);
  39. std::cout << aux->info << " ";
  40. }
  41. }
  42. void SRD(Node<T> *aux) const
  43. {
  44. if (aux != NULL)
  45. {
  46. SRD(aux->left);
  47. std::cout << aux->info << " ";
  48. SRD(aux->right);
  49. }
  50. }
  51. void levels() const
  52. {
  53. std::queue<Node<T> *> c;
  54. for (c.push(root); c.empty() == false; c.pop())
  55. {
  56. std::cout << c.front()->info << " ";
  57. if (c.front()->left != NULL)
  58. c.push(c.front()->left);
  59. if (c.front()->right != NULL)
  60. c.push(c.front()->right);
  61. }
  62. }
  63. void postDelete(Node<T> *node)
  64. {
  65. if (node != NULL)
  66. {
  67. postDelete(node->left);
  68. postDelete(node->right);
  69. delete node;
  70. }
  71. }
  72. void transplant(Node<T>* node1, Node<T>* node2)
  73. {
  74. if (node1 == root)
  75. root = node2;
  76. else
  77. if (node1 == node1->parent->left)
  78. node1->parent->left = node2;
  79. else
  80. node1->parent->right = node2;
  81. if (node2 != NULL)
  82. node2->parent = node1->parent;
  83. }
  84. public:
  85. Node<T> *root;
  86. void insert(const T &key)
  87. {
  88. if (root == NULL)
  89. root = new Node<T>(key);
  90. else
  91. {
  92. Node<T>* toInsert = new Node<T>(key);
  93. Node<T>* copyR = root;
  94. while (copyR)
  95. {
  96. if (toInsert->info > copyR->info)
  97. {
  98. if (copyR->right == NULL)
  99. {
  100. copyR->right = toInsert;
  101. toInsert->parent = copyR;
  102. return;
  103. }
  104. copyR = copyR->right;
  105. }
  106. else
  107. {
  108. if (copyR->left == NULL)
  109. {
  110. copyR->left = toInsert;
  111. toInsert->parent = copyR;
  112. return;
  113. }
  114. copyR = copyR->left;
  115. }
  116. }
  117. }
  118. }
  119. Node<T>* min(Node<T> *R) const
  120. {
  121. Node<T> *copyR = R;
  122. while (copyR->left != NULL)
  123. copyR = copyR->left;
  124. return copyR;
  125. }
  126. Node<T>* max(Node<T> *R) const
  127. {
  128. Node<T> *copyR = R;
  129. while (copyR->right != NULL)
  130. copyR = copyR->right;
  131. return copyR;
  132. }
  133. Node<T>* successor(Node<T> *node) const
  134. {
  135. if (node->right != NULL)
  136. {
  137. return min(node->right);
  138. }
  139. Node<T> *aux = node;
  140. while (aux->right == node && aux != NULL)
  141. {
  142. node = aux;
  143. aux = aux->parent;
  144. }
  145. aux = node->parent;
  146. return aux;
  147. }
  148. Node<T>* predecessor(Node<T> *node) const
  149. {
  150. if (node->left != NULL)
  151. {
  152. return max(node->left);
  153. }
  154. Node<T> *aux = node;
  155. while (aux->left == node && aux != NULL)
  156. {
  157. node = aux;
  158. aux = aux->parent;
  159. }
  160. aux = node->parent;
  161. return aux;
  162. }
  163. Node<T>* search(const T &val) const
  164. {
  165. Node<T> *aux = root;
  166. while (aux != NULL && aux->info != val)
  167. {
  168. if (val < aux->info)
  169. aux = aux->left;
  170. else
  171. aux = aux->right;
  172. }
  173. return aux;
  174. }
  175. void deleteNode(Node<T> *node)
  176. {
  177. if (node->left == NULL)
  178. transplant(node, node->right);
  179. else
  180. if (node->right == NULL)
  181. transplant(node, node->left);
  182. else
  183. {
  184. Node<T>* succ = successor(node);
  185. if (succ != node->right)
  186. {
  187. transplant(succ, succ->right);
  188. succ->right = node->right;
  189. node->right->parent = succ;
  190. }
  191. transplant(node, succ);
  192. succ->left = node->left;
  193. node->left->parent = succ;
  194. }
  195. }
  196. void clear()
  197. {
  198. postDelete(root);
  199. root = NULL;
  200. }
  201. void construct(const std::vector<T> elements)
  202. {
  203. for (auto element : elements)
  204. insert(element);
  205. }
  206. void printTree(const int &option) const
  207. {
  208. if (option == 1)
  209. RSD(root);
  210. else if (option == 2)
  211. SRD(root);
  212. else if (option == 3)
  213. SDR(root);
  214. else if (option == 4)
  215. levels();
  216. }
  217. BinaryTree()
  218. {
  219. root = NULL;
  220. }
  221. ~BinaryTree()
  222. {
  223. clear();
  224. }
  225. };
  226.  
  227. int main()
  228. {
  229.  
  230. BinaryTree<int> tree;
  231. Node<int> *node;
  232. int input, key;
  233. while (true)
  234. {
  235. std::cout << "1. Insert\n2. Max\n3. Min\n4. Successor\n5. Predeccessor\n6. Delete\n7. Empty\n8. Print tree\n9. Quit\n\n";
  236. std::cout << "Select option: ";
  237. std::cin >> input;
  238. switch (input)
  239. {
  240. case 1:
  241. std::cout << "Insert key: ";
  242. std::cin >> key;
  243. tree.insert(key);
  244. break;
  245. case 2:
  246. std::cin >> key;
  247. node = tree.search(key);
  248. if (node != NULL)
  249. std::cout << tree.max(node)->info << std::endl;
  250. else
  251. std::cout << "Invalid" << std::endl;
  252. break;
  253. case 3:
  254. std::cin >> key;
  255. node = tree.search(key);
  256. if (node != NULL)
  257. std::cout << tree.min(node)->info << std::endl;
  258. else
  259. std::cout << "Invalid" << std::endl;
  260. break;
  261. case 4:
  262. std::cin >> key;
  263. node = tree.search(key);
  264. if (node != NULL)
  265. std::cout << tree.successor(node)->info << std::endl;
  266. else
  267. std::cout << "Invalid" << std::endl;
  268. break;
  269. case 5:
  270. std::cin >> key;
  271. node = tree.search(key);
  272. if (node != NULL)
  273. std::cout << tree.predecessor(node)->info << std::endl;
  274. else
  275. std::cout << "Invalid" << std::endl;
  276. break;
  277. case 6:
  278. std::cin >> key;
  279. node = tree.search(key);
  280. if (node != NULL)
  281. tree.deleteNode(node);
  282. else
  283. std::cout << "Invalid" << std::endl;
  284. break;
  285. case 7:
  286. tree.clear();
  287. break;
  288. case 8:
  289. std::cin >> key;
  290. tree.printTree(key);
  291. std::cout << "\n";
  292. break;
  293. case 9:
  294. return 1;
  295. break;
  296. default:
  297. std::cout << "Invalid option.\n";
  298. break;
  299. }
  300. system("pause");
  301. system("cls");
  302. }
  303. return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement