Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <deque>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. typedef long long int ll;
  10.  
  11. class Node {
  12. public:
  13. ll key;
  14. string value;
  15. Node* parent;
  16. Node* left;
  17. Node* right;
  18.  
  19. Node(ll key, const string& value) {
  20. this->key = key;
  21. this->parent = nullptr;
  22. this->left = nullptr;
  23. this->right = nullptr;
  24. this->value = value;
  25. }
  26. };
  27.  
  28. class SplayTree {
  29. public:
  30. Node *root;
  31. SplayTree();
  32. ~SplayTree();
  33. Node* find(ll, int);
  34. pair<ll, string> getMin();
  35. pair<ll, string> getMax();
  36. bool update(ll, const string&);
  37. bool insert(ll, const string&);
  38. bool remove(ll);
  39. void showResult(const string&);
  40. void show();
  41. bool isEmpty();
  42. string getResult(string delimeter);
  43. static void freeMemory(Node*);
  44.  
  45. private:
  46. static void zig(Node*);
  47. static void zigZig(Node*);
  48. static void zigZag(Node*);
  49. void splay(Node*);
  50. };
  51.  
  52. void SplayTree::zig(Node *node) {
  53. Node *p = node->parent;
  54. node->parent = nullptr;
  55. p->parent = node;
  56. Node *c = nullptr;
  57.  
  58. if (p->left == node) {
  59. c = node->right;
  60. node->right = p;
  61. p->left = c;
  62. } else {
  63. c = node->left;
  64. node->left = p;
  65. p->right = c;
  66. }
  67.  
  68. if (c != nullptr) {
  69. c->parent = p;
  70. }
  71. }
  72.  
  73. void SplayTree::zigZig(Node *node) {
  74. Node *p = node->parent;
  75. Node *pp = p->parent;
  76. node->parent = pp->parent;
  77. p->parent = node;
  78.  
  79. if (p->left == node) {
  80. Node *c = node->right;
  81. Node *pc = p->right;
  82.  
  83. node->right = p;
  84.  
  85. p->left = c;
  86. p->right = pp;
  87.  
  88. pp->parent = p;
  89. pp->left = pc;
  90.  
  91. if (node->parent != nullptr) {
  92. if (node->parent->left == pp)
  93. node->parent->left = node;
  94. else
  95. node->parent->right = node;
  96. }
  97.  
  98. if (c != nullptr)
  99. c->parent = p;
  100.  
  101. if (pc != nullptr)
  102. pc->parent = pp;
  103. }
  104. else {
  105. Node *c = p->left;
  106. Node *pc = node->left;
  107.  
  108. node->left = p;
  109.  
  110. p->left = pp;
  111. p->right = pc;
  112.  
  113. pp->parent = p;
  114. pp->right = c;
  115.  
  116. if (node->parent != nullptr) {
  117. if (node->parent->left == pp)
  118. node->parent->left = node;
  119. else
  120. node->parent->right = node;
  121. }
  122.  
  123. if (c != nullptr)
  124. c->parent = pp;
  125.  
  126. if (pc != nullptr)
  127. pc->parent = p;
  128. }
  129. }
  130.  
  131. void SplayTree::zigZag(Node *node) {
  132. Node *p = node->parent;
  133. Node *pp = p->parent;
  134. Node *cl = node->left;
  135. Node *cr = node->right;
  136. node->parent = pp->parent;
  137. p->parent = node;
  138. pp->parent = node;
  139.  
  140. if (p->right == node) {
  141. node->left = p;
  142. node->right = pp;
  143. p->right = cl;
  144. pp->left = cr;
  145.  
  146. if (cl != nullptr)
  147. cl->parent = p;
  148.  
  149. if (cr != nullptr)
  150. cr->parent = pp;
  151. } else {
  152. node->left = pp;
  153. node->right = p;
  154. p->left = cr;
  155. pp->right = cl;
  156.  
  157. if (cl != nullptr)
  158. cl->parent = pp;
  159.  
  160. if (cr != nullptr)
  161. cr->parent = p;
  162. }
  163. if (node->parent != nullptr) {
  164. if (node->parent->left == pp)
  165. node->parent->left = node;
  166. else
  167. node->parent->right = node;
  168. }
  169. }
  170.  
  171. void SplayTree::splay(Node *node) {
  172. while (node->parent != nullptr) {
  173. Node *p = node->parent;
  174. Node *pp = p->parent;
  175. if (pp == nullptr){
  176. zig(node);
  177. } else if ((pp->left == p && p->left == node) || (pp->right == p && p->right == node)){
  178. zigZig(node);
  179. } else{
  180. zigZag(node);
  181. }
  182. }
  183. this->root = node;
  184. }
  185.  
  186. SplayTree::SplayTree() {
  187. this->root = nullptr;
  188. }
  189.  
  190. Node* SplayTree::find(ll key, int f) {
  191. Node *res = nullptr;
  192. Node *cur = this->root;
  193. Node *prev = nullptr;
  194. while (cur != nullptr) {
  195. prev = cur;
  196. if (key < cur->key)
  197. cur = cur->left;
  198. else if (key > cur->key)
  199. cur = cur->right;
  200. else {
  201. res = cur;
  202. break;
  203. }
  204. }
  205. if(f) {
  206. if(!(res == nullptr && f == 2)){
  207. if (res != nullptr)
  208. splay(res);
  209. else if (prev != nullptr)
  210. splay(prev);
  211. }
  212. }
  213. return res;
  214. }
  215.  
  216. bool SplayTree::update(ll key, const string &value) {
  217. Node *n = find(key, 2);
  218. if (n == nullptr) {
  219. return false;
  220. }
  221. n->value = value;
  222. return true;
  223. }
  224.  
  225. bool SplayTree::insert(ll key, const string& value) {
  226. if (root == nullptr) {
  227. root = new Node(key, value);
  228. return true;
  229. }
  230. Node* f = find(key, 0);
  231. if (f != nullptr){
  232. return false;
  233. }
  234. Node *cur = this->root;
  235. while (cur != nullptr) {
  236. if (key < cur->key) {
  237. if (cur->left == nullptr) {
  238. Node *node = new Node(key, value);
  239. cur->left = node;
  240. node->parent = cur;
  241. splay(node);
  242. return true;
  243. }
  244. else
  245. cur = cur->left;
  246. }
  247. else if (key > cur->key) {
  248. if (cur->right == nullptr) {
  249. Node *node = new Node(key, value);
  250. cur->right = node;
  251. node->parent = cur;
  252. splay(node);
  253. return true;
  254. }
  255. else
  256. cur = cur->right;
  257. } else {
  258. splay(cur);
  259. return true;
  260. }
  261. }
  262. }
  263.  
  264. bool SplayTree::remove(ll key) {
  265. Node *n = find(key, 2);
  266. if (n == nullptr) {
  267. return false;
  268. }
  269. Node *l = n->left;
  270. Node *r = n->right;
  271.  
  272. if (l == nullptr && r == nullptr) {
  273. this->root = nullptr;
  274. } else if (l == nullptr) {
  275. root = r;
  276. n->right->parent = nullptr;
  277. } else {
  278. if (r != nullptr) {
  279. Node *k = l;
  280. while (k->right != nullptr)
  281. k = k->right;
  282. splay(k);
  283. k->right = r;
  284. r->parent = k;
  285. } else {
  286. root = l;
  287. n->left->parent = nullptr;
  288. }
  289. }
  290. delete n;
  291. return true;
  292. }
  293. string SplayTree::getResult(string delimeter) {
  294. string result;
  295. if(root == nullptr) {
  296. result = "_" + delimeter;
  297. return result;
  298. }
  299. deque<Node*> q;
  300. unsigned long long count = 0;
  301. unsigned long long maximum = 2;
  302. result += "[" + to_string(root->key) + " " + root->value + "]" + delimeter;
  303. if(root->left == nullptr && root->right == nullptr)
  304. return result;
  305. q.push_back(root->left); q.push_back(root->right);
  306.  
  307. string level;
  308.  
  309. while(!q.empty()) {
  310. Node *elem = q.front();
  311. q.pop_front();
  312. count++;
  313. if (elem != nullptr) {
  314. if(count == maximum){
  315. level += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]";
  316. } else {
  317. level += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]" + " ";
  318. }
  319. q.push_back(elem->left); q.push_back(elem->right);
  320. } else {
  321. if(count == maximum) {
  322. level += "_";
  323. } else {
  324. level += "_ ";
  325. }
  326. q.push_back(nullptr); q.push_back(nullptr);
  327. }
  328. if(count == maximum){
  329. result += level + delimeter;
  330. level = "";
  331. bool check = false;
  332. for (auto& e : q){
  333. if(e != nullptr){
  334. check = true;
  335. break;
  336. }
  337. }
  338. if(!check){
  339. break;
  340. }
  341. maximum *= 2;
  342. count = 0;
  343. }
  344. }
  345. return result;
  346. }
  347. void SplayTree::showResult(const string &result) {
  348. cout << result;
  349. }
  350. void SplayTree::show() {
  351. string result;
  352. if(root == nullptr) {
  353. result = "_\n";
  354. showResult(result);
  355. return;
  356. }
  357. deque<Node*> q;
  358. unsigned long long count = 0;
  359. unsigned long long maximum = 2;
  360. string level;
  361. result = "[" + to_string(root->key) + " " + root->value + "]" + '\n';
  362. if(root->left == nullptr && root->right == nullptr)
  363. return;
  364. q.push_back(root->left); q.push_back(root->right);
  365.  
  366. while(!q.empty()) {
  367. Node *elem = q.front();
  368. q.pop_front();
  369. count++;
  370. if (elem != nullptr) {
  371. if(count == maximum){
  372. result += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]";
  373. } else {
  374. result += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]" + " ";
  375. }
  376. q.push_back(elem->left); q.push_back(elem->right);
  377. } else {
  378. if(count == maximum) {
  379. result += "_";
  380. } else {
  381. result += "_ ";
  382. }
  383. q.push_back(nullptr); q.push_back(nullptr);
  384. }
  385. if(count == maximum){
  386. result += '\n';
  387. bool check = false;
  388. for (auto& e : q){
  389. if(e != nullptr){
  390. check = true;
  391. break;
  392. }
  393. }
  394. if(!check){
  395. break;
  396. }
  397. maximum *= 2;
  398. count = 0;
  399. }
  400. }
  401. showResult(result);
  402. }
  403.  
  404. pair<ll, string> SplayTree::getMax() {
  405. Node* c = root;
  406. while (c->right != nullptr)
  407. c = c->right;
  408. splay(c);
  409. return pair<ll, string>(c->key, c->value);
  410. }
  411.  
  412. pair<ll, string> SplayTree::getMin() {
  413. Node *c = root;
  414. while (c->left != nullptr)
  415. c = c->left;
  416. splay(c);
  417. return pair<ll, string>(c->key, c->value);
  418. }
  419.  
  420.  
  421. SplayTree::~SplayTree() {
  422. freeMemory(root);
  423. root = nullptr;
  424. }
  425.  
  426. void SplayTree::freeMemory(Node *r) {
  427. if(r == nullptr)
  428. return;
  429.  
  430. freeMemory(r->left);
  431. freeMemory(r->right);
  432. delete r;
  433. }
  434.  
  435. bool SplayTree::isEmpty() {
  436. return root == nullptr;
  437. }
  438.  
  439.  
  440. int main() {
  441. SplayTree sTree;
  442. string operation;
  443. ll x, y;
  444. string value;
  445. ios_base::sync_with_stdio(false);
  446. while (cin >> operation) {
  447. if(cin.eof() || operation.length() < 1)
  448. break;
  449.  
  450. if (operation == "add"){
  451. cin >> x >> value;
  452. bool f = sTree.insert(x, value);
  453. if(!f)
  454. cout << "error" << endl;
  455. }
  456. if (operation == "search"){
  457. cin >> x;
  458. Node* f = sTree.find(x, 1);
  459. if (f == nullptr) {
  460. cout << 0 << endl;
  461. } else {
  462. cout << 1 << " " << f->value << endl;
  463. }
  464. }
  465.  
  466. if (operation == "delete") {
  467. cin >> x;
  468. bool f = sTree.remove(x);
  469. if(!f)
  470. cout << "error" << endl;
  471. }
  472.  
  473. if (operation == "set") {
  474. cin >> x >> value;
  475. bool f = sTree.update(x, value);
  476. if(!f)
  477. cout << "error" << endl;
  478. }
  479.  
  480. if (operation == "min"){
  481. if(!sTree.isEmpty()){
  482. pair<ll, string> p = sTree.getMin();
  483. cout << p.first << " " << p.second << endl;
  484. } else {
  485. cout << "error" << endl;
  486. }
  487. }
  488.  
  489. if (operation == "max"){
  490. if(!sTree.isEmpty()){
  491. pair<ll, string> p = sTree.getMax();
  492. cout << p.first << " " << p.second << endl;
  493. } else {
  494. cout << "error" << endl;
  495. }
  496. }
  497.  
  498. if (operation == "print"){/*
  499. string s = sTree.getResult("\n");
  500. cout << s;*/
  501. sTree.show();
  502. }
  503. }
  504.  
  505. return 0;
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement