Advertisement
Ne-Biolog

Untitled

Apr 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. #include <unordered_map>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <memory.h>
  5. #include <iterator>
  6. #include <cassert>
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <cstdlib>
  10. #include <bitset>
  11. #include <vector>
  12. #include <cstdio>
  13. #include <string>
  14. #include <queue>
  15. #include <deque>
  16. #include <cmath>
  17. #include <ctime>
  18. #include <stack>
  19. #include <set>
  20. #include <map>
  21.  
  22.  
  23. using namespace std;
  24.  
  25. typedef int T;
  26.  
  27. #define CMP_EQ(a, b) ((a) == (b))
  28. #define CMP_LT(a, b) ((a) < (b))
  29. #define CMP_GT(a, b) ((a) > (b))
  30.  
  31. typedef struct Node {
  32. T data;
  33. struct Node *left;
  34. struct Node *right;
  35. struct Node *parent;
  36. } Node;
  37.  
  38.  
  39. Node* getFreeNode(T value, Node *parent) {
  40. Node* tmp = (Node*) malloc(sizeof(Node));
  41. tmp->left = tmp->right = NULL;
  42. tmp->data = value;
  43. tmp->parent = parent;
  44. return tmp;
  45. }
  46.  
  47. void insert(Node **head, int value) {
  48. Node *tmp = NULL;
  49. Node *ins = NULL;
  50. if (*head == NULL) {
  51. *head = getFreeNode(value, NULL);
  52. return;
  53. }
  54.  
  55. tmp = *head;
  56. while (tmp) {
  57. if (CMP_GT(value, tmp->data)) {
  58. if (tmp->right) {
  59. tmp = tmp->right;
  60. continue;
  61. } else {
  62. tmp->right = getFreeNode(value, tmp);
  63. return;
  64. }
  65. } else if (CMP_LT(value, tmp->data)) {
  66. if (tmp->left) {
  67. tmp = tmp->left;
  68. continue;
  69. } else {
  70. tmp->left = getFreeNode(value, tmp);
  71. return;
  72. }
  73. } else {
  74. exit(2);
  75. }
  76. }
  77. }
  78.  
  79. Node* getMinNode(Node *root) {
  80. while (root->left) {
  81. root = root->left;
  82. }
  83. return root;
  84. }
  85.  
  86.  
  87. Node* getMaxNode(Node *root) {
  88. while (root->right) {
  89. root = root->right;
  90. }
  91. return root;
  92. }
  93.  
  94. Node *getNodeByValue(Node *root, T value) {
  95. while (root) {
  96. if (CMP_GT(root->data, value)) {
  97. root = root->left;
  98. continue;
  99. } else if (CMP_LT(root->data, value)) {
  100. root = root->right;
  101. continue;
  102. } else {
  103. return root;
  104. }
  105. }
  106. return NULL;
  107. }
  108.  
  109. void removeNodeByPtr(Node *target) {
  110. if (target->left && target->right) {
  111. Node *localMax = getMaxNode(target->left);
  112. target->data = localMax->data;
  113. removeNodeByPtr(localMax);
  114. return;
  115. } else if (target->left) {
  116. if (target == target->parent->left) {
  117. target->parent->left = target->left;
  118. } else {
  119. target->parent->right = target->left;
  120. }
  121. } else if (target->right) {
  122. if (target == target->parent->right) {
  123. target->parent->right = target->right;
  124. } else {
  125. target->parent->left = target->right;
  126. }
  127. } else {
  128. if (target == target->parent->left) {
  129. target->parent->left = NULL;
  130. } else {
  131. target->parent->right = NULL;
  132. }
  133. }
  134. free(target);
  135. }
  136.  
  137.  
  138. void deleteValue(Node *root, T value) {
  139. Node *target = getNodeByValue(root, value);
  140. removeNodeByPtr(target);
  141. }
  142.  
  143. Node *addToBST(int arr[], int start, int end)
  144. {
  145. if(end < start) return NULL;
  146. int mid = (start + end)/2;
  147.  
  148. Node *r = (Node*) malloc(sizeof(Node));
  149. r->data = arr[mid];
  150. r->left = addToBST(arr, start, mid-1);
  151. r->right = addToBST(arr, mid+1, end);
  152. return r;
  153. }
  154.  
  155. Node *createMinimalBST(int arr[], int size)
  156. {
  157. return addToBST(arr,0,size-1);
  158. }
  159.  
  160. void TreeToArray(Node *node, int a[]){
  161. static int pos = 0;
  162. if(node){
  163. TreeToArray(node->left,a);
  164. a[pos++] = node->data;
  165. TreeToArray(node->right,a);
  166. }
  167. }
  168.  
  169. void printTreeReverseOrder(struct Node *node)
  170. {
  171. if(node == NULL) return;
  172. if(node->left == NULL && node->right == NULL) {
  173. cout << node->data << " ";
  174. return;
  175. }
  176.  
  177. printTreeReverseOrder(node->right);
  178. cout << node->data << " ";
  179. printTreeReverseOrder(node->left);
  180. }
  181.  
  182. void printTreePreOrder(struct Node *node)
  183. {
  184. if(node == NULL) return;
  185.  
  186. cout << node->data << " ";
  187. printTreePreOrder(node->left);
  188. printTreePreOrder(node->right);
  189. }
  190.  
  191. void printTreePostOrder(struct Node *node)
  192. {
  193. if(node == NULL) return;
  194.  
  195. printTreePostOrder(node->left);
  196. printTreePostOrder(node->right);
  197. cout << node->data << " ";
  198. }
  199.  
  200. void printTreeInOrder(struct Node *node)
  201. {
  202. if(node == NULL) return;
  203.  
  204. printTreeInOrder(node->left);
  205. cout << node->data << " ";
  206. printTreeInOrder(node->right);
  207. }
  208.  
  209. int main ()
  210. {
  211. freopen("input.txt" , "r" , stdin);
  212. freopen("output.txt" , "w" , stdout);
  213. ios_base::sync_with_stdio(false);
  214.  
  215. int a[10];
  216. for(int i = 1; i <= 10; ++i) {
  217. a[i - 1] = i;
  218. }
  219. Node *root = createMinimalBST(a, 10);
  220.  
  221.  
  222. int b[10];
  223. TreeToArray(root, b);
  224. cout << "b = ";
  225. for(int i = 0; i < 10; ++i) {
  226. cout << b[i] << ' ';
  227. }
  228.  
  229. /*insert(&root, 10);
  230. insert(&root, 12);
  231. insert(&root, 8);
  232. insert(&root, 9);
  233. insert(&root, 7);
  234. insert(&root, 3);
  235. insert(&root, 4);
  236. printTree(root, "root", 0);
  237. printf("max = %d\n", getMaxNode(root)->data);
  238. printf("min = %d\n", getMinNode(root)->data);
  239. deleteValue(root, 4);
  240. printf("parent of 7 is %d\n", getNodeByValue(root, 7)->parent->data);
  241. printTree(root, "root", 0);
  242. deleteValue(root, 8);
  243. printTree(root, "root", 0);
  244. printf("------------------\n");
  245. deleteValue(root, 10);
  246. printTree(root, "root", 0);*/
  247.  
  248.  
  249. return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement