Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <deque>
- #include <string>
- #include <vector>
- using namespace std;
- typedef long long int ll;
- class Node {
- public:
- ll key;
- string value;
- Node* parent;
- Node* left;
- Node* right;
- Node(ll key, const string& value) {
- this->key = key;
- this->parent = nullptr;
- this->left = nullptr;
- this->right = nullptr;
- this->value = value;
- }
- };
- class SplayTree {
- public:
- Node *root;
- SplayTree();
- ~SplayTree();
- Node* find(ll, int);
- pair<ll, string> getMin();
- pair<ll, string> getMax();
- bool update(ll, const string&);
- bool insert(ll, const string&);
- bool remove(ll);
- void showResult(const string&);
- void show();
- bool isEmpty();
- string getResult(string delimeter);
- static void freeMemory(Node*);
- private:
- static void zig(Node*);
- static void zigZig(Node*);
- static void zigZag(Node*);
- void splay(Node*);
- };
- void SplayTree::zig(Node *node) {
- Node *p = node->parent;
- node->parent = nullptr;
- p->parent = node;
- Node *c = nullptr;
- if (p->left == node) {
- c = node->right;
- node->right = p;
- p->left = c;
- } else {
- c = node->left;
- node->left = p;
- p->right = c;
- }
- if (c != nullptr) {
- c->parent = p;
- }
- }
- void SplayTree::zigZig(Node *node) {
- Node *p = node->parent;
- Node *pp = p->parent;
- node->parent = pp->parent;
- p->parent = node;
- if (p->left == node) {
- Node *c = node->right;
- Node *pc = p->right;
- node->right = p;
- p->left = c;
- p->right = pp;
- pp->parent = p;
- pp->left = pc;
- if (node->parent != nullptr) {
- if (node->parent->left == pp)
- node->parent->left = node;
- else
- node->parent->right = node;
- }
- if (c != nullptr)
- c->parent = p;
- if (pc != nullptr)
- pc->parent = pp;
- }
- else {
- Node *c = p->left;
- Node *pc = node->left;
- node->left = p;
- p->left = pp;
- p->right = pc;
- pp->parent = p;
- pp->right = c;
- if (node->parent != nullptr) {
- if (node->parent->left == pp)
- node->parent->left = node;
- else
- node->parent->right = node;
- }
- if (c != nullptr)
- c->parent = pp;
- if (pc != nullptr)
- pc->parent = p;
- }
- }
- void SplayTree::zigZag(Node *node) {
- Node *p = node->parent;
- Node *pp = p->parent;
- Node *cl = node->left;
- Node *cr = node->right;
- node->parent = pp->parent;
- p->parent = node;
- pp->parent = node;
- if (p->right == node) {
- node->left = p;
- node->right = pp;
- p->right = cl;
- pp->left = cr;
- if (cl != nullptr)
- cl->parent = p;
- if (cr != nullptr)
- cr->parent = pp;
- } else {
- node->left = pp;
- node->right = p;
- p->left = cr;
- pp->right = cl;
- if (cl != nullptr)
- cl->parent = pp;
- if (cr != nullptr)
- cr->parent = p;
- }
- if (node->parent != nullptr) {
- if (node->parent->left == pp)
- node->parent->left = node;
- else
- node->parent->right = node;
- }
- }
- void SplayTree::splay(Node *node) {
- while (node->parent != nullptr) {
- Node *p = node->parent;
- Node *pp = p->parent;
- if (pp == nullptr){
- zig(node);
- } else if ((pp->left == p && p->left == node) || (pp->right == p && p->right == node)){
- zigZig(node);
- } else{
- zigZag(node);
- }
- }
- this->root = node;
- }
- SplayTree::SplayTree() {
- this->root = nullptr;
- }
- Node* SplayTree::find(ll key, int f) {
- Node *res = nullptr;
- Node *cur = this->root;
- Node *prev = nullptr;
- while (cur != nullptr) {
- prev = cur;
- if (key < cur->key)
- cur = cur->left;
- else if (key > cur->key)
- cur = cur->right;
- else {
- res = cur;
- break;
- }
- }
- if(f) {
- if(!(res == nullptr && f == 2)){
- if (res != nullptr)
- splay(res);
- else if (prev != nullptr)
- splay(prev);
- }
- }
- return res;
- }
- bool SplayTree::update(ll key, const string &value) {
- Node *n = find(key, 2);
- if (n == nullptr) {
- return false;
- }
- n->value = value;
- return true;
- }
- bool SplayTree::insert(ll key, const string& value) {
- if (root == nullptr) {
- root = new Node(key, value);
- return true;
- }
- Node* f = find(key, 0);
- if (f != nullptr){
- return false;
- }
- Node *cur = this->root;
- while (cur != nullptr) {
- if (key < cur->key) {
- if (cur->left == nullptr) {
- Node *node = new Node(key, value);
- cur->left = node;
- node->parent = cur;
- splay(node);
- return true;
- }
- else
- cur = cur->left;
- }
- else if (key > cur->key) {
- if (cur->right == nullptr) {
- Node *node = new Node(key, value);
- cur->right = node;
- node->parent = cur;
- splay(node);
- return true;
- }
- else
- cur = cur->right;
- } else {
- splay(cur);
- return true;
- }
- }
- }
- bool SplayTree::remove(ll key) {
- Node *n = find(key, 2);
- if (n == nullptr) {
- return false;
- }
- Node *l = n->left;
- Node *r = n->right;
- if (l == nullptr && r == nullptr) {
- this->root = nullptr;
- } else if (l == nullptr) {
- root = r;
- n->right->parent = nullptr;
- } else {
- if (r != nullptr) {
- Node *k = l;
- while (k->right != nullptr)
- k = k->right;
- splay(k);
- k->right = r;
- r->parent = k;
- } else {
- root = l;
- n->left->parent = nullptr;
- }
- }
- delete n;
- return true;
- }
- string SplayTree::getResult(string delimeter) {
- string result;
- if(root == nullptr) {
- result = "_" + delimeter;
- return result;
- }
- deque<Node*> q;
- unsigned long long count = 0;
- unsigned long long maximum = 2;
- result += "[" + to_string(root->key) + " " + root->value + "]" + delimeter;
- if(root->left == nullptr && root->right == nullptr)
- return result;
- q.push_back(root->left); q.push_back(root->right);
- string level;
- while(!q.empty()) {
- Node *elem = q.front();
- q.pop_front();
- count++;
- if (elem != nullptr) {
- if(count == maximum){
- level += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]";
- } else {
- level += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]" + " ";
- }
- q.push_back(elem->left); q.push_back(elem->right);
- } else {
- if(count == maximum) {
- level += "_";
- } else {
- level += "_ ";
- }
- q.push_back(nullptr); q.push_back(nullptr);
- }
- if(count == maximum){
- result += level + delimeter;
- level = "";
- bool check = false;
- for (auto& e : q){
- if(e != nullptr){
- check = true;
- break;
- }
- }
- if(!check){
- break;
- }
- maximum *= 2;
- count = 0;
- }
- }
- return result;
- }
- void SplayTree::showResult(const string &result) {
- cout << result;
- }
- void SplayTree::show() {
- string result;
- if(root == nullptr) {
- result = "_\n";
- showResult(result);
- return;
- }
- deque<Node*> q;
- unsigned long long count = 0;
- unsigned long long maximum = 2;
- string level;
- result = "[" + to_string(root->key) + " " + root->value + "]" + '\n';
- if(root->left == nullptr && root->right == nullptr)
- return;
- q.push_back(root->left); q.push_back(root->right);
- while(!q.empty()) {
- Node *elem = q.front();
- q.pop_front();
- count++;
- if (elem != nullptr) {
- if(count == maximum){
- result += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]";
- } else {
- result += "[" + to_string(elem->key) + " " + elem->value + " " + to_string(elem->parent->key) + "]" + " ";
- }
- q.push_back(elem->left); q.push_back(elem->right);
- } else {
- if(count == maximum) {
- result += "_";
- } else {
- result += "_ ";
- }
- q.push_back(nullptr); q.push_back(nullptr);
- }
- if(count == maximum){
- result += '\n';
- bool check = false;
- for (auto& e : q){
- if(e != nullptr){
- check = true;
- break;
- }
- }
- if(!check){
- break;
- }
- maximum *= 2;
- count = 0;
- }
- }
- showResult(result);
- }
- pair<ll, string> SplayTree::getMax() {
- Node* c = root;
- while (c->right != nullptr)
- c = c->right;
- splay(c);
- return pair<ll, string>(c->key, c->value);
- }
- pair<ll, string> SplayTree::getMin() {
- Node *c = root;
- while (c->left != nullptr)
- c = c->left;
- splay(c);
- return pair<ll, string>(c->key, c->value);
- }
- SplayTree::~SplayTree() {
- freeMemory(root);
- root = nullptr;
- }
- void SplayTree::freeMemory(Node *r) {
- if(r == nullptr)
- return;
- freeMemory(r->left);
- freeMemory(r->right);
- delete r;
- }
- bool SplayTree::isEmpty() {
- return root == nullptr;
- }
- int main() {
- SplayTree sTree;
- string operation;
- ll x, y;
- string value;
- ios_base::sync_with_stdio(false);
- while (cin >> operation) {
- if(cin.eof() || operation.length() < 1)
- break;
- if (operation == "add"){
- cin >> x >> value;
- bool f = sTree.insert(x, value);
- if(!f)
- cout << "error" << endl;
- }
- if (operation == "search"){
- cin >> x;
- Node* f = sTree.find(x, 1);
- if (f == nullptr) {
- cout << 0 << endl;
- } else {
- cout << 1 << " " << f->value << endl;
- }
- }
- if (operation == "delete") {
- cin >> x;
- bool f = sTree.remove(x);
- if(!f)
- cout << "error" << endl;
- }
- if (operation == "set") {
- cin >> x >> value;
- bool f = sTree.update(x, value);
- if(!f)
- cout << "error" << endl;
- }
- if (operation == "min"){
- if(!sTree.isEmpty()){
- pair<ll, string> p = sTree.getMin();
- cout << p.first << " " << p.second << endl;
- } else {
- cout << "error" << endl;
- }
- }
- if (operation == "max"){
- if(!sTree.isEmpty()){
- pair<ll, string> p = sTree.getMax();
- cout << p.first << " " << p.second << endl;
- } else {
- cout << "error" << endl;
- }
- }
- if (operation == "print"){/*
- string s = sTree.getResult("\n");
- cout << s;*/
- sTree.show();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement