Advertisement
Guest User

BST

a guest
Jan 16th, 2018
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. //SDIZO I1 213A LAB05
  2. //Mateusz Rynkiewicz
  3. //rm39424@zut.edu.pl
  4. #define _CRTDBG_MAP_ALLOC
  5. #include "stdafx.h"
  6. #include <string>
  7. #include <iostream>
  8. #include <ctype.h>
  9. #include <time.h>
  10. //mniejszy element, klucz, wiekszy element
  11.  
  12. struct Wezel {
  13. int a;
  14. Wezel* left;
  15. Wezel* right;
  16. };
  17.  
  18.  
  19. Wezel* Nowy() {
  20. Wezel* korzen = (Wezel*)malloc(sizeof(Wezel));
  21. korzen->a = NULL;
  22. korzen->left = NULL;
  23. korzen->right = NULL;
  24. return korzen;
  25. }
  26. Wezel* Nowy(int i) {
  27. Wezel* korzen = (Wezel*)malloc(sizeof(Wezel));
  28. korzen->a = i;
  29. korzen->left = NULL;
  30. korzen->right = NULL;
  31. return korzen;
  32. }
  33.  
  34. Wezel* DodajElement(Wezel* korzen, int klucz, bool &check) {
  35. Wezel* kopia = korzen;
  36. check = true;
  37. if (korzen == NULL) {
  38. korzen = Nowy(klucz);
  39. return korzen;
  40. }
  41. go:
  42. if (korzen->a == klucz) {
  43. //std::cout << "Element znajduje sie juz w drzewie" << std::endl;
  44. check = false;
  45. return kopia;
  46. }
  47. else {
  48. if (korzen->a > klucz) {
  49. if (korzen->left == NULL) {
  50. korzen->left = Nowy(klucz);
  51. return kopia;
  52. }korzen = korzen->left;
  53. goto go;
  54. }
  55. else {
  56. if (korzen->right == NULL) {
  57. korzen->right = Nowy(klucz);
  58. return kopia;
  59. }
  60. korzen = korzen->right;
  61. goto go;
  62. }
  63. }
  64.  
  65. }
  66.  
  67. bool SzukajElement(Wezel* korzen, int klucz) {
  68. for (;;) {
  69. if (korzen == NULL) {
  70. //std::cout << "Nie znaleziono elementu o kluczu: " << klucz << std::endl;
  71. return false;
  72. }
  73. if (korzen->a == klucz) {
  74. //std::cout << "Znaleziono element o kluczu: " << klucz << std::endl;
  75. return true;
  76. }
  77. if (korzen->a > klucz) {
  78. korzen = korzen->left;
  79. }
  80. else if (korzen->a < klucz) {
  81. korzen = korzen->right;
  82. }
  83.  
  84. }
  85. }
  86. Wezel* WstawX(Wezel* korzen, int x, int &count) {
  87. int w;
  88. bool check = false;
  89. count = 0;
  90. for (int i = 0; i <x; i++) {
  91. //std::cout << "wstawiono: " << w << std::endl;
  92.  
  93. w = (rand() % 25000) * 4 + (rand() % 4);
  94.  
  95.  
  96. korzen = DodajElement(korzen, w, check);
  97. if (check)count++;
  98. check = false;
  99. }
  100. return korzen;
  101. }
  102. Wezel* WstawX2(Wezel* korzen, int x, int &count) {
  103. int w;
  104. x = x / 2;
  105. bool check = false;
  106. count = 0;
  107. for (int i = 0; i < x; i++) {
  108. //std::cout << "wstawiono: " << w << std::endl;
  109.  
  110. w = (rand() % 25000) * 4 + (rand() % 4);
  111. korzen = DodajElement(korzen, w, check);
  112. if (check)count++;
  113. check = false;
  114. korzen = DodajElement(korzen, i + 1, check);
  115. if (check)count++;
  116. check = false;
  117. }
  118. return korzen;
  119. }
  120.  
  121.  
  122. //nierekurencyjnie
  123.  
  124. Wezel* UsunElement(Wezel* korzen, int klucz, bool &check) {
  125. Wezel* kopia = korzen;
  126. check = true;
  127. ////0 elementow
  128. if (korzen == NULL)return NULL;
  129. if (korzen->a == klucz && korzen->left == NULL && korzen->right == NULL) {
  130. free(korzen);
  131. check = false;
  132. return NULL;
  133. }
  134. ////Usuwamy 1 element
  135. go:
  136. if (korzen->a == klucz) {
  137.  
  138. //2xNULL
  139. if (korzen->left == NULL && korzen->right == NULL) {
  140. free(korzen);
  141. return kopia;
  142. }
  143. //1xNULL
  144. if (korzen->left != NULL && korzen->right == NULL) {
  145. Wezel* temp = korzen->left;
  146. korzen->a = temp->a;
  147. korzen->left = temp->left;
  148. korzen->right = temp->right;
  149. free(temp);
  150. return kopia;
  151. }
  152. if (korzen->left == NULL && korzen->right != NULL) {
  153. Wezel* temp = korzen->right;
  154. korzen->a = temp->a;
  155. korzen->left = temp->left;
  156. korzen->right = temp->right;
  157. free(temp);
  158. return kopia;
  159. }
  160. //0xNULL
  161. if (korzen->left != NULL && korzen->right != NULL) {
  162. Wezel* temp = korzen;
  163. //od razu po lewej jest lisc
  164. if (temp->left->right == NULL) {
  165. temp = korzen->left;
  166. korzen->a = temp->a;
  167. korzen->left = temp->left;
  168. free(temp);
  169. return kopia;
  170. }
  171. temp = temp->left;
  172. for (;;) {
  173. if (temp->right->right == NULL)break;
  174. temp = temp->right;
  175. }
  176. korzen->a = temp->right->a;
  177. Wezel* temp2 = temp->right;
  178. temp->right = temp->right->left;
  179. free(temp2);
  180. return kopia;
  181. }
  182. }
  183. //kolejny element
  184. if (korzen->a > klucz) {
  185. if (korzen->left == NULL) {
  186. check = false;
  187. return kopia;
  188. }
  189. if (korzen->left->a == klucz && korzen->left->left == NULL && korzen->left->right == NULL) {
  190. Wezel* temp = korzen->left;
  191. korzen->left = NULL;
  192. korzen = temp;
  193. goto go;
  194. }
  195. korzen = korzen->left;
  196. goto go;
  197. }
  198. else {
  199. if (korzen->right == NULL) {
  200. check = false; return kopia;
  201. }
  202. if (korzen->right->a == klucz && korzen->right->left == NULL && korzen->right->right == NULL) {
  203. Wezel* temp = korzen->right;
  204. korzen->right = NULL;
  205. korzen = temp;
  206. goto go;
  207. }
  208. korzen = korzen->right;
  209. goto go;
  210. }
  211. }
  212.  
  213. //pre-order
  214.  
  215.  
  216.  
  217.  
  218. void main() {
  219.  
  220.  
  221. int ziarno = 2;
  222. bool losowe = false;
  223. int n = 10000;
  224. //for (int iterator = 0; iterator < 5; iterator++) {
  225. //n = n + 5000;
  226. std::cout << std::endl;
  227. std::cout << std::endl;
  228. srand(ziarno);
  229. double* wyniki = (double*)malloc(sizeof(double) * 6);
  230. srand(ziarno);
  231. clock_t begin, end; double time_spent;
  232. Wezel* korzen = NULL;
  233. if (losowe) {
  234. #pragma region losowe
  235. begin = clock();
  236.  
  237. int x;
  238. korzen = WstawX(korzen, n, x);
  239.  
  240. end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  241. wyniki[0] = x;
  242. wyniki[1] = time_spent;
  243. #pragma endregion
  244. }
  245. else {
  246. #pragma region nielosowo
  247. begin = clock();
  248.  
  249. int x;
  250. korzen = WstawX2(korzen, n, x);
  251.  
  252. end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  253. wyniki[0] = x;
  254. wyniki[1] = time_spent;
  255. #pragma endregion
  256. }
  257. #pragma region Szukaj
  258. begin = clock();
  259. int x = 0;
  260. for (int i = 0; i < n; i++) {
  261. if (SzukajElement(korzen, (rand() % 25000) * 4 + (rand() % 4))) {
  262. x++;
  263. }
  264. }
  265. end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  266. wyniki[2] = x;
  267. wyniki[3] = time_spent;
  268. #pragma endregion
  269. #pragma region Usun
  270. begin = clock();
  271. x = 0;
  272. bool check = false;
  273. for (int i = 0; i < n; i++) {
  274. korzen = UsunElement(korzen, (rand() % 25000) * 4 + (rand() % 4), check);
  275. if (check)x++;
  276. check = false;
  277. }
  278.  
  279. end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  280. wyniki[4] = x;
  281. wyniki[5] = time_spent;
  282. #pragma endregion
  283. std::cout << "Liczba elementow: " << n << std::endl;
  284. std::cout << "Wstawianie: " << wyniki[0] << " " << wyniki[1] << std::endl;
  285. std::cout << "Szukanie: " << wyniki[2] << " " << wyniki[3] << std::endl;
  286. std::cout << "Usuwanie: " << wyniki[4] << " " << wyniki[5] << std::endl;
  287. //}
  288. system("pause");
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement