Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. struct wezel
  9. {
  10. int key;
  11. wezel *left, *right;
  12. };
  13.  
  14. wezel * root; //wskaznik globalny na korzen
  15.  
  16. wezel * &inicjalizacja();
  17. void wstawianieNowegoElementu(wezel * &root, int k);
  18. void wstawianieXElementow(wezel * &root, int x);
  19. wezel * wyszukajWezel(wezel * &root, int k);
  20. void usuwanieElementu(wezel * &root, int k);
  21. void usuwanieDrzewa(wezel * &root);
  22. void inorder(wezel * x);
  23. void walk(wezel * x);
  24. wezel * searchParent(wezel *&root, int x);
  25. wezel * searchPoprzednik(wezel *&root, int x);
  26. wezel * searchNastepnik(wezel *&root, int x);
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30. srand(time(NULL)); //zawsze przy uzywaniu randa, zeby liczby sie nie powtarzaly
  31.  
  32.  
  33. int X, k1, k2, k3, k4;
  34. FILE* fp = fopen("inlab03.txt", "r");
  35. if (fp == NULL)return -1;
  36. fscanf(fp, "%d %d %d %d %d", &X, &k1, &k2, &k3, &k4);
  37. fclose(fp);
  38.  
  39. clock_t begin, end;
  40. double time_spent; // czas start
  41. begin = clock();
  42.  
  43.  
  44. wezel * root = inicjalizacja();
  45. usuwanieElementu(root, k1);
  46. wstawianieXElementow(root, 12);
  47. wstawianieNowegoElementu(root, k1);
  48. wstawianieNowegoElementu(root, k2);
  49. wstawianieNowegoElementu(root, k3);
  50. wstawianieNowegoElementu(root, k4);
  51. usuwanieElementu(root, k1);
  52. wyszukajWezel(root, k1);
  53. usuwanieElementu(root, k2);
  54. usuwanieElementu(root, k3);
  55. usuwanieElementu(root, k4);
  56. usuwanieDrzewa(root);
  57.  
  58. end = clock();
  59. time_spent = (double)(end - begin) / CLOCKS_PER_SEC; // czas stop
  60.  
  61. cout << "Czas: " << time_spent << endl;
  62.  
  63. system("pause");
  64.  
  65. return 0;
  66. }
  67.  
  68. wezel * &inicjalizacja()
  69. {
  70. root = NULL;
  71. return root;
  72. }
  73.  
  74. void wstawianieNowegoElementu(wezel * &root, int k)
  75. {
  76. wezel * parent = NULL;
  77. wezel * tmp;
  78. tmp = root;
  79. wezel * nowy = new wezel;
  80. nowy->left = NULL;
  81. nowy->right = NULL;
  82. nowy->key = k;
  83. if (root == NULL)
  84. {
  85. root = nowy;
  86. }
  87. else
  88. {
  89. while (tmp != NULL) //dopoki nie dojdziesz za lisc
  90. {
  91. if (k == tmp->key) // jesli klucz = dodawany to nic nie rob
  92. {
  93. return;
  94. }
  95. if (k>tmp->key && tmp->right == NULL)
  96. {
  97. //parent = tmp;
  98. tmp->right = nowy;
  99.  
  100. }
  101. if (k<tmp->key && tmp->left == NULL) //jesli klucz dodawany jest mniejszy od tymczasowego ktory wskazuje na root oryginalny
  102. {
  103. //parent = tmp;
  104. tmp->left = nowy;
  105. }
  106.  
  107. if (k>tmp->key && tmp->right != NULL)
  108. {
  109. //parent = tmp;
  110. tmp = tmp->right;
  111.  
  112. }
  113. if (k<tmp->key && tmp->left != NULL) //jesli klucz dodawany jest mniejszy od tymczasowego ktory wskazuje na root oryginalny
  114. {
  115. //parent = tmp;
  116. tmp = tmp->left;
  117. }
  118. //if (tmp->right)
  119.  
  120. }
  121. tmp = nowy;// przypisuje wartosc tymczasowa do nowo dodanego wezla
  122. }
  123. }
  124.  
  125. void wstawianieXElementow(wezel * &root, int x)
  126. {
  127. int liczbaLosowa;
  128. for (int i = 0; i < x; i++)
  129. {
  130. liczbaLosowa = (rand() % 1000009) + 10;
  131. wstawianieNowegoElementu(root, liczbaLosowa);
  132. }
  133. }
  134.  
  135. wezel * wyszukajWezel(wezel * &root, int k)
  136. {
  137. wezel * tmp;
  138. tmp = root;
  139. if (root == NULL)
  140. {
  141.  
  142. }
  143. while ((tmp) && (tmp->key != k))
  144. {
  145.  
  146. if (k < tmp->key)
  147. {
  148. if (tmp->left == NULL)
  149. {
  150. cout << "Szukany element: "<<k<<" nie istnieje" << endl;
  151. return NULL;
  152. }
  153. tmp = tmp->left;
  154. }
  155. else
  156. {
  157. if (tmp->right == NULL)
  158. {
  159. cout << "Szukany element: "<<k<<" nie istnieje" << endl;
  160. return NULL;
  161. }
  162. tmp = tmp->right;
  163. }
  164. }
  165.  
  166. cout << "Wyszukiwany wezel wynosi " << tmp->key << endl;
  167. return (tmp);
  168. }
  169.  
  170. void inorder(wezel * x)
  171. {
  172. if (x)
  173. {
  174. inorder(x->left);
  175. cout << x->key << endl; // tutaj przetwarzamy bieżący węzeł
  176. inorder(x->right);
  177. }
  178. }
  179.  
  180. void walk(wezel * x)
  181. {
  182. cout << x->key << " : Left-> ";
  183. if (x->left) cout << x->left->key;
  184. else cout << "NULL";
  185. cout << ", Right-> ";
  186. if (x->right) cout << x->right->key;
  187. else cout << "NULL";
  188. cout << endl;
  189. if (x->left) walk(x->left);
  190. if (x->right) walk(x->right);
  191. }
  192.  
  193. wezel * searchParent(wezel *&root, int k)
  194. {
  195. wezel *tmp = root;
  196. wezel *parent = NULL;
  197.  
  198. if (root == NULL)
  199. {
  200. return 0;
  201. }
  202. while ((tmp) && (tmp->key != k))
  203. {
  204. parent = tmp;
  205. if (k < tmp->key)
  206. {
  207. if (tmp->left == NULL)
  208. {
  209. cout << "Element nie istnieje" << endl;
  210. return NULL;
  211. }
  212. tmp = tmp->left;
  213. }
  214. else
  215. {
  216. if (tmp->right == NULL)
  217. {
  218. cout << "Element nie istnieje" << endl;
  219. return NULL;
  220. }
  221. tmp = tmp->right;
  222. }
  223. }
  224.  
  225. return (parent);
  226. }
  227.  
  228. wezel * searchPoprzednik(wezel *&root, int x)
  229. {
  230. wezel *tmp = root;
  231.  
  232. while (tmp && (x != tmp->key))
  233. {
  234. if (tmp->key < x)
  235. tmp = tmp->right;
  236. else
  237. tmp = tmp->left;
  238. }
  239. if (tmp != NULL)
  240. {
  241.  
  242. if (tmp->left != NULL)
  243. {
  244. tmp = tmp->left;
  245.  
  246. while (tmp->right)
  247. {
  248. tmp = tmp->right;
  249. }
  250. }
  251. }
  252.  
  253. return tmp;
  254. }
  255.  
  256. wezel * searchNastepnik(wezel *&root, int x)
  257. {
  258. wezel *tmp = root;
  259.  
  260. while (tmp && (x != tmp->key))
  261. {
  262. if (tmp->key < x)
  263. tmp = tmp->right;
  264. else
  265. tmp = tmp->left;
  266. }
  267. if (tmp != NULL)
  268. {
  269. if (tmp->right != NULL)
  270. {
  271. tmp = tmp->right;
  272.  
  273. while (tmp->left)
  274. {
  275. tmp = tmp->left;
  276. }
  277. }
  278. }
  279.  
  280. return tmp;
  281. }
  282.  
  283. void usuwanieElementu(wezel * &root, int k)
  284. {
  285.  
  286. wezel* toRemove = root;
  287.  
  288. while (toRemove && (k != toRemove->key))
  289. {
  290. if (toRemove->key < k)
  291. toRemove = toRemove->right;
  292. else
  293. toRemove = toRemove->left;
  294. }
  295.  
  296. if(toRemove != NULL)
  297. {
  298. wezel* parent = searchParent(root, toRemove->key);
  299. // jesli wezel ma dwojke dzieci
  300. if ( toRemove -> left != NULL && toRemove -> right != NULL )
  301. {
  302. wezel* pop = NULL;
  303. cout<<"Wybierz forme usuwania:"<<endl;
  304. cout<<"1 - nastepnik"<<endl;
  305. cout<<"2 - poprzednik"<<endl;
  306. int a = 0;
  307. cin>>a;
  308.  
  309. if(a==1)
  310. {
  311. pop = searchNastepnik(root, k);
  312. }
  313. else
  314. {
  315. if(a==2)
  316. {
  317. pop = searchPoprzednik(root, k);
  318. }
  319. else
  320. {
  321. usuwanieElementu(root, k);
  322. return;
  323. }
  324. }
  325. parent = searchParent(root, pop->key);
  326. toRemove->key = pop->key;
  327. toRemove = pop;
  328. }
  329.  
  330. // jesli wezel nie ma dzieci
  331. if ( toRemove -> left == NULL && toRemove -> right == NULL )
  332. {
  333. if(parent != NULL)
  334. {
  335.  
  336. if ( parent -> right == toRemove )
  337. parent -> right = NULL ;
  338. else
  339. parent -> left = NULL ;
  340.  
  341. }
  342. else
  343. {
  344. root = NULL;
  345. }
  346.  
  347. delete toRemove ;
  348. return;
  349. }
  350.  
  351. // jesli wezel ma tylko prawe dziecko
  352. if ( toRemove -> left == NULL && toRemove -> right != NULL )
  353. {
  354. if(parent != NULL)
  355. {
  356.  
  357. if ( parent -> left == toRemove )
  358. parent -> left = toRemove -> right;
  359. else
  360. parent -> right = toRemove -> right;
  361.  
  362. }
  363. else
  364. {
  365. root = toRemove->right;
  366. }
  367.  
  368. delete toRemove ;
  369. return;
  370. }
  371.  
  372. // jesli wezel ma tylko lewe dziecko
  373. if ( toRemove -> left != NULL && toRemove -> right== NULL )
  374. {
  375. if(parent != NULL)
  376. {
  377.  
  378. if ( parent -> left == toRemove )
  379. parent -> left= toRemove -> left;
  380. else
  381. parent -> right = toRemove -> left;
  382.  
  383. }
  384. else
  385. {
  386. root = toRemove->left;
  387. }
  388.  
  389. delete toRemove ;
  390. return;
  391. }
  392. }
  393. else
  394. {
  395. cout<<"Nie mozna usunac wezla o kluczu: "<<k<<endl;
  396. }
  397. }
  398.  
  399. void usuwanieDrzewa(wezel * &root)
  400. {
  401. if(root != NULL)
  402. usuwanieElementu(root, root->key);
  403.  
  404. if(root != NULL)
  405. usuwanieDrzewa(root);
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement