Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. // ConsoleApplication8.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include"stdafx.h"
  5. #include <iostream>
  6. #include<windows.h>
  7. using namespace std;
  8.  
  9. struct BSTnode {
  10. BSTnode * left;
  11. BSTnode * right;
  12. BSTnode* up;
  13. int key;
  14. };
  15.  
  16. void addNode(BSTnode **root, int key) {
  17. system("CLS");
  18. BSTnode * newNode = new BSTnode();
  19. newNode->key = key;
  20. BSTnode * temp = new BSTnode();
  21. temp = *root;
  22. if (*root == NULL) {
  23. *root = newNode;
  24. }
  25. else if (key > temp->key) {
  26. if (temp->right) {
  27. addNode(&temp->right, key);
  28. }
  29. else {
  30. newNode->up = temp;
  31. temp->right = newNode;
  32. }
  33. }
  34. else if (key < temp->key) {
  35. if (temp->left) {
  36. addNode(&temp->left, key);
  37. }
  38. else {
  39. newNode->up = temp;
  40. temp->left = newNode;
  41. }
  42. }
  43. }
  44.  
  45. void maxNode(BSTnode ** root) {
  46. BSTnode * temp = (*root);
  47. while (temp->right)
  48. temp = temp->right;
  49. cout<< temp->key;
  50. }
  51.  
  52. void minNode(BSTnode ** root) {
  53. BSTnode * temp = (*root);
  54. while (temp->left)
  55. temp = temp->left;
  56. cout << temp->key;
  57. }
  58.  
  59. void in_order(BSTnode * root) {
  60. if (root->left)
  61. in_order(root->left);
  62. cout << root->key << " ";
  63. if (root->right)
  64. in_order(root->right);
  65. if (root == NULL)
  66. cout << "Jebac";
  67. }
  68.  
  69. BSTnode * searchNode(BSTnode ** root,int key) {
  70. BSTnode*temp = (*root);
  71. while (temp) {
  72. if (key == temp->key)
  73. return temp;
  74. else if (key < temp->key)
  75. temp = temp->left;
  76. else if (key > temp->key)
  77. temp = temp->right;
  78. }
  79. return NULL;
  80. }
  81. BSTnode * BSTminnode(BSTnode * root)
  82. {
  83. BSTnode *temp = root;
  84.  
  85. while (temp->left)
  86. temp = temp->left;
  87.  
  88. return temp;
  89. }
  90. BSTnode * BSTsucc(BSTnode * node)
  91. {
  92. if (node->right)
  93. return BSTminnode(node->right);
  94.  
  95. BSTnode * y;
  96.  
  97. do
  98. {
  99. y = node;
  100. node = node->up;
  101. } while (node && (node->left != y));
  102.  
  103. return node;
  104. }
  105.  
  106. void deleteNode(BSTnode * &root, BSTnode * node) {
  107. BSTnode * Y, *Z;
  108.  
  109. if (node)
  110. {
  111. // Jeśli X nie ma synów lub ma tylko jednego, to Y wskazuje X
  112. // Inaczej Y wskazuje następnik X
  113.  
  114. Y = !node->left || !node->right ? node : BSTsucc(node);
  115.  
  116. // Z wskazuje syna Y lub NULL
  117.  
  118. Z = Y->left ? Y->left : Y->right;
  119.  
  120. // Jeśli syn Y istnieje, to zastąpi Y w drzewie
  121.  
  122. if (Z) Z->up = Y->up;
  123.  
  124. // Y zostaje zastąpione przez Z w drzewie
  125.  
  126. if (!Y->up) root = Z;
  127. else if (Y == Y->up->left)
  128. Y->up->left = Z;
  129. else
  130. Y->up->right = Z;
  131.  
  132. // Jeśli Y jest następnikiem X, to kopiujemy dane
  133.  
  134. if (Y != node) node->key = Y->key;
  135.  
  136. delete Y;
  137. }
  138. }
  139.  
  140. int main()
  141. {
  142. BSTnode * root = NULL;
  143. BSTnode*x;
  144. root = NULL;
  145. int wybor, a, b,value;
  146. do {
  147. cout << endl;
  148. cout << "------------------------------------------" << endl;
  149. cout << "1. addNode " << endl;
  150. cout << "2. maxNode " << endl;
  151. cout << "3. minNode " << endl;
  152. cout << "4. searchNode " << endl;
  153. cout << "------------------------------------------" << endl;
  154. cout << "Wybor: ";
  155. cin >> wybor;
  156. switch (wybor) {
  157. case 1:
  158. cout << "Jaki element chcesz dodac do drzewa: ";
  159. cin >> a;
  160. addNode(&root, a);
  161. break;
  162.  
  163. case 2:
  164. cout << "Najwiekszy element to: ";
  165. maxNode(&root);
  166. Sleep(3000);
  167. system("CLS");
  168. break;
  169.  
  170. case 3:
  171. cout << "Najmniejszy element to: ";
  172. minNode(&root);
  173. Sleep(3000);
  174. system("CLS");
  175. break;
  176.  
  177. case 4:
  178. cout << "Szukany element: ";
  179. cin >> b;
  180. cout << "Wezel: "<< searchNode(&root, b);
  181. Sleep(3000);
  182. system("CLS");
  183. break;
  184.  
  185. case 5:
  186. cin >> value;
  187. x = searchNode(&root, value);
  188. deleteNode(root, x);
  189. }
  190. cout << "Elementy na drzewie: ";
  191. in_order(root);
  192. } while (wybor != 10);
  193.  
  194. return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement