Advertisement
Guest User

Q

a guest
Jul 10th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. template<typename Key, typename Value>
  7. class SplayBST{
  8.     struct Node{
  9.         Key key;
  10.         Value value;
  11.         Node *left, *right;
  12.         Node(){}
  13.         Node(Key key, Value value){
  14.             this->key = key, this->value = value;
  15.             this->left  = this->right  = NULL;
  16.         }
  17.     };
  18. // Get value
  19. public:
  20.     Node* root = NULL;
  21.  
  22.     Value get(Key key){
  23.         root = splay(root, key);
  24.         if (root->key == key){
  25.             return root->value;
  26.         } else {
  27.             return NULL;
  28.         }
  29.     }
  30. // insert
  31.     void put(Key key, Value value){
  32.         if (root == NULL){
  33.             root = new Node(key, value);
  34.             return;
  35.         }
  36.         root = splay(root, key);
  37.         if (key < root->key){
  38.             Node* n = new Node(key, value);
  39.             n->left = root->left;
  40.             n->right= root;
  41.             root->left = NULL;
  42.             root = n;
  43.         } else if (key > root->key){
  44.             Node* n = new Node(key, value);
  45.             n->right = root->right;
  46.             n->left = root;
  47.             root->right = NULL;
  48.             root = n;
  49.         } else {
  50.             root->value = value;
  51.         }
  52.     }
  53.     Node *splay(Node* h, Key key){
  54.         if (h == NULL) return NULL;
  55.         if (key < h->key){
  56.             if (h->left == NULL)
  57.                 return h;
  58.             if (key < h->left->key){
  59.                 h->left->left  = splay(h->left->left, key);
  60.                 h = rotateRight(h);
  61.             } else if (key > h->key){
  62.                 h->left->right = splay(h->left->right, key);
  63.                 if(h->left->right != NULL)
  64.                     h->left = rotateLeft(h->left);
  65.             }
  66.             if (h->left == NULL) return h;
  67.             else                 return rotateRight(h);
  68. // rotate left done --------------
  69.         } else if (key > h->key){
  70.             if (h->right == NULL)
  71.                 return h;
  72.             if (key > h->right->key){
  73.                 h->right->right  = splay(h->right->right, key);
  74.                 h = rotateLeft(h);
  75.             } else if (key < h->key){
  76.                 h->right->left = splay(h->right->left, key);
  77.                 if(h->right->left != NULL)
  78.                     h->right = rotateRight(h->right);
  79.             }
  80.             if (h->right == NULL) return h;
  81.             else                  return rotateLeft(h);
  82.         } else {
  83.             return h;
  84.         }
  85.     }
  86. //rotate
  87.     Node *rotateRight(Node* h){
  88.         Node *x  = h->left;
  89.         h->left  = x->right;
  90.         x->right = h;
  91.         return x;
  92.     }
  93.  
  94.     Node *rotateLeft (Node* h){
  95.         Node* x  = h->right;
  96.         h->right = x->left;
  97.         x->left  = h;
  98.         return x;
  99.     }
  100.     Value operator[] (const Key& k){
  101.         return get(k);
  102.     }
  103. //debug
  104.     void preOrder(Node *r) {
  105.         if (r != NULL){
  106.             printf("%d ", r->key);
  107.             preOrder(r->left);
  108.             preOrder(r->right);
  109.         }
  110.     }
  111.     void remove(Key key){
  112.         root = splay(root, key);
  113.         if (k == root->key){
  114.             if (root->left == NULL) {
  115.                 root = root->right;
  116.             } else {
  117.                 Node x = root->right;
  118.                 root = root->left;
  119.                 root->right = x;
  120.             }
  121.         }
  122.     }
  123.     Node *low(Node *r){
  124.         Node *tmp;
  125.         return tmp;
  126.     }
  127.     Node *getMax(Node *r){
  128.         Node *cur = r;
  129.         while (cur->right != NULL) cur = cur->right;
  130.         return cur;
  131.     }
  132.     void operator+=(SplayBST T){
  133.         root = splay(root, getMax(root)->key);
  134.         root->right = T.root;
  135.     }
  136.     SplayBST *split(Node *r){
  137.         Node *lw = low(r);
  138.         root = splay(root, lw->key);
  139.         SplayBST *Tree1 = root->right;
  140.         root->right = NULL;
  141.         SplayBST tmp[2];
  142.         tmp[0] = root;
  143.         tmp[1] = Tree1;
  144.         return tmp;
  145.     }
  146. };
  147.  
  148. int main(){
  149.     SplayBST<int, string> st1;
  150.     vector<int> arr;
  151.     st1.put(1, "QUANG");
  152.     st1.put(2, "TEST" );
  153.     st1.put(3, "test2");
  154.     st1.put(99, "LOL");
  155.     cout << st1[99];
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement