Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. /*
  2. Build Tree is a procedure that builds a balanced tree from an ordered set of keys
  3.  
  4. OS_SELECT
  5. This procedure returns a pointer to the node containing the i smallest key
  6. in the subtreee rooted at x
  7. - if our node is equal to the key - what are we are looking for, we return it
  8. - it it less than we search in the left subtree and if it is greater we search in the
  9. right subtree
  10.  
  11. OS_DELETE
  12. This procedure deletes a node from the tree
  13. There are 3 cases:
  14. 1. if the node has no children(it is a leaf) we simply delete ot
  15. 2. if the node has one child, we delete the nodeand update the link to its son
  16. 3. if the node has two children, we find the smallest successor in the right subtree, we copy
  17. the content to the nodeand we delete the successor(we replace the node to the smallest right successor)
  18.  
  19. MAINTAINING SUBTREE SIZES
  20. - we increment the size of the node with every node added in the subtree
  21. - when we delete a node, we simply decrement the size of every other node that is traveresed untill that node
  22.  
  23.  
  24. */
  25. #include <iostream>
  26. #include <string>
  27. #include <cstdlib>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <list>
  31. #include <vector>
  32. #include <iterator>
  33. #include "Profiler.h"
  34. #include <time.h>
  35. #include <iomanip>
  36. #define COUNT 10
  37. Profiler profiler("HashTables");
  38. using namespace std;
  39.  
  40. typedef struct Node {
  41. int key;
  42. int size;
  43. Node* left;
  44. Node* right;
  45. } Node;
  46.  
  47. Node* create_Node(int key, int size) {
  48. Node* new_Node = (Node*)malloc(sizeof(Node));
  49. new_Node->key = key;
  50. new_Node->size = size;
  51. new_Node->left = NULL;
  52. new_Node->right = NULL;
  53. return new_Node;
  54. }
  55. Node* build_tree(int a[], int left, int right) {
  56. if (left <= right)
  57. {
  58. int m = (left + right) / 2;
  59. Node* root = create_Node(a[m], 1);
  60. root->right = build_tree(a, m + 1, right);
  61. root->left = build_tree(a, left, m - 1);
  62. if(root->right!=NULL)
  63. root->size = root->size + root->right->size;
  64. if (root->left!= NULL)
  65. root->size = root->size + root->left->size;
  66.  
  67. return root;
  68. }
  69. else return NULL;
  70. }
  71.  
  72. Node* os_select(Node* root, int i,int n) {
  73. int r;
  74. profiler.createOperation("OS-SELECT", n);
  75. if (root != NULL)
  76. {
  77. profiler.countOperation("OS-SELECT", n, 1);
  78. if (root->left == NULL) r = 1;
  79. else r = (root->left)->size + 1;
  80.  
  81. if (i == r)
  82. return root;
  83. else
  84. {
  85.  
  86. if (i < r)
  87. return os_select(root->left, i, n);
  88. else return os_select(root->right, i - r, n);
  89.  
  90. }
  91. }
  92. }
  93. Node* GetMaxValueNode(Node* root) {
  94. Node* max_node = root;
  95. while (max_node->right != NULL) {
  96. max_node = max_node->right;
  97. }
  98. return max_node;
  99. }
  100. Node* deleteNode(Node* root, int key, int n)
  101. {
  102. profiler.createOperation("OS-DELETE", n);
  103. profiler.countOperation("OS-DELETE", n, 1);
  104. if (root == NULL) return root;
  105.  
  106. root->size--;
  107. profiler.countOperation("OS-DELETE", n, 1);
  108. if (key < root->key)
  109. {
  110. root->left = deleteNode(root->left, key,n);
  111. }
  112. else if (key > root->key)
  113. {
  114. profiler.countOperation("OS-DELETE", n, 1);
  115. root->right = deleteNode(root->right, key, n);
  116. }
  117. else
  118. {
  119. profiler.countOperation("OS-DELETE", n, 2);
  120. if (root->left == NULL)
  121. {
  122. profiler.countOperation("OS-DELETE", n, 1);
  123. Node* temp = root->right;
  124. free(root);
  125. return temp;
  126. }
  127. else if (root->right == NULL)
  128. {
  129. profiler.countOperation("OS-DELETE", n, 1);
  130. Node* temp = root->left;
  131. free(root);
  132. return temp;
  133. }
  134.  
  135. Node* temp = GetMaxValueNode(root->left);
  136. profiler.countOperation("OS-DELETE", n, 1);
  137. root->key = temp->key;
  138.  
  139.  
  140. root->left = deleteNode(root->left, temp->key, n);
  141. }
  142. return root;
  143. }
  144.  
  145. Node* delete_n(Node* root, int i,int n) {
  146. Node* temp = os_select(root, i, n);
  147. return deleteNode(root, temp->key, n);
  148. }
  149. void inorder(Node* node)
  150. {
  151. if (node == NULL)
  152. return;
  153.  
  154. inorder(node->left);
  155.  
  156.  
  157. printf("(%d with size %d)\n", node->key, node->size);
  158.  
  159.  
  160. inorder(node->right);
  161. }
  162.  
  163. void prettyPrint(Node* root, int space)
  164. {
  165.  
  166. if (root == NULL)
  167. return;
  168.  
  169. space += COUNT;
  170.  
  171. prettyPrint(root->right, space);
  172. cout << endl;
  173. for (int i = COUNT; i < space; i++)
  174. cout << " ";
  175. cout << root->key << "\n";
  176.  
  177. prettyPrint(root->left, space);
  178. }
  179. void demo()
  180. {
  181. int k = 11;
  182. int* a = (int*)malloc(k * sizeof(int));
  183. FillRandomArray(a, k, 0, k - 1, true, 1);
  184. printf("Sorted array: ");
  185. for (int j = 0; j < k; j++) {
  186. printf("%d ", a[j]);
  187. }
  188.  
  189. printf("\nPretty print: ");
  190. printf("\n");
  191. Node* n = build_tree(a, 0, k - 1);
  192.  
  193. prettyPrint(n, 0);
  194. printf("\n");
  195. inorder(n);
  196. int index[3];
  197. FillRandomArray(index, 3, 1, k - 1, true,0);
  198. for (int i = 0; i < 3; i++) {
  199. printf("We delete element %d \n", a[index[i]]);
  200. n = delete_n(n, a[index[i]],i);
  201. prettyPrint(n, 0);
  202. printf("\n");
  203. inorder(n);
  204. }
  205. system("pause");
  206. }
  207.  
  208. void compute()
  209. {
  210. int r;
  211. for (int i = 100; i <= 1000; i = i + 100)
  212. {
  213. int* a = (int*)malloc(i * sizeof(int));
  214. int* index_array = (int*)malloc(i * sizeof(int));
  215.  
  216. for (int j = 1; j <= 5; j++)
  217. {
  218. FillRandomArray(a, i, 0, i-1, true, 1);
  219. Node* n = build_tree(a, 1, i);
  220.  
  221. for (int k = 0; k < i; k++)
  222. {
  223. r = rand() % (i-k) + 1;
  224. n = delete_n(n, a[r], i);
  225. }
  226. }
  227. }
  228. profiler.divideValues("OS-SELECT", 5);
  229. profiler.divideValues("OS-DELETE", 5);
  230. profiler.createGroup("AverageOperations", "OS-SELECT", "OS-DELETE");
  231. profiler.showReport();
  232. }
  233.  
  234. int main()
  235. {
  236. //demo();
  237. compute();
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement