Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <iterator>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int maxPath;
  9.  
  10.  
  11. struct Node
  12. {
  13. int countOfLeaves;
  14. int maxHeight;
  15. int maxPathsRoot;
  16. int maxPathVert;
  17. int key;
  18. Node * left;
  19. Node * right;
  20. Node(int k): left(NULL), right(NULL), key(k), countOfLeaves(0), maxHeight(0), maxPathsRoot(0),maxPathVert(0) {}
  21. Node(int k, Node * l, Node * r): left(l), right(r), key(k) {}
  22. Node(): left(NULL), right(NULL), countOfLeaves(0), maxHeight(0), maxPathsRoot(0),maxPathVert(0) {}
  23.  
  24. };
  25.  
  26. Node * root;
  27. bool start = true;
  28. int numberToDelete = 0;
  29.  
  30. void addNode(int key)
  31. {
  32. if (start)
  33. {
  34. start = false;
  35. root->key = key;
  36. return;
  37. }
  38. Node * curr = root;
  39. while (true)
  40. {
  41. if (curr->key == key) return;
  42. if (curr->left != NULL && key < curr->key)
  43. {
  44. curr = curr->left;
  45. continue;
  46. }
  47. if (curr->right != NULL && key > curr->key)
  48. {
  49. curr = curr->right;
  50. continue;
  51. }
  52. if (key < curr->key)
  53. {
  54. Node * leave = new Node(key);
  55. curr->left = leave;
  56. return;
  57. }
  58. if (key > curr->key)
  59. {
  60. Node * leave = new Node(key);
  61. curr->right = leave;
  62. return;
  63. }
  64. }
  65.  
  66.  
  67. }
  68.  
  69. void printTree(Node * root)
  70. {
  71. //cout << "Key: " << root->key << " MaxPath : " << root -> maxPathVert << " MaxRoot: " << root ->maxPathsRoot << endl;
  72. //cout << "Key: " << root->key << endl;
  73. cout << root->key << endl;
  74. if (root->left != NULL)
  75. printTree(root ->left);
  76. if (root->right != NULL)
  77. printTree(root -> right);
  78. }
  79.  
  80. void fillHeight(Node * root)
  81. {
  82. int l = -1;
  83. int r = -1;
  84. int countL = 0; int countR = 0;
  85. if (root -> left != NULL)
  86. {
  87. fillHeight(root->left);
  88. l = (root -> left) -> maxHeight;
  89. countL = (root -> left) -> countOfLeaves;
  90. }
  91. if (root -> right != NULL)
  92. {
  93. fillHeight(root->right);
  94. r = (root -> right) -> maxHeight;
  95. countR = (root -> right) -> countOfLeaves;
  96. }
  97. if (root -> right == NULL && root->left ==NULL)
  98. {
  99. root -> countOfLeaves = 1;
  100. }
  101. root -> maxHeight = max(l, r) + 1;
  102. if (l == root -> maxHeight - 1)
  103. root ->countOfLeaves += countL;
  104. if (r == root -> maxHeight - 1)
  105. root ->countOfLeaves += countR;
  106. if (maxPath < l + r + 2)
  107. {
  108. maxPath = l + r + 2;
  109. }
  110. }
  111.  
  112.  
  113.  
  114. void findMaxRoots(Node * root)
  115. {
  116. int l = -1;
  117. int r = -1;
  118. int countL = 1; int countR = 1;
  119. if (root -> left != NULL)
  120. {
  121. findMaxRoots(root->left);
  122. l = (root -> left) -> maxHeight;
  123. countL = (root -> left) -> countOfLeaves;
  124. }
  125. if (root -> right != NULL)
  126. {
  127. findMaxRoots(root->right);
  128. r = (root -> right) -> maxHeight;
  129. countR = (root -> right) -> countOfLeaves;
  130. }
  131. if (l + r + 2 == maxPath)
  132. {
  133. root -> maxPathsRoot = countL * countR;
  134. return;
  135. }
  136. root ->maxPathsRoot = 0;
  137. }
  138.  
  139.  
  140. void findPathsfromFather(Node * root)
  141. {
  142. int l = -1;
  143. int r = -1;
  144. int countL = 1; int countR = 1;
  145. if (root -> left != NULL)
  146. {
  147. //findPathsfromFather(root->left);
  148. l = (root -> left) -> maxHeight;
  149. countL = (root -> left) -> countOfLeaves;
  150. }
  151. if (root -> right != NULL)
  152. {
  153. //findPathsfromFather(root->right);
  154. r = (root -> right) -> maxHeight;
  155. countR = (root -> right) -> countOfLeaves;
  156. }
  157. if (l > r)
  158. {
  159. (root -> left)-> maxPathVert = root -> maxPathsRoot + root -> maxPathVert;
  160. if (r != -1) (root -> right)-> maxPathVert = root -> maxPathsRoot;
  161. }
  162. if (l < r)
  163. {
  164. (root -> right)-> maxPathVert = root -> maxPathsRoot + root -> maxPathVert;
  165. if (l != -1) (root -> left)-> maxPathVert = root -> maxPathsRoot;
  166. }
  167. if (l == r && l != -1)
  168. {
  169. (root -> right)-> maxPathVert = root -> maxPathsRoot + (root ->maxPathVert * countR) / (countL + countR);
  170. (root -> left)-> maxPathVert = root -> maxPathsRoot + (root ->maxPathVert * countL) / (countL + countR);
  171. }
  172.  
  173. if (root -> left != NULL)
  174. {
  175. findPathsfromFather(root->left);
  176. }
  177. if (root -> right != NULL)
  178. {
  179. findPathsfromFather(root->right);
  180. }
  181.  
  182. }
  183.  
  184. int countGoodVert(Node * elem)
  185. {
  186. if (elem == NULL) return 0;
  187. int ans = 0;
  188. if ((elem ->maxPathsRoot + elem -> maxPathVert) % 2 == 0
  189. && elem ->maxPathsRoot + elem ->maxPathVert >0)
  190. ans = 1;
  191. return ans + countGoodVert( elem -> left) + countGoodVert(elem -> right);
  192. }
  193.  
  194.  
  195.  
  196.  
  197. void deleteNode(Node * elem)
  198. {
  199. int countOfChildren = 0;
  200. if (elem ->left != NULL)
  201. countOfChildren++;
  202. if (elem ->right != NULL)
  203. countOfChildren++;
  204.  
  205. if (countOfChildren == 0)
  206. {
  207. if (root == elem)
  208. {
  209. root = NULL;
  210. return;
  211. }
  212. Node * pre = root;
  213. while (true)
  214. {
  215. if (pre->left == elem) break;
  216. if (pre->right == elem) break;
  217. if (elem->key > pre-> key)
  218. {
  219. pre = pre-> right;
  220. continue;
  221. }
  222. if (elem->key < pre-> key)
  223. {
  224. pre = pre-> left;
  225. continue;
  226. }
  227. }
  228. if (pre->left == elem) pre->left = NULL;
  229. else pre->right = NULL;
  230. return;
  231. }
  232. if (countOfChildren == 1)
  233. {
  234. Node * post;
  235. if (elem->left == NULL)
  236. post = elem -> right;
  237. else post = elem -> left;
  238. int k = elem -> key;
  239. elem-> key = post->key;
  240. post->key = k;
  241. Node * x = elem -> left;
  242. elem -> left = post->left;
  243. post ->left = x;
  244. x = elem -> right;
  245. elem-> right = post->right;
  246. post->right = x;
  247. return;
  248. }
  249.  
  250. Node * curr = elem;
  251. curr = curr ->right;
  252. if (curr->left == NULL)
  253. {
  254. int k = elem->key;
  255. elem->key = curr->key;
  256. curr->key = k;
  257. elem->right = curr->right;
  258. return;
  259. }
  260. Node * pre = curr;
  261. curr = curr->left;
  262. while (curr->left != NULL)
  263. {
  264. curr = curr->left;
  265. pre = pre->left;
  266. }
  267. int k = elem->key;
  268. elem->key = curr->key;
  269. curr->key = k;
  270. pre->left = curr->right;
  271.  
  272. }
  273.  
  274.  
  275. /*void deleteNode(Node * root)
  276. {
  277. Node * curr = root;
  278. if (curr -> right == NULL)
  279. {
  280. if (curr ->parent != NULL) curr->parent = NULL;
  281. return;
  282. }
  283. curr = curr ->right;
  284. if (curr->left == NULL)
  285. {
  286. int k = root->key;
  287. root->key = curr->key;
  288. curr->key = k;
  289. root->right = curr->right;
  290. return;
  291. }
  292. Node * pre = curr;
  293. curr = curr->left;
  294. while (curr->left != NULL)
  295. {
  296. curr = curr->left;
  297. pre = pre->left;
  298. }
  299. int k = root->key;
  300. root->key = curr->key;
  301. curr->key = k;
  302. pre->left = curr->right;
  303.  
  304. }
  305. */
  306.  
  307. void findToDelete(Node * elem)
  308. {
  309. if (elem == NULL) return;
  310. findToDelete(elem -> left);
  311. if ((elem ->maxPathsRoot + elem -> maxPathVert) % 2 == 0
  312. && elem ->maxPathsRoot + elem ->maxPathVert >0)
  313. numberToDelete--;
  314. if (numberToDelete == 0)
  315. {
  316. deleteNode(elem);
  317. numberToDelete = -10000;
  318. }
  319. findToDelete(elem -> right);
  320. }
  321.  
  322.  
  323. int main()
  324. {
  325. freopen("tst.in", "r", stdin);
  326. freopen("tst.out", "w", stdout);
  327. root = new Node();
  328. int x;
  329. while (scanf("%d", &x) != -1)
  330. {
  331. addNode(x);
  332. }
  333.  
  334. fillHeight(root);
  335. if (maxPath == 0)
  336. {
  337. printTree(root);
  338. return 0;
  339. }
  340.  
  341. findMaxRoots(root);
  342. findPathsfromFather(root);
  343.  
  344. int goodVert = countGoodVert(root);
  345. if (goodVert % 2 == 0)
  346. {
  347. printTree(root);
  348. return 0;
  349. }
  350.  
  351. numberToDelete = (goodVert + 1) / 2;
  352. findToDelete(root);
  353.  
  354.  
  355. printTree(root);
  356.  
  357. return 0;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement