Advertisement
TimxAG

Untitled

Jun 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <iomanip>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. struct Node {
  10. int size;
  11. int key;
  12. struct Node *son;
  13. struct Node *brother;
  14. };
  15. void repeat(char c, int n) {for (int i = 0; i < n; i++) cout << c;}
  16. void PRINT(struct Node *tree, int space) {
  17. repeat(' ', 2*space);
  18. cout << tree->key << endl;
  19. if (tree->brother == NULL && tree->son == NULL) return;
  20. if (tree->son != NULL)
  21. {
  22. PRINT(tree->son, space + 1);
  23. }
  24. if (tree->brother != NULL) {
  25. tree = tree->brother;
  26. PRINT(tree, space);
  27. }
  28. return;
  29. }
  30. int update(struct Node *tree) {
  31. if (tree->brother == NULL && tree->son == NULL) {
  32. tree->size = 1;
  33. }
  34. if (tree->son != NULL)
  35. {
  36. update(tree->son);
  37. tree->size = tree->son->size + 1;
  38. }
  39. if (tree->brother != NULL) {
  40. update(tree->brother);
  41. tree->size = max(tree->brother->size, tree->size);
  42. }
  43. return tree->size;
  44. }
  45. struct Node *create_tree(int v) {
  46. struct Node *Tree = (struct Node *) malloc(sizeof(struct Node));
  47. Tree->key = v;
  48. Tree->son = NULL;
  49. Tree->brother = NULL;
  50. Tree->size = 1;
  51. return Tree;
  52. }
  53.  
  54. struct Node *to_son(struct Node *tree) {
  55. return tree->son;
  56. }
  57.  
  58. struct Node *to_brother(struct Node *tree) {
  59. return tree->brother;
  60. }
  61.  
  62. struct Node *last_son(struct Node *it) {
  63. it = it->son;
  64. while (it->brother != NULL) {
  65. it = it->brother;
  66. cout << it->key << endl;
  67. }
  68. return it;
  69. }
  70. bool destroy(struct Node *tree, struct Node *it) {
  71. if (it == NULL)
  72. return false;
  73. destroy(tree, it->son);
  74. if (tree != it)
  75. destroy(tree, it->brother);
  76. free(it);
  77. return true;
  78. }
  79.  
  80. struct Node* find_del(struct Node *tree, int value) {
  81. struct Node *it = tree, *t;
  82. if (it->son->key == value)
  83. {
  84. t = it->son;
  85. it->son = t->brother;
  86. t->brother = NULL;
  87. if (destroy(t, t))
  88. return tree;
  89. else
  90. return NULL;
  91. }
  92. else
  93. {
  94. it = it->son;
  95. while (it->brother != NULL) {
  96. if (it->brother->key == value) {
  97. t = it->brother;
  98. it->brother = t->brother;
  99. t->brother = NULL;
  100. if (destroy(t, t))
  101. return tree;
  102. else
  103. return NULL;
  104. }
  105. it = it->brother; }
  106. }
  107. return NULL;
  108. }
  109. struct Node* add_node(struct Node* &tree, const string& a) {
  110. int n = a.size();
  111. struct Node* t = tree;
  112. int value;
  113. cin >> value;
  114. int i = 0;
  115. while (++i < n) {
  116. if (a[i] == 'r') {
  117. tree = create_tree(value);
  118. return tree;
  119. }
  120. if (a[i] == 's') {
  121. if (t = to_son(t))
  122. continue;
  123. return NULL;
  124. }
  125. if (a[i] == 'b') {
  126. if (t = to_brother(t))
  127. continue;
  128. return NULL;
  129. }
  130. }
  131. if (t->son != NULL) {
  132. t = last_son(t);
  133. t->brother = create_tree(value);
  134. return t->brother;
  135. }
  136. else {
  137. t->son = create_tree(value);
  138. return t->son;
  139. }
  140. }
  141. struct Node* del_node(struct Node* &tree, const string& a) {
  142. int n = a.size();
  143. struct Node* t = tree;
  144. int value;
  145. cin >> value;
  146. int i = 0;
  147. while (++i < n) {
  148. if (a[i] == 's') {
  149. if (t = to_son(t))
  150. continue;
  151. return NULL;
  152. }
  153. if (a[i] == 'b') {
  154. if (t = to_brother(t))
  155. continue;
  156. return NULL;
  157. }
  158. }
  159. if (t->son != NULL) {
  160. return find_del(t, value);}
  161. return NULL;
  162. }
  163. bool destroyTree(struct Node* &it) {
  164.  
  165. if (it == NULL)
  166. return false;
  167. destroyTree(it->son);
  168. destroyTree(it->brother);
  169. free(it);
  170. it = NULL;
  171. return true;
  172. }
  173. int main()
  174. {
  175.  
  176. struct Node *tree = NULL;
  177. struct Node *ex =NULL;
  178. string a;
  179. while (cin >> a) {
  180. if (a[0] == '+') {
  181. if (add_node(tree, a) == NULL)
  182. cout << "NO" << endl;
  183. else
  184. cout << "OK" << endl;
  185. }
  186. if (a[0] == 'p') {
  187. if (tree == NULL)
  188. cout << "empty" << endl;
  189. else
  190. PRINT(tree, 0);
  191. }
  192. if (a[0] == '-') {
  193. if (a[1] == 'r') {
  194. if (destroyTree(tree))
  195. cout << "OK" << endl;
  196. else
  197. cout << "NO" << endl;
  198. continue;
  199. }
  200. if (del_node(tree, a) == NULL)
  201. cout << "NO" << endl;
  202. else
  203. cout << "OK" << endl;
  204. }
  205. if (a[0] == 't') {
  206. if (tree == NULL) {
  207. cout << "Tree no created" << endl;
  208. continue;
  209. }
  210. cout << update(tree) << endl;
  211. }
  212. }
  213. return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement