Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.24 KB | None | 0 0
  1. /** Задание 3. Задача №3_2
  2. * Дано число N и N строк. Каждая строка содержащит команду добавления или удаления натуральных чисел, а также запрос на получение k-ой порядковой статистики.
  3. * Команда добавления числа A задается положительным числом A, команда удаления числа A задается отрицательным числом “-A”.
  4. * Запрос на получение k-ой порядковой статистики задается числом k. Требуемая скорость выполнения запроса - O(log n).
  5. */
  6.  
  7. /** Признаю. Хранить информацию о родителях каждой вершины - плохая идея
  8. * Но когда я это понял, было уже поздно
  9. * В результате код вырос до непозволительных размеров
  10. */
  11.  
  12. #include <iostream>
  13. #include <vector>
  14. #include <stack>
  15. #include <cstdint>
  16.  
  17. //#define DEBUG
  18.  
  19. #ifdef DEBUG
  20. #include "visLib/visualization.h"
  21. #endif
  22.  
  23. template <typename elementType>
  24. class Comparator {
  25. public:
  26. bool operator() (const elementType &left, const elementType &right) {
  27. return left < right;
  28. }
  29. };
  30.  
  31. template <typename elementType, typename Comparator>
  32. class BinarySearchTree {
  33. public:
  34.  
  35. explicit BinarySearchTree(Comparator comparator):
  36. comparator(comparator) {}
  37.  
  38. ~BinarySearchTree() {
  39. delete root;
  40. }
  41.  
  42. void simpleInsert(elementType item) {
  43. if (isEmpty()) {
  44. root = new Node(item);
  45. } else {
  46. Node *currentNode = root;
  47.  
  48. while (currentNode != nullptr) {
  49. if (comparator(item, currentNode -> value)) {
  50. if (currentNode -> leftChild != nullptr) {
  51. currentNode = currentNode -> leftChild;
  52. } else {
  53. currentNode -> leftChild = new Node(item);
  54. break;
  55. }
  56. } else if (comparator(currentNode -> value, item)) {
  57. if (currentNode -> rightChild != nullptr) {
  58. currentNode = currentNode -> rightChild;
  59. } else {
  60. currentNode -> rightChild = new Node(item);
  61. break;
  62. }
  63. } else {
  64. return;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. void balancedInsert(elementType item) {
  71. if (isEmpty()) {
  72. root = new Node(item);
  73. } else {
  74. insert(root, nullptr, item, true);
  75. }
  76. }
  77.  
  78. bool isExist(elementType item) {
  79. return findNode(item) != nullptr;
  80. }
  81.  
  82. bool balancedErase(elementType item) {
  83. if (isEmpty()) return false;
  84. Node *currentNode = root;
  85. while (currentNode != nullptr) {
  86. if (item > currentNode -> value) {
  87. if (currentNode -> rightChild != nullptr) {
  88. currentNode = currentNode -> rightChild;
  89. } else {
  90. return false;
  91. }
  92. } else if (item < currentNode -> value){
  93. if (currentNode -> leftChild != nullptr) {
  94. currentNode = currentNode -> leftChild;
  95. } else {
  96. return false;
  97. }
  98. } else {
  99. break;
  100. }
  101. }
  102. erase(currentNode);
  103. return true;
  104. }
  105.  
  106. elementType select(int32_t orderStatistic) {
  107. Node* currentNode = root;
  108. while (currentNode != nullptr) {
  109. if (currentNode -> leftChildCount <= orderStatistic) {
  110. orderStatistic -= currentNode -> leftChildCount;
  111. if (orderStatistic == 0) {
  112. return currentNode -> value;
  113. }
  114. orderStatistic--;
  115. currentNode = currentNode -> rightChild;
  116. } else {
  117. currentNode = currentNode -> leftChild;
  118. }
  119. }
  120. return -1;
  121. }
  122.  
  123. bool isEmpty() {
  124. return root == nullptr;
  125. }
  126.  
  127. std::vector<elementType> inOrderTraversal() {
  128. return inOrder(root);
  129. }
  130.  
  131. #ifdef DEBUG
  132. void visualize() {
  133. visual.dump(root);
  134. }
  135.  
  136. void visualize(int32_t index) {
  137. visual.logDump(root, const_cast<char *>(std::to_string(index).c_str()));
  138. }
  139. #endif
  140.  
  141. private:
  142.  
  143. struct Node {
  144. elementType value;
  145. Node *leftChild = nullptr;
  146. Node *rightChild = nullptr;
  147. Node *parent = nullptr;
  148. int32_t diffHeight = 0; // h(R) - h(L)
  149. int32_t rightChildCount = 0;
  150. int32_t leftChildCount = 0;
  151.  
  152. explicit Node (elementType const &item):
  153. value(item) {}
  154.  
  155. Node (elementType const &item, Node *parent):
  156. value(item),
  157. parent(parent) {}
  158.  
  159. ~Node() {
  160. if (this != nullptr) {
  161. delete leftChild;
  162. leftChild = nullptr;
  163. delete rightChild;
  164. rightChild = nullptr;
  165. }
  166. }
  167. };
  168.  
  169. Node* findNode(elementType item) {
  170. if (isEmpty()) {
  171. return nullptr;
  172. } else {
  173. Node *currentNode = root;
  174. while (currentNode != nullptr) {
  175. if (comparator(item, currentNode -> value)) {
  176. currentNode = currentNode -> leftChild;
  177. } else if (comparator(currentNode -> value, item)) {
  178. currentNode = currentNode -> rightChild;
  179. } else {
  180. return currentNode;
  181. }
  182. }
  183. }
  184. return nullptr;
  185. }
  186.  
  187. void rotateRight(Node* &rotatingNode) {
  188. Node *previousParent = rotatingNode -> parent;
  189. bool isLeft = false;
  190. bool isRoot = false;
  191. if (previousParent == nullptr) {
  192. isRoot = true;
  193. } else if (rotatingNode == previousParent -> leftChild) {
  194. isLeft = true;
  195. }
  196. Node *previousLeftChild = rotatingNode -> leftChild;
  197. previousLeftChild -> parent = rotatingNode -> parent;
  198. rotatingNode -> leftChild = previousLeftChild -> rightChild;
  199. if (rotatingNode -> leftChild != nullptr) {
  200. rotatingNode -> leftChild -> parent = rotatingNode;
  201. }
  202. previousLeftChild -> rightChild = rotatingNode;
  203. rotatingNode -> parent = previousLeftChild;
  204.  
  205. if (previousLeftChild -> diffHeight == 0) {
  206. rotatingNode -> diffHeight = -1;
  207. previousLeftChild -> diffHeight = 1;
  208. } else {
  209. rotatingNode -> diffHeight = 0;
  210. previousLeftChild -> diffHeight = 0;
  211. }
  212. rotatingNode -> leftChildCount = previousLeftChild -> rightChildCount;
  213. previousLeftChild -> rightChildCount = rotatingNode -> rightChildCount + rotatingNode -> leftChildCount + 1;
  214. rotatingNode = previousLeftChild;
  215. if (isRoot) {
  216. root = rotatingNode;
  217. } else if (isLeft) {
  218. previousParent -> leftChild = rotatingNode;
  219. } else {
  220. previousParent -> rightChild = rotatingNode;
  221. }
  222. }
  223.  
  224. void rotateLeft(Node* &rotatingNode) {
  225. Node *previousParent = rotatingNode -> parent;
  226. bool isLeft = false;
  227. bool isRoot = false;
  228. if (previousParent == nullptr) {
  229. isRoot = true;
  230. } else if (rotatingNode == previousParent -> leftChild) {
  231. isLeft = true;
  232. }
  233. Node *previousRightChild = rotatingNode -> rightChild;
  234. previousRightChild -> parent = rotatingNode -> parent;
  235. rotatingNode -> rightChild = previousRightChild -> leftChild;
  236. if (rotatingNode -> rightChild != nullptr) {
  237. rotatingNode -> rightChild -> parent = rotatingNode;
  238. }
  239. previousRightChild -> leftChild = rotatingNode;
  240. rotatingNode -> parent = previousRightChild;
  241.  
  242. if (previousRightChild -> diffHeight == 0) {
  243. rotatingNode -> diffHeight = 1;
  244. previousRightChild -> diffHeight = -1;
  245. } else {
  246. rotatingNode -> diffHeight = 0;
  247. previousRightChild -> diffHeight = 0;
  248. }
  249. rotatingNode -> rightChildCount = previousRightChild -> leftChildCount;
  250. previousRightChild -> leftChildCount = rotatingNode -> rightChildCount + rotatingNode -> leftChildCount + 1;
  251. rotatingNode = previousRightChild;
  252. if (isRoot) {
  253. root = rotatingNode;
  254. } else if (isLeft) {
  255. previousParent -> leftChild = rotatingNode;
  256. } else {
  257. previousParent -> rightChild = rotatingNode;
  258. }
  259. }
  260.  
  261. void rotateRightLeft(Node* &rotatingNode) {
  262. Node *previousParent = rotatingNode -> parent;
  263. bool isLeft = false;
  264. bool isRoot = false;
  265. if (previousParent == nullptr) {
  266. isRoot = true;
  267. } else if (rotatingNode == previousParent -> leftChild) {
  268. isLeft = true;
  269. }
  270.  
  271. Node *pivot = rotatingNode -> rightChild;
  272. Node *bottom = pivot -> leftChild;
  273. Node *swapChildRight = bottom -> rightChild;
  274.  
  275. int32_t bottomLeftChildCount = bottom -> leftChildCount;
  276. int32_t bottomRightChildCount = bottom -> rightChildCount;
  277.  
  278. pivot -> leftChild = swapChildRight;
  279.  
  280. if (swapChildRight != nullptr) {
  281. swapChildRight -> parent = pivot;
  282. }
  283.  
  284. bottom -> rightChild = pivot;
  285. pivot -> parent = bottom;
  286. Node *swapChildLeft = bottom -> leftChild;
  287. rotatingNode -> rightChild = swapChildLeft;
  288. if (swapChildLeft != nullptr) {
  289. swapChildLeft -> parent = rotatingNode;
  290. }
  291. bottom -> leftChild = rotatingNode;
  292. rotatingNode -> parent = bottom;
  293. if (bottom -> diffHeight > 0) {
  294. rotatingNode -> diffHeight = -1;
  295. pivot -> diffHeight = 0;
  296. } else {
  297. if (bottom -> diffHeight == 0) {
  298. rotatingNode -> diffHeight = 0;
  299. pivot -> diffHeight = 0;
  300. } else {
  301. rotatingNode -> diffHeight = 0;
  302. pivot -> diffHeight = 1;
  303. }
  304. }
  305. bottom -> diffHeight = 0;
  306.  
  307. pivot -> leftChildCount = bottomRightChildCount;
  308. rotatingNode -> rightChildCount = bottomLeftChildCount;
  309. bottom -> rightChildCount = pivot -> rightChildCount + pivot -> leftChildCount + 1;
  310. bottom -> leftChildCount = rotatingNode -> rightChildCount + rotatingNode -> leftChildCount + 1;
  311.  
  312. rotatingNode = bottom;
  313. rotatingNode -> parent = previousParent;
  314. if (isRoot) {
  315. root = rotatingNode;
  316. } else if (isLeft) {
  317. previousParent -> leftChild = rotatingNode;
  318. } else {
  319. previousParent -> rightChild = rotatingNode;
  320. }
  321. int a = 5;
  322. }
  323.  
  324. void rotateLeftRight(Node* &rotatingNode) {
  325. Node *previousParent = rotatingNode -> parent;
  326. bool isLeft = false;
  327. bool isRoot = false;
  328. if (previousParent == nullptr) {
  329. isRoot = true;
  330. } else if (rotatingNode == previousParent -> leftChild) {
  331. isLeft = true;
  332. }
  333.  
  334.  
  335. Node *pivot = rotatingNode -> leftChild;
  336. Node *bottom = pivot -> rightChild;
  337. Node *swapChildLeft = bottom -> leftChild;
  338.  
  339. int32_t bottomLeftChildCount = bottom -> leftChildCount;
  340. int32_t bottomRightChildCount = bottom -> rightChildCount;
  341.  
  342. pivot -> rightChild = swapChildLeft;
  343.  
  344. if (swapChildLeft != nullptr) {
  345. swapChildLeft -> parent = pivot;
  346. }
  347.  
  348. bottom -> leftChild = pivot;
  349. pivot -> parent = bottom;
  350. Node *swapChildRight = bottom -> rightChild;
  351. rotatingNode -> leftChild = swapChildRight;
  352. if (swapChildRight != nullptr) {
  353. swapChildRight -> parent = rotatingNode;
  354. }
  355. bottom -> rightChild = rotatingNode;
  356. rotatingNode -> parent = bottom;
  357. if (bottom -> diffHeight > 0) {
  358. rotatingNode -> diffHeight = 0;
  359. pivot -> diffHeight = -1;
  360. } else {
  361. if (bottom -> diffHeight == 0) {
  362. rotatingNode -> diffHeight = 0;
  363. pivot -> diffHeight = 0;
  364. } else {
  365. rotatingNode -> diffHeight = 1;
  366. pivot -> diffHeight = 0;
  367. }
  368. }
  369. bottom -> diffHeight = 0;
  370. pivot -> rightChildCount = bottomLeftChildCount;
  371. rotatingNode -> leftChildCount = bottomRightChildCount;
  372. bottom -> rightChildCount = rotatingNode -> leftChildCount + rotatingNode -> rightChildCount + 1;
  373. bottom -> leftChildCount = pivot -> leftChildCount + pivot -> rightChildCount + 1;
  374. rotatingNode = bottom;
  375. rotatingNode -> parent = previousParent;
  376. if (isRoot) {
  377. root = rotatingNode;
  378. } else if (isLeft) {
  379. previousParent -> leftChild = rotatingNode;
  380. } else {
  381. previousParent -> rightChild = rotatingNode;
  382. }
  383. }
  384.  
  385. void insert(Node* currentNode, Node* previousNode, elementType item, bool isLeft) {
  386. if (currentNode == nullptr) {
  387. currentNode = new Node(item, previousNode);
  388. if (isLeft) {
  389. previousNode -> leftChild = currentNode;
  390. } else {
  391. previousNode -> rightChild = currentNode;
  392. }
  393.  
  394. balance(currentNode);
  395.  
  396. return;
  397. }
  398. if (comparator(item, currentNode -> value)) {
  399. currentNode -> leftChildCount++;
  400. insert(currentNode -> leftChild, currentNode, item, true);
  401. } else if (comparator(currentNode -> value, item)) {
  402. currentNode -> rightChildCount++;
  403. insert(currentNode -> rightChild, currentNode, item, false);
  404. } else {
  405. return;
  406. }
  407. }
  408.  
  409. void erase(Node* currentNode) {
  410. if ((currentNode -> rightChild == nullptr) && (currentNode -> leftChild == nullptr)) { // Sinple delete
  411. eraseBalance(currentNode);
  412. if (currentNode -> parent != nullptr) {
  413. if (currentNode == currentNode -> parent -> leftChild) {
  414. currentNode -> parent -> leftChild = nullptr;
  415. } else {
  416. currentNode -> parent -> rightChild = nullptr;
  417. }
  418. } else {
  419. root = nullptr;
  420. }
  421. currentNode -> leftChild = nullptr;
  422. currentNode -> rightChild = nullptr;
  423. delete currentNode;
  424. } else if (currentNode -> rightChild == nullptr) {
  425. eraseBalance(currentNode);
  426. if (currentNode -> parent != nullptr) {
  427. if (currentNode == currentNode -> parent -> leftChild) {
  428. currentNode -> parent -> leftChild = currentNode -> leftChild;
  429. currentNode -> leftChild -> parent = currentNode -> parent;
  430. } else {
  431. currentNode -> parent -> rightChild = currentNode -> leftChild;
  432. currentNode -> leftChild -> parent = currentNode -> parent;
  433. }
  434. } else {
  435. root = currentNode -> leftChild;
  436. currentNode -> leftChild -> parent = nullptr;
  437. }
  438. currentNode -> leftChild = nullptr;
  439. currentNode -> rightChild = nullptr;
  440. delete currentNode;
  441. } else if (currentNode -> leftChild == nullptr) {
  442. eraseBalance(currentNode);
  443. if (currentNode -> parent != nullptr) {
  444. if (currentNode == currentNode -> parent -> leftChild) {
  445. currentNode -> parent -> leftChild = currentNode -> rightChild;
  446. currentNode -> rightChild -> parent = currentNode -> parent;
  447. } else {
  448. currentNode -> parent -> rightChild = currentNode -> rightChild;
  449. currentNode -> rightChild -> parent = currentNode -> parent;
  450. }
  451. } else {
  452. root = currentNode -> rightChild;
  453. currentNode -> rightChild -> parent = nullptr;
  454. }
  455. currentNode -> leftChild = nullptr;
  456. currentNode -> rightChild = nullptr;
  457. delete currentNode;
  458. } else {
  459. Node* nodeForSwap = currentNode -> leftChild;
  460. while (nodeForSwap -> rightChild != nullptr) {
  461. nodeForSwap = nodeForSwap -> rightChild;
  462. }
  463. currentNode -> value = nodeForSwap -> value;
  464. erase(nodeForSwap);
  465. }
  466. }
  467.  
  468. void balance(Node* currentNode) {
  469. if (currentNode == nullptr) return;
  470. for (Node* parent = currentNode -> parent; parent != nullptr; parent = currentNode -> parent) {
  471. if (currentNode == parent -> rightChild) {
  472. if (parent -> diffHeight > 0) { // Rotate left or rightLeft
  473. if (currentNode -> diffHeight < 0) {
  474. rotateRightLeft(parent);
  475. break;
  476. } else {
  477. rotateLeft(parent);
  478. break;
  479. }
  480. } else if (parent -> diffHeight < 0) {
  481. parent -> diffHeight = 0;
  482. break;
  483. } else {
  484. parent -> diffHeight = 1;
  485. currentNode = parent;
  486. continue;
  487. }
  488. } else {
  489. if (parent -> diffHeight < 0) { // Rotate right or leftRight
  490. if (currentNode -> diffHeight > 0) {
  491. rotateLeftRight(parent);
  492. break;
  493. } else {
  494. rotateRight(parent);
  495. break;
  496. }
  497. } else if (parent -> diffHeight > 0) {
  498. parent -> diffHeight = 0;
  499. break;
  500. } else {
  501. parent -> diffHeight = -1;
  502. currentNode = parent;
  503. continue;
  504. }
  505. }
  506. }
  507. }
  508.  
  509. void calculateChild(Node *currentNode) {
  510. if (currentNode == nullptr) return;;
  511. while (currentNode -> parent != nullptr) {
  512. if (currentNode == currentNode -> parent -> rightChild) {
  513. currentNode -> parent -> rightChildCount--;
  514. } else {
  515. currentNode -> parent -> leftChildCount--;
  516. }
  517. currentNode = currentNode -> parent;
  518. }
  519. }
  520.  
  521. void eraseBalance(Node* currentNode) {
  522. if (currentNode == nullptr) return;
  523. for (Node* parent = currentNode -> parent; parent != nullptr; parent = parent -> parent) {
  524. if (currentNode == parent -> rightChild) {
  525. parent -> rightChildCount--;
  526. if (parent -> diffHeight < 0) { // Rotate right or leftRight
  527. if (parent -> leftChild -> diffHeight > 0) {
  528. rotateLeftRight(parent);
  529. } else {
  530. rotateRight(parent);
  531. }
  532. if (parent -> diffHeight != 0) {
  533. calculateChild(parent);
  534. break;
  535. } else {
  536. currentNode = parent;
  537. }
  538. } else if (parent -> diffHeight > 0) {
  539. parent -> diffHeight = 0;
  540. currentNode = parent;
  541. continue;
  542. } else {
  543. parent -> diffHeight = -1;
  544. calculateChild(parent);
  545. break;
  546. }
  547. } else {
  548. parent -> leftChildCount--;
  549. if (parent -> diffHeight > 0) { // Rotate left or rightLeft
  550. if (parent -> rightChild -> diffHeight < 0) {
  551. rotateRightLeft(parent);
  552. } else {
  553. rotateLeft(parent);
  554. }
  555. if (parent -> diffHeight != 0) {
  556. calculateChild(parent);
  557. break;
  558. } else {
  559. currentNode = parent;
  560. }
  561. } else if (parent -> diffHeight < 0) {
  562. parent -> diffHeight = 0;
  563. currentNode = parent;
  564. continue;
  565. } else {
  566. parent -> diffHeight = 1;
  567. calculateChild(parent);
  568. break;
  569. }
  570. }
  571. }
  572. }
  573.  
  574. std::vector<elementType> inOrder(Node* startNode) {
  575. std::stack<Node *> stack;
  576. std::vector<elementType> buffer;
  577.  
  578. if (startNode == nullptr) return buffer;
  579.  
  580. Node *currentNode = startNode;
  581.  
  582. while (currentNode != nullptr || !stack.empty()) {
  583. while (currentNode != nullptr) {
  584. stack.push(currentNode);
  585. currentNode = currentNode -> leftChild;
  586. }
  587.  
  588. currentNode = stack.top();
  589. stack.pop();
  590.  
  591. buffer.push_back(currentNode -> value);
  592.  
  593. currentNode = currentNode -> rightChild;
  594. }
  595. return buffer;
  596. }
  597.  
  598. Node *root = nullptr;
  599. Comparator comparator;
  600.  
  601. #ifdef DEBUG
  602. Visual<Node, elementType> visual;
  603. #endif
  604.  
  605. };
  606.  
  607. int main() {
  608. Comparator<int32_t> comparator;
  609. BinarySearchTree<int32_t, Comparator<int32_t > > binarySearchTree(comparator);
  610. //freopen("../testCode/test", "r", stdin);
  611. int32_t n;
  612. std::cin >> n;
  613.  
  614. for (int32_t i = 0; i < n; i++) {
  615. int32_t number;
  616. std::cin >> number;
  617. binarySearchTree.simpleInsert(number);
  618. }
  619.  
  620. std::vector<int32_t> inOrderBuffer = binarySearchTree.inOrderTraversal();
  621.  
  622. for (auto item : inOrderBuffer) {
  623. std::cout << item << " ";
  624. }
  625.  
  626. return 0;
  627. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement