vakho

Splay Tree

Oct 2nd, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Node {
  6. public:
  7.     int key;
  8.     Node* child[2];
  9.  
  10.     Node() {
  11.         this->child[0] = nullptr;
  12.         this->child[1] = nullptr;
  13.     }
  14.     Node(int key) {
  15.         this->key = key;
  16.         this->child[0] = nullptr;
  17.         this->child[1] = nullptr;
  18.     }
  19.     Node(int key, Node* leftChild, Node* rightChild) {
  20.         this->key = key;
  21.         this->child[0] = leftChild;
  22.         this->child[1] = rightChild;
  23.     }
  24.     ~Node() {}
  25. };
  26.  
  27. /**
  28.     Searching
  29. */
  30.  
  31. // Long version
  32. Node* search(Node* node, int key) {
  33.     if (nullptr == node || node->key == key)
  34.         return node;
  35.     if (node->key <= key)
  36.         return search(node->child[0], key);
  37.     return search(node->child[1], key);
  38. }
  39.  
  40. // Short version
  41. Node* searchShort(Node* node, int key) {
  42.     if (nullptr == node || node->key == key)
  43.         return node;
  44.     return searchShort(node->child[ node->key <= key ], key);
  45. }
  46.  
  47.  
  48. /**
  49.     Rotating
  50. */
  51.  
  52. // Rotate right
  53. Node* rotateRight(Node* x) {
  54.     if (nullptr == x || x->child[0] == nullptr)
  55.         return x;
  56.     Node* y = x->child[0];
  57.     x->child[0] = y->child[1];
  58.     y->child[1] = x;
  59.     return y;
  60. }
  61.  
  62. // Rotate left
  63. Node* rotateLeft(Node* x) {
  64.     if (nullptr == x || x->child[1] == nullptr)
  65.         return x;
  66.     Node* y = x->child[1];
  67.     x->child[1] = y->child[0];
  68.     y->child[0] = x;
  69.     return y;
  70. }
  71.  
  72. int main() {
  73.     system("COLOR F0");
  74.    
  75.  
  76.  
  77.     Node child1(-4);            Node child2(3);
  78.                 //                  //
  79.                 //                  //
  80.             Node child3(2, &child1, &child2);                   Node child4(18);
  81.                                         //                      //
  82.                                         //                      //
  83.                                     Node parent(5, &child3, &child4);
  84.  
  85.  
  86.  
  87.     Node* searchNode = searchShort(&parent, 13);
  88.  
  89.     if (nullptr == searchNode) {
  90.         cout << "Couldn't find the node!" << endl;
  91.     }
  92.     else {
  93.         cout << "Found the node with key: " << searchNode->key << endl;
  94.     }
  95.     cout << endl;
  96.  
  97.     cout << "Rotating right..." << endl;
  98.     Node* yRight = rotateRight(&parent);
  99.     cout << "Y node is: " << yRight->key << endl;
  100.  
  101.     cout << "Rotating left..." << endl;
  102.     Node* yLeft = rotateLeft(&parent);
  103.     cout << "Y node is: " << yLeft->key << endl;
  104.  
  105.     cout << "Rotating left again (Y has no right child)..." << endl;
  106.     yLeft = rotateLeft(yLeft);
  107.     if (nullptr == yLeft) {
  108.         cout << "Same situation: " << yLeft->key << endl;
  109.     } else {
  110.         cout << "Y node is: " << yLeft->key << endl;
  111.     }
  112.     cout << endl;
  113.  
  114.     system("PAUSE");
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment