Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. //ALGO2 N1 20B LAB04
  2. //Maciej Baraniecki
  3. //bm35988@zut.edu.pl
  4.  
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <cstdlib>
  9. #include <ctime>
  10. #include <stdio.h>
  11.  
  12. using namespace std;
  13.  
  14. struct BSTNode
  15. {
  16. int klucz;
  17. struct BSTNode *left, *right;
  18. char tab[10];
  19.  
  20. };
  21.  
  22. /*BSTNode* newNode(int klucz)
  23. {
  24. BSTNode* temp = new BSTNode;
  25. temp->klucz = klucz;
  26. temp->left=temp->right = NULL;
  27. return temp;
  28. }*/
  29.  
  30.  
  31. bool dodaj(BSTNode* root, int klucz)
  32. {
  33. BSTNode* p = root;
  34. BSTNode* prev=nullptr;
  35.  
  36. while(p!=nullptr)
  37. {
  38. prev=p;
  39. if(p->klucz<klucz)
  40. {
  41. p=p->right;
  42. }
  43. else
  44. {
  45. p=p->left;
  46. }
  47.  
  48. }
  49.  
  50. if(root->klucz==0)
  51. {
  52. //root=new BSTNode;
  53. root->klucz=klucz;
  54. root->left=nullptr;
  55. root->right=nullptr;
  56. cout<<"Dodano korzen"<<endl;
  57. return true;
  58. }
  59. else if(prev->klucz<klucz)
  60. {
  61. prev->right=new BSTNode;
  62. (prev->right)->klucz=klucz;
  63. (prev->right)->left=nullptr;
  64. (prev->right)->right=nullptr;
  65. cout<<"Dodano wezel prawy"<<endl;
  66. return true;
  67. }
  68. else if(prev->klucz>klucz)
  69. {
  70. prev->left=new BSTNode;
  71. (prev->left)->klucz=klucz;
  72. (prev->left)->left=nullptr;
  73. (prev->left)->right=nullptr;
  74. cout<<"Dodano wezel lewy"<<endl;
  75. return true;
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81.  
  82.  
  83. };
  84.  
  85. void wstawianie(BSTNode* root, int X)
  86. {
  87. int klucz;
  88. for (int i = 0; i < X; i++)
  89. {
  90. do
  91. {
  92. klucz = (rand() % 20000) - 1000;
  93. }while(dodaj(root,klucz)!=true);
  94. }
  95. }
  96.  
  97. BSTNode* search(BSTNode* root, int klucz)
  98. {
  99. BSTNode* p = root;
  100. if(!p)
  101. {
  102. cout<<"Drzewo puste!"<<endl;
  103. return nullptr;
  104. }
  105. while((p==nullptr)&(klucz==p->klucz))
  106. {
  107. if(p->klucz<klucz)
  108. {
  109. cout<<"NIE"<<endl;
  110. p=p->right;
  111. }
  112. else
  113. {
  114. cout<<"TAK"<<endl;
  115. p=p->left;
  116. }
  117. }
  118. cout<<"ZNaleziono"<<endl;
  119. return(p);
  120.  
  121. }
  122.  
  123. BSTNode* minValueNode(BSTNode* node)
  124. {
  125. BSTNode* current = node;
  126.  
  127. /* loop down to find the leftmost leaf */
  128. while (current->left != nullptr)
  129. current = current->left;
  130.  
  131. return current;
  132. }
  133.  
  134.  
  135. BSTNode* usun(BSTNode* root, int klucz)
  136. {
  137. if(root == NULL)
  138. {
  139. return root;
  140. }
  141.  
  142. if(klucz < root->klucz)
  143. {
  144. root->left = usun(root->left,klucz);
  145. }
  146. else if(klucz > root->klucz)
  147. {
  148. root->right = usun(root->right, klucz);
  149. }
  150. else
  151. {
  152. if(root->left == NULL)
  153. {
  154. BSTNode* temp = root->right;
  155. delete(root);
  156. return temp;
  157. }
  158. else if(root->right == NULL)
  159. {
  160. BSTNode* temp = root->left;
  161. delete(root);
  162. return temp;
  163. }
  164.  
  165. BSTNode* temp=minValueNode(root->right);
  166.  
  167. root->klucz = temp->klucz;
  168.  
  169. root->right = usun(root->right, temp->klucz);
  170.  
  171. }
  172. return root;
  173.  
  174.  
  175.  
  176. }
  177.  
  178. BSTNode* usuwaj(BSTNode* root){ //pierwszy argument to head
  179. if (root->left)
  180. usuwaj(root->left);
  181. if (root->right)
  182. usuwaj(root->right);
  183. delete(root);
  184. if (root)
  185. root = NULL;
  186.  
  187.  
  188. return NULL;
  189. }
  190.  
  191.  
  192.  
  193.  
  194. void preorder(BSTNode* root)
  195. {
  196. int liczba = 0;
  197. {
  198. if (root == NULL)
  199. return;
  200. cout << "PREORDER-------------------------- ";
  201. cout << root->klucz << " "<<endl;
  202. liczba++;
  203.  
  204. preorder(root->left);
  205. preorder(root->right);
  206. }
  207.  
  208. }
  209.  
  210.  
  211. void inorder(BSTNode * root)
  212. {
  213. if (root)
  214. {
  215. inorder(root->left);
  216. cout << "INORDER------------------------ ";
  217. cout << root->klucz << endl; // tutaj przetwarzamy bieżący węzeł
  218. inorder(root->right);
  219. }
  220. }
  221.  
  222.  
  223. void postorder(BSTNode* root)
  224. {
  225. if (root == NULL)
  226. return;
  227. postorder(root->left);
  228. postorder(root->right);
  229. cout << root->klucz << " ";
  230. }
  231.  
  232.  
  233. BSTNode* rotate_right(BSTNode* root, BSTNode* grandfather, BSTNode* parent, BSTNode* child)
  234. {
  235. BSTNode* tmp;
  236. if (grandfather){
  237. if (grandfather->right == parent){
  238. grandfather->right = child;
  239. }
  240. else{
  241. grandfather->left = child;
  242. }
  243. }
  244. else
  245. root = child;
  246.  
  247. if (parent->left == child){
  248. tmp = child->right;
  249. child->right = parent;
  250. parent->left = tmp;
  251. }
  252. else{
  253. tmp = child->right;
  254. child->left = parent;
  255. parent->right = tmp;
  256. }
  257.  
  258. return root;
  259.  
  260.  
  261. }
  262.  
  263. BSTNode* rotate_left(BSTNode* root, BSTNode* grandfather, BSTNode* parent, BSTNode* child)
  264. {
  265. BSTNode* tmp;
  266. if (grandfather){
  267. if (grandfather->right == parent){
  268. grandfather->right = child;
  269. }
  270. else{
  271. grandfather->left = child;
  272. }
  273. }
  274. else
  275. root = child;
  276.  
  277. if (parent->right == child){
  278. tmp = child->left;
  279. child->left = parent;
  280. parent->right = tmp;
  281. }
  282. else{
  283. tmp = child->left;
  284. child->right = parent;
  285. parent->left = tmp;
  286. }
  287.  
  288. return root;
  289. }
  290.  
  291. BSTNode* make_backbone(BSTNode* root)
  292. {
  293. BSTNode* grandfather=NULL;
  294. BSTNode* tmp=root;
  295. while(tmp!=NULL)
  296. {
  297. if((tmp->left)!=NULL)
  298. {
  299. BSTNode* tmp2 = tmp->left;
  300. root=rotate_right(root,grandfather,tmp,tmp->left);
  301. tmp=tmp2;
  302. }
  303. else
  304. {
  305. grandfather=tmp;
  306. tmp=tmp->right;
  307. }
  308. }
  309. return root;
  310. }
  311.  
  312. BSTNode* make_perfect_tree(BSTNode* root,int N)
  313. {
  314. BSTNode* grandfather=NULL;
  315. BSTNode* tmp=root;
  316. BSTNode* tmp2;
  317. int m=1;
  318. while(m<=N)
  319. {
  320. m=2*m+1;
  321. }
  322. m=m/2;
  323. for(int i=0;i<(N-m);i++)
  324. {
  325. tmp2=tmp->right;
  326. if(tmp2!=NULL)
  327. {
  328. root=rotate_left(root, grandfather,tmp,tmp->right);
  329. grandfather=tmp2;
  330. tmp=tmp2->right;
  331. }
  332. }
  333. while(m>1)
  334. {
  335. m=m/2;
  336. grandfather=NULL;
  337. tmp=root;
  338. for(int j=0;j<m;j++)
  339. {
  340. tmp2=tmp->right;
  341. root=rotate_left(root, grandfather,tmp,tmp->right);
  342. grandfather=tmp2;
  343. tmp=tmp2->right;
  344. }
  345. }
  346. return root;
  347. }
  348.  
  349. int maxDepth(BSTNode* root)
  350. {
  351. if (root == NULL)
  352. return 0;
  353. else
  354. {
  355. int lDepth = maxDepth(root->left);
  356. int rDepth = maxDepth(root->right);
  357. if (lDepth > rDepth)
  358. return(lDepth + 1);
  359. else return(rDepth + 1);
  360. }
  361. }
  362.  
  363.  
  364. int main()
  365. {
  366. int X1,X2;
  367. FILE* fp = fopen("inlab04.txt", "r");
  368. if (fp == NULL)
  369. return -1;
  370. fscanf(fp, "%d %d", &X1, &X2); // wczytanie pliku
  371. fclose(fp);
  372.  
  373. //czas start
  374.  
  375. clock_t begin, end;
  376. double time_spent;
  377. srand((unsigned int)time(NULL));
  378. begin = clock();
  379. BSTNode* root=new BSTNode;
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386. //wstaw X elementów do drzewa;
  387. cout << "Wstawianie X elementow do drzewa..." << endl;
  388. wstawianie(root, X1);
  389. int wysokosc1=maxDepth(root);
  390. cout<<"Wysokosc:"<<wysokosc1<<endl;
  391. root=make_backbone(root);
  392. root=make_perfect_tree(root,wysokosc1);
  393. wysokosc1=maxDepth(root);
  394. cout<<"Wysokosc po DSW:"<<wysokosc1<<endl;
  395.  
  396. usuwaj(root);
  397. root=new BSTNode;
  398. wstawianie(root,X2);
  399. int wysokosc2=maxDepth(root);
  400. cout<<"Wysokosc:"<<wysokosc2<<endl;
  401. root=make_backbone(root);
  402. root=make_perfect_tree(root,wysokosc2);
  403. wysokosc2=maxDepth(root);
  404. cout<<"Wysokosc po DSW:"<<wysokosc2<<endl;
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411. //czas stop;
  412.  
  413. end = clock();
  414. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  415.  
  416. //wypisz czas wykonania;
  417. cout << "Czas wykonania:" << time_spent << endl;
  418.  
  419. //cout << "root:" << root << " " << root->klucz<<endl;
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement