Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. /*
  2.  
  3. CORRECTNESS:
  4. partial correctness : OS_select finds the element
  5. OS_delete deletes it
  6. BuildTree builds PBT
  7. total correctness : above + program terminates
  8.  
  9.  
  10.  
  11. OS_select -> cormen -> finds the i'th smallest element
  12.  
  13. OS_delete -> dsa -> deletes a node.
  14. case1 : no sons -> delete
  15. case2 : 1 son -> delete node and update links to son
  16. case3 : 2 sons -> find min in the right subtree, swap keys and delete that min
  17.  
  18. OBS: height is log(n) - perfectly balanced tree
  19.  
  20.  
  21. */
  22.  
  23. #define _CRT_SECURE_NO_WARNINGS
  24.  
  25. #include <stdio.h>
  26. #include <conio.h>
  27. #include <stdlib.h>
  28. #include "Profiler.h"
  29.  
  30. Profiler profiler("OS_Trees");
  31.  
  32. int nbOp_sel = 0;
  33. int nbOp_del = 0;
  34. int nbOp_bld = 0;
  35. struct Node
  36. {
  37. int info;
  38. Node *pLeft;
  39. Node *pRight;
  40. int size;
  41. };
  42.  
  43. Node* buildTree(int l, int r)
  44. {
  45. struct Node *pNew = (struct Node*)malloc(sizeof(struct Node));
  46. nbOp_bld++;
  47. if (l <= r)
  48. {
  49. int mid = (l + r) / 2;
  50.  
  51. nbOp_bld += 2;
  52. pNew->info = mid;
  53. pNew->size = 1;
  54.  
  55. nbOp_bld += 2;
  56. pNew->pLeft = buildTree(l, mid - 1);
  57. pNew->pRight = buildTree(mid + 1, r);
  58.  
  59.  
  60. nbOp_bld++;
  61. if (pNew->pLeft != NULL)
  62. {
  63. nbOp_bld++;
  64. pNew->size = pNew->size + pNew->pLeft->size;
  65. }
  66.  
  67. nbOp_bld++;
  68. if (pNew->pRight != NULL)
  69. {
  70. nbOp_bld++;
  71. pNew->size = pNew->size + pNew->pRight->size;
  72. }
  73.  
  74. return pNew;
  75. }
  76. else return NULL;
  77. }
  78.  
  79. void showTree_in(struct Node* pWalker, int level)
  80. {
  81. if (pWalker == NULL) return;
  82.  
  83. showTree_in(pWalker->pRight, level + 1);
  84. printf("%*s(%d,%d)\n\n", 7 * level, "", pWalker->info, pWalker->size);
  85. showTree_in(pWalker->pLeft, level + 1);
  86.  
  87. }
  88.  
  89. Node * OS_Select(struct Node* root, int i)
  90. {
  91. nbOp_sel++;
  92. if (root != NULL)
  93. {
  94. int r;
  95. nbOp_sel++;
  96. if (root->pLeft != NULL) r = root->pLeft->size + 1;
  97. else r = 1;
  98. if (i == r) return root;
  99. else
  100. {
  101. if (i < r) return OS_Select(root->pLeft, i);
  102. else return OS_Select(root->pRight, i - r);
  103. }
  104. }
  105. else return NULL;
  106.  
  107. }
  108.  
  109. struct Node* findMin(struct Node* pNode)
  110. {
  111. nbOp_del++;
  112. if (pNode == NULL) return NULL;
  113. nbOp_del++;
  114. if (pNode->pLeft != NULL) return findMin(pNode->pLeft);
  115. else
  116. return pNode;
  117.  
  118. }
  119.  
  120. struct Node* OS_delete_node(struct Node* pNode, int x)
  121. {
  122. nbOp_del++;
  123. if (pNode == NULL)
  124. {
  125. printf("\nElement %c not found.\n", x);
  126. }
  127. else
  128. {
  129. nbOp_del++;
  130. if (x < pNode->info)
  131. {
  132. pNode->pLeft = OS_delete_node(pNode->pLeft, x);
  133. pNode->size--;
  134. nbOp_del++;
  135. }
  136. else
  137. {
  138. nbOp_del++;
  139. if (x > pNode->info)
  140. {
  141. pNode->pRight = OS_delete_node(pNode->pRight, x);
  142. pNode->size--;
  143. nbOp_del++;
  144. }
  145. else
  146. {
  147. nbOp_del +=2;
  148. if (pNode->pRight != NULL && pNode->pLeft != NULL)
  149. {
  150. pNode->size--;
  151. nbOp_del++;
  152. struct Node* temp = findMin(pNode->pRight);
  153. pNode->info = temp->info;
  154. nbOp_del++;
  155. pNode->pRight = OS_delete_node(pNode->pRight, temp->info);
  156. }
  157. else
  158. {
  159. struct Node* temp = pNode;
  160. nbOp_del++;
  161. if (pNode->pLeft == NULL)
  162. {
  163. pNode = pNode->pRight;
  164. }
  165. else
  166. {
  167. nbOp_del++;
  168.  
  169. if (pNode->pRight == NULL)
  170. {
  171. pNode = pNode->pLeft;
  172. }
  173. }
  174. free(temp);
  175. }
  176. }
  177. }
  178. }
  179. return pNode;
  180. }
  181.  
  182. int main()
  183. {
  184.  
  185. printf("For demo press 1 , for actual stuff press 2 .\n");
  186. int demo;
  187. scanf("%d", &demo);
  188.  
  189. if (demo == 1)
  190. {
  191. struct Node *root = NULL;
  192. int n = 11;
  193. root = buildTree(1, n);
  194. printf("TREE : \n");
  195. showTree_in(root, 0);
  196.  
  197. printf("\n");
  198. srand(time(NULL));
  199.  
  200.  
  201.  
  202. // random select & delete
  203. int a = 2;// rand() % (n) + 1;
  204. printf("FIND the %d-th smallest ! \n", a);
  205. if (OS_Select(root, a) != NULL) printf("Found %d , now we will DELETE IT ! \n", OS_Select(root, a)->info);
  206. root = OS_delete_node(root, OS_Select(root, a)->info);
  207. showTree_in(root, 0);
  208. n--;
  209.  
  210. int b = 3; //rand() % (n - 1) + 1;
  211. printf("\nFIND the %d-th smallest ! \n", b);
  212. if (OS_Select(root, b) != NULL) printf("Found %d , now we will DELETE IT ! \n", OS_Select(root, b)->info);
  213. root = OS_delete_node(root, OS_Select(root, b)->info);
  214. showTree_in(root, 0);
  215. n--;
  216.  
  217. int c = 4; //rand() % (n - 1) + 1;
  218. printf("\nFIND the %d-th smallest ! \n", c);
  219. if (OS_Select(root, c) != NULL) printf("Found %d , now we will DELETE IT ! \n", OS_Select(root, c)->info);
  220. root = OS_delete_node(root, OS_Select(root, c)->info);
  221. showTree_in(root, 0);
  222. n--;
  223.  
  224.  
  225. }
  226. else
  227. {
  228. int n;
  229. for (n = 100; n <= 10000; n += 100)
  230. {
  231.  
  232. nbOp_sel = 0;
  233. nbOp_del = 0;
  234. nbOp_bld = 0;
  235. struct Node *root = NULL;
  236. root = buildTree(1, n);
  237.  
  238. int i;
  239. srand(time(NULL));
  240. int nnn = n;
  241. for (i = 0; i < n; i++)
  242. {
  243. int xth_smallest = rand() % (nnn)+1;
  244. struct Node *val = OS_Select(root, xth_smallest);
  245.  
  246. root = OS_delete_node(root, val->info);
  247. nnn--;
  248. }
  249.  
  250. profiler.countOperation("nbOp_SEL", n, nbOp_sel);
  251. profiler.countOperation("nbOp_DEL", n, nbOp_del);
  252. profiler.countOperation("nbOp_BLD", n, nbOp_bld);
  253. profiler.countOperation("sumOps", n, nbOp_sel+nbOp_del+nbOp_bld);
  254.  
  255. profiler.createGroup("ops", "nbOp_SEL", "nbOp_DEL","nbOp_BLD", "sumOps");
  256. }
  257. profiler.showReport();
  258. }
  259. printf("gata");
  260.  
  261.  
  262.  
  263. _getch();
  264. return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement