Advertisement
CHU2

Splay Tree

Mar 9th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using  namespace std;
  5.  
  6. struct node {
  7.     int key;
  8.     node* left, * right;
  9. };
  10.  
  11. node* newNode(int);
  12. node* rightRotation(node*);
  13. node* leftRotation(node*);
  14. node* splay(node*, int);
  15. node* insert(node*, int);
  16. void levelorder(node*);
  17. node* Delete(int, node*);
  18. node* Search(int, node*);
  19.  
  20. int main() {
  21.     node* root = NULL;
  22.     root = insert(root, 100);       //inserts nodes
  23.     root = insert(root, 50);
  24.     root = insert(root, 200);
  25.     root = insert(root, 40);
  26.     root = insert(root, 60);
  27.     int del, serch;
  28.    
  29.     cout << " Splay tree in levelorder: ";
  30.     levelorder(root);
  31.  
  32.     cout << "\n Enter a value to be deleted: ";
  33.     cin >> del;
  34.  
  35.     cout << " Splay tree after delete: ";
  36.     Delete(del, root);
  37.     levelorder(root);
  38.  
  39.     cout << "\n Enter value to be searched: ";
  40.     cin >> serch;
  41.  
  42.     cout << " Splay tree after rotation search: ";
  43.     Search(serch, root);
  44.     levelorder(root);
  45.  
  46.     return 0;
  47. }
  48.  
  49. node* newNode(int key)
  50. {
  51.     node* temp = new node;
  52.     temp->key = key;
  53.     temp->left = temp->right = NULL;
  54.     return temp;
  55. }
  56.  
  57. node* rightRotate(node* x)
  58. {
  59.     node* y = x->left;
  60.     x->left = y->right;
  61.     y->right = x;
  62.     return y;
  63. }
  64.  
  65. node* leftRotate(node* x)
  66. {
  67.     node* y = x->right;
  68.     x->right = y->left;
  69.     y->left = x;
  70.     return y;
  71. }
  72.  
  73. node* splay(node* root, int key)
  74. {
  75.     if (root == NULL || root->key == key)
  76.         return root;
  77.  
  78.     if (root->key > key) {
  79.         if (root->left == NULL)
  80.             return root;
  81.         if (root->left->key > key) {
  82.             root->left->left = splay(root->left->left, key);
  83.             root = rightRotate(root);
  84.         }
  85.         else if (root->left->key < key) {
  86.             root->left->right
  87.                 = splay(root->left->right, key);
  88.             if (root->left->right != NULL)
  89.                 root->left = leftRotate(root->left);
  90.         }
  91.         return (root->left == NULL) ? root
  92.             : rightRotate(root);
  93.     }
  94.     else {
  95.         if (root->right == NULL)
  96.             return root;
  97.         if (root->right->key > key) {
  98.             root->right->left
  99.                 = splay(root->right->left, key);
  100.             if (root->right->left != NULL)
  101.                 root->right = rightRotate(root->right);
  102.         }
  103.         else if (root->right->key < key) {
  104.             root->right->right
  105.                 = splay(root->right->right, key);
  106.             root = leftRotate(root);
  107.         }
  108.         return (root->right == NULL) ? root
  109.             : leftRotate(root);
  110.     }
  111. }
  112.  
  113. node* insert(node* root, int key)
  114. {
  115.     if (root == NULL)
  116.         return newNode(key);
  117.  
  118.     root = splay(root, key);
  119.  
  120.     if (root->key == key)
  121.         return root;
  122.  
  123.     node* temp = newNode(key);
  124.     if (root->key > key) {
  125.         temp->right = root;
  126.         temp->left = root->left;
  127.         root->left = NULL;
  128.     }
  129.     else {
  130.         temp->left = root;
  131.         temp->right = root->right;
  132.         root->right = NULL;
  133.     }
  134.     return temp;
  135. }
  136.  
  137. void levelorder(node* root) {
  138.     queue<node*> q;
  139.  
  140.     q.push(root);
  141.  
  142.     while (!q.empty()) {
  143.         node* nodea = q.front();
  144.  
  145.         cout << nodea->key << ' ';
  146.  
  147.         q.pop();
  148.  
  149.         if (nodea->left != NULL) {
  150.             q.push(nodea->left);
  151.         }
  152.         if (nodea->right != NULL) {    //for some reason, compiler skips this if its on "else if" condition
  153.             q.push(nodea->right);
  154.         }
  155.     }
  156. }
  157.  
  158. node* Delete(int key, node* root)       //delete function
  159. {
  160.     node* temp;
  161.     if (!root)
  162.         return NULL;
  163.     root = splay(root, key);
  164.     if (key != root->key)
  165.         return root;
  166.     else {
  167.         if (!root->left) {
  168.             temp = root;
  169.             root = root->right;
  170.         }
  171.         else {
  172.             temp = root;
  173.  
  174.             root = splay(root->left, key);
  175.             root->right = temp->right;
  176.         }
  177.         free(temp);
  178.         return root;
  179.     }
  180. }
  181.  
  182. node* Search(int key, node* root)       //search function uses splay function but with differnt key value
  183. {
  184.     return splay(root, key);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement