Guest User

Untitled

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. using namespace std;
  5. template <typename T> class Tree
  6. {
  7. public:
  8. struct Node
  9. {
  10. T Key;
  11. Node* Parent;
  12. Node* Left;
  13. Node* Right;
  14. }* root;
  15. private:
  16. Node* SearchNext(Node* start, T Key)
  17. {
  18. Node* res = NULL;
  19. if(start != NULL){
  20. if(start->Key < Key)
  21. {
  22. res = start->Right;
  23. }
  24. if(start->Key >= Key)
  25. {
  26. res = start->Left;
  27. }
  28. }
  29. return res;
  30. }
  31. void Vis(Node* nd)
  32. {
  33. cout << nd->Key << " ";
  34. if(nd->Left != NULL)
  35. {
  36. Vis(nd->Left);
  37. }
  38. if(nd->Right != NULL)
  39. {
  40. Vis(nd->Right);
  41. }
  42. }
  43. public:
  44. Tree(T Key)
  45. {
  46. root = new Node();
  47. root->Key = Key;
  48. root->Parent = NULL;
  49. root->Left = NULL;
  50. root->Right = NULL;
  51. }
  52. void Add(T Key)
  53. {
  54. Node* now = root;
  55. Node* tmp = SearchNext(now, Key);
  56. while(tmp != NULL)
  57. {
  58. now = tmp;
  59. tmp = SearchNext(now, Key);
  60. }
  61. tmp = new Node();
  62. tmp->Key = Key;
  63. tmp->Parent = now;
  64. if(now->Key < tmp->Key)
  65. {
  66. now->Right = tmp;
  67. }
  68. else
  69. {
  70. now->Left = tmp;
  71. }
  72. }
  73. Node* Min()
  74. {
  75. Node* now = root;
  76. Node* tmp = now->Left;
  77. while(tmp != NULL)
  78. {
  79. now = tmp;
  80. tmp = now->Left;
  81. }
  82. return now;
  83. }
  84. Node* Max()
  85. {
  86. Node* now = root;
  87. Node* tmp = now->Right;
  88. while(tmp != NULL)
  89. {
  90. now = tmp;
  91. tmp = now->Right;
  92. }
  93. return now;
  94. }
  95. Node* Search(T Key)
  96. {
  97. Node* now = root;
  98. Node* tmp = SearchNext(now, Key);
  99. while(tmp != NULL && now->Key != Key)
  100. {
  101. now = tmp;
  102. tmp = SearchNext(now, Key);
  103. }
  104. if(now->Key == Key)
  105. {
  106. return now;
  107. }
  108. return NULL;
  109. }
  110. void Remove(T Key)
  111. {
  112. Node* now = root;
  113. Node* tmp = SearchNext(now, Key);
  114. while(tmp != NULL && now->Key != Key)
  115. {
  116. now = tmp;
  117. tmp = SearchNext(now, Key);
  118. }
  119. if(now->Key != Key)
  120. {
  121. printf("Key is not found!\n");
  122. return;
  123. }
  124. Node* parent = now->Parent;
  125. if(now->Left == NULL && now->Right == NULL)
  126. {
  127. if(parent != NULL && parent->Left == now)
  128. {
  129. parent->Left = NULL;
  130. }
  131. if(parent != NULL && parent->Right == now)
  132. {
  133. parent->Right = NULL;
  134. }
  135. delete now;
  136. printf("Key is deleted!\n");
  137. return;
  138. }
  139. if(now->Left == NULL)
  140. {
  141. if(parent != NULL && parent->Left == now)
  142. {
  143. parent->Left = now->Right;
  144. }
  145. if(parent != NULL && parent->Right == now)
  146. {
  147. parent->Right = now->Right;
  148. }
  149. Node* next = now->Right;
  150. next->Parent = parent;
  151. delete now;
  152. printf("Key is deleted!\n");
  153. return;
  154. }
  155. if(now->Right == NULL)
  156. {
  157. if(parent != NULL && parent->Left == now)
  158. {
  159. parent->Left = now->Left;
  160. }
  161. if(parent != NULL && parent->Right == now)
  162. {
  163. parent->Right = now->Left;
  164. }
  165. Node* next = now->Left;
  166. next->Parent = parent;
  167. delete now;
  168. printf("Key is deleted!\n");
  169. return;
  170. }
  171. tmp = now->Right;
  172. while(tmp->Left != NULL)
  173. {
  174. tmp = tmp->Left;
  175. }
  176. Node* t = tmp->Parent;
  177. if(t->Right != tmp)
  178. {
  179. t->Left = tmp->Right;
  180. }
  181. if(parent != NULL && parent->Left == now)
  182. {
  183. parent->Left = tmp;
  184. }
  185. if(parent != NULL && parent->Right == now)
  186. {
  187. parent->Right = tmp;
  188. }
  189. tmp->Left = now->Left;
  190. t = tmp->Left;
  191. t->Parent = tmp;
  192. if(now->Right == tmp)
  193. {
  194. tmp->Right = NULL;
  195. }
  196. else
  197. {
  198. tmp->Right = now->Right;
  199. }
  200. if(root == now)
  201. {
  202. root = tmp;
  203. tmp->Parent = NULL;
  204. }
  205. delete now;
  206. printf("Key is deleted!\n");
  207. }
  208. Node* Earlier(int key)
  209. {
  210. Node* n = Search(key);
  211. if(n == NULL)
  212. {
  213. return ((typename Tree<T>::Node*)-1);
  214. }
  215. else
  216. {
  217. return n->Parent;
  218. }
  219. }
  220. void Visual()
  221. {
  222. Vis(root);
  223. }
  224. };
  225. void AutoStart()
  226. {
  227. srand(time(NULL));
  228. int num = 1;
  229. int t = 0;
  230. t = - 25 + rand() % 50;
  231. cout << "Input 1 element>" << t << "\n";
  232. Tree<int>* tr = new Tree<int>(t);
  233. for(num; num < 10;)
  234. {
  235. t = - 25 + rand() % 50;
  236. cout << "Input " << ++num << " element>" << t << "\n";
  237. tr->Add(t);
  238. }
  239. tr->Visual();
  240. cin.get();
  241. cout << "Task 1. Search of element.\n";
  242. Tree<int>::Node* n;
  243. cout << "Input key of node>";
  244. cin >> t;
  245. n = tr->Search(t);
  246. if(n == NULL)
  247. {
  248. cout << "Node with key " << t << " was not found.\n";
  249. }
  250. else
  251. {
  252. cout << "Address of node with key " << t << " is " << n << "\n";
  253. }
  254. cout << "Task 2. Min and Max elements.\n";
  255. n = tr->Min();
  256. cout << "Min element on address " << n << " has key " << n->Key << "\n";
  257. cin.get();
  258. n = tr->Max();
  259. cout << "Max element on address " << n << " has key " << n->Key << "\n";
  260. cin.get();
  261. cout << "Task 3. Parent element.\n";
  262. cout << "Input key of node>";
  263. cin >> t;
  264. n = tr->Earlier(t);
  265. if(n == (Tree<int>::Node*)-1)
  266. {
  267. cout << "Node with key " << t << " was not found.\n";
  268. }
  269. if(n == NULL)
  270. {
  271. cout << "Node with key " << t << " has no parent. It is root.\n";
  272. }
  273. if(n != NULL && n != (Tree<int>::Node*)-1)
  274. {
  275. cout << "Address of parent node with key " << t << " is " << n << " it has key " << n->Key << "\n";
  276. }
  277. cout << "Task 4. Remove element.\n";
  278. cout << "Input key of node>";
  279. cin >> t;
  280. tr->Remove(t);
  281. tr->Visual();
  282. cin.get();
  283. cin.get();
  284. }
  285. int _tmain(int argc, char* argv[])
  286. {
  287. AutoStart();
  288. return 0;
  289. }
Add Comment
Please, Sign In to add comment