4eyes4u

Splay tree [to do]

Oct 16th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NIL nullptr
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int key;
  8.     Node *left, *right, *parent;
  9.     Node (int x)
  10.     {
  11.         this->left=NIL;
  12.         this->right=NIL;
  13.         this->parent=NIL;
  14.         this->key=x;
  15.     }
  16. };
  17.  
  18. class SplayTree
  19. {
  20. private:
  21.     Node *root;
  22.     void zig (Node *);
  23.     void zig_zig (Node *);
  24.     void zig_zag (Node *);
  25. public:
  26.     SplayTree ();
  27.     SplayTree (int);
  28.     void splay (Node *);
  29.     void insert (int);
  30.     void remove (int);
  31.     Node* min (Node *);
  32.     Node* max (Node *);
  33.     Node* find (int);
  34. };
  35.  
  36. SplayTree::SplayTree ()
  37. {
  38.     this->root=NIL;
  39. }
  40.  
  41. SplayTree::SplayTree (int x)
  42. {
  43.     this->root=new Node(x);
  44. }
  45.  
  46. void SplayTree::zig (Node *x)
  47. {
  48.     Node *p=x->parent;
  49.  
  50.     if (p->left==x) //right rotation
  51.     {
  52.         Node *B=x->right;
  53.         p->left=B;
  54.         if (B) B->parent=p;
  55.         x->right=p;
  56.         p->parent=x;
  57.     }
  58.     else if (p->right==x) //left rotation
  59.     {
  60.         Node *B=x->left;
  61.         p->right=B;
  62.         if (B) B->parent=p;
  63.         x->left=p;
  64.         p->parent=x;
  65.     }
  66.  
  67.     x->parent=NIL;
  68. }
  69.  
  70. void SplayTree::zig_zig (Node *x)
  71. {
  72.     Node *p=x->parent;
  73.     Node *g=p->parent;
  74.     Node *gg=g->parent;
  75.  
  76.     if (g->left==p && p->left==x) //double right rotation
  77.     {
  78.         Node *B=x->right;
  79.  
  80.         p->left=B;
  81.         if (B) B->parent=p;
  82.         p->parent=g;
  83.  
  84.         g->left=p;
  85.         g->parent=x;
  86.  
  87.         x->right=g;
  88.         x->parent=gg;
  89.  
  90.         if (gg && gg->left==g) gg->left=x;
  91.         else if (gg && gg->right==g) gg->right=x;
  92.     }
  93.     else if (g->right==p && p->right==x) //double left rotation
  94.     {
  95.         Node *B=x->left;
  96.  
  97.         p->right=B;
  98.         if (B) B->parent=p;
  99.         p->parent=g;
  100.  
  101.         g->right=p;
  102.         g->parent=x;
  103.  
  104.         x->left=g;
  105.         x->parent=gg;
  106.  
  107.         if (gg && gg->left==g) gg->left=x;
  108.         else if (gg && gg->right==g) gg->right=x;
  109.     }
  110. }
  111.  
  112. void SplayTree::zig_zag (Node *x)
  113. {
  114.     Node *p=x->parent;
  115.     Node *g=p->parent;
  116.     Node *gg=g->parent;
  117.  
  118.     if (g->left==p && p->right==x) //left rotation; right rotation
  119.     {
  120.         Node *B=x->left;
  121.         Node *C=x->right;
  122.  
  123.         p->right=B;
  124.         if (B) B->parent=p;
  125.         p->parent=x;
  126.  
  127.         g->left=C;
  128.         if (C) C->parent=g;
  129.         g->parent=x;
  130.  
  131.         x->left=p;
  132.         x->right=g;
  133.         x->parent=gg;
  134.  
  135.         if (gg && gg->left==g) gg->left=x;
  136.         else if (gg && gg->right==g) gg->right=x;
  137.     }
  138.     else if (g->right==p && p->left==x) //right rotation; left rotation
  139.     {
  140.         Node *B=x->left;
  141.         Node *C=x->right;
  142.  
  143.         g->right=B;
  144.         if (B) B->parent=g;
  145.         g->parent=x;
  146.  
  147.         p->left=C;
  148.         if (C) C->parent=p;
  149.         p->parent=x;
  150.  
  151.         x->left=g;
  152.         x->right=p;
  153.         x->parent=gg;
  154.  
  155.         if (gg && gg->left==g) gg->left=x;
  156.         else if (gg && gg->right==g) gg->right=x;
  157.     }
  158. }
  159.  
  160. void SplayTree::splay (Node *x)
  161. {
  162.     while (x->parent)
  163.     {
  164.         Node *p=x->parent;
  165.         Node *g=p->parent;
  166.         if (!g) zig (x);
  167.         else if (g->left==p && p->left==x) zig_zig (x);
  168.         else if (g->right==p && p->right==x) zig_zig (x);
  169.         else if (g->left==p && p->right==x) zig_zag (x);
  170.         else if (g->right==p && p->left==x) zig_zag (x);
  171.     }
  172.  
  173.     this->root=x;
  174. }
  175.  
  176. Node* SplayTree::min (Node *x)
  177. {
  178.     Node *ret=x;
  179.     while (ret->left) ret=ret->left;
  180.     return ret;
  181. }
  182.  
  183. Node* SplayTree::max (Node *x)
  184. {
  185.     Node *ret=x;
  186.     while (ret->right) ret=ret->right;
  187.     return ret;
  188. }
  189.  
  190. void SplayTree::insert (int x)
  191. {
  192.     Node *cur=root;
  193.  
  194.     if (!this->root)
  195.     {
  196.         this->root=new Node(x);
  197.         return;
  198.     }
  199.  
  200.     while (cur)
  201.     {
  202.         if (x==cur->key)
  203.         {
  204.             splay(cur);
  205.             cur=NIL;
  206.         }
  207.         else if (x<cur->key)
  208.         {
  209.             if (cur->left) cur=cur->left;
  210.             else
  211.             {
  212.                 Node *xt=new Node(x);
  213.                 cur->left=xt;
  214.                 xt->parent=cur;
  215.                 splay(xt);
  216.                 cur=NIL;
  217.             }
  218.         }
  219.         else
  220.         {
  221.             if (cur->right) cur=cur->right;
  222.             else
  223.             {
  224.                 Node *xt=new Node(x);
  225.                 cur->right=xt;
  226.                 xt->parent=cur;
  227.                 splay(xt);
  228.                 cur=NIL;
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234. Node* SplayTree::find (int x)
  235. {
  236.     Node *ret=NIL;
  237.     Node *cur=this->root;
  238.  
  239.     while (cur)
  240.     {
  241.         if (x==cur->key)
  242.         {
  243.             ret=cur;
  244.             cur=NIL;
  245.         }
  246.         else if (x>cur->key) cur=cur->right;
  247.         else cur=cur->left;
  248.     }
  249.  
  250.     if (ret) splay (ret);
  251.     return ret;
  252. }
  253.  
  254. Node* SplayTree::remove (int x) //to do
  255. {
  256. }
  257.  
  258. int main()
  259. {
  260.     SplayTree *st=new SplayTree();
  261.  
  262.     st->insert(1);
  263.     st->insert(100);
  264.     st->insert(25);
  265.     st->insert(3);
  266.     st->insert(5);
  267.     st->insert(7);
  268.  
  269.     return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment