C Program to Implement Splay Tree

This is a C Program to implement Splay tree. Like AVL and Red-Black Trees, Splay tree is also self-balancing BST. The main idea of splay tree is to bring the recently accessed item to root of the tree, this makes the recently searched item to be accessible in O(1) time if accessed again. The idea is to use locality of reference.

Here is source code of the C Program to Implement Splay Tree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. // An AVL tree node
  5. struct node {
  6.     int key;
  7.     struct node *left, *right;
  8. };
  9.  
  10. /* Helper function that allocates a new node with the given key and
  11.  NULL left and right pointers. */
  12. struct node* newNode(int key) {
  13.     struct node* node = (struct node*) malloc(sizeof(struct node));
  14.     node->key = key;
  15.     node->left = node->right = NULL;
  16.     return (node);
  17. }
  18.  
  19. // A utility function to right rotate subtree rooted with y
  20. // See the diagram given above.
  21. struct node *rightRotate(struct node *x) {
  22.     struct node *y = x->left;
  23.     x->left = y->right;
  24.     y->right = x;
  25.     return y;
  26. }
  27.  
  28. // A utility function to left rotate subtree rooted with x
  29. // See the diagram given above.
  30. struct node *leftRotate(struct node *x) {
  31.     struct node *y = x->right;
  32.     x->right = y->left;
  33.     y->left = x;
  34.     return y;
  35. }
  36.  
  37. // This function brings the key at root if key is present in tree.
  38. // If key is not present, then it brings the last accessed item at
  39. // root.  This function modifies the tree and returns the new root
  40. struct node *splay(struct node *root, int key) {
  41.     // Base cases: root is NULL or key is present at root
  42.     if (root == NULL || root->key == key)
  43.         return root;
  44.  
  45.     // Key lies in left subtree
  46.     if (root->key > key) {
  47.         // Key is not in tree, we are done
  48.         if (root->left == NULL)
  49.             return root;
  50.  
  51.         // Zig-Zig (Left Left)
  52.         if (root->left->key > key) {
  53.             // First recursively bring the key as root of left-left
  54.             root->left->left = splay(root->left->left, key);
  55.  
  56.             // Do first rotation for root, second rotation is done after else
  57.             root = rightRotate(root);
  58.         } else if (root->left->key < key) // Zig-Zag (Left Right)
  59.         {
  60.             // First recursively bring the key as root of left-right
  61.             root->left->right = splay(root->left->right, key);
  62.  
  63.             // Do first rotation for root->left
  64.             if (root->left->right != NULL)
  65.                 root->left = leftRotate(root->left);
  66.         }
  67.  
  68.         // Do second rotation for root
  69.         return (root->left == NULL) ? root : rightRotate(root);
  70.     } else // Key lies in right subtree
  71.     {
  72.         // Key is not in tree, we are done
  73.         if (root->right == NULL)
  74.             return root;
  75.  
  76.         // Zag-Zig (Right Left)
  77.         if (root->right->key > key) {
  78.             // Bring the key as root of right-left
  79.             root->right->left = splay(root->right->left, key);
  80.  
  81.             // Do first rotation for root->right
  82.             if (root->right->left != NULL)
  83.                 root->right = rightRotate(root->right);
  84.         } else if (root->right->key < key)// Zag-Zag (Right Right)
  85.         {
  86.             // Bring the key as root of right-right and do first rotation
  87.             root->right->right = splay(root->right->right, key);
  88.             root = leftRotate(root);
  89.         }
  90.  
  91.         // Do second rotation for root
  92.         return (root->right == NULL) ? root : leftRotate(root);
  93.     }
  94. }
  95.  
  96. // The search function for Splay tree.  Note that this function
  97. // returns the new root of Splay Tree.  If key is present in tree
  98. // then, it is moved to root.
  99. struct node *search(struct node *root, int key) {
  100.     return splay(root, key);
  101. }
  102.  
  103. // A utility function to print preorder traversal of the tree.
  104. // The function also prints height of every node
  105. void preOrder(struct node *root) {
  106.     if (root != NULL) {
  107.         printf("%d ", root->key);
  108.         preOrder(root->left);
  109.         preOrder(root->right);
  110.     }
  111. }
  112.  
  113. int main() {
  114.     struct node *root = newNode(100);
  115.     root->left = newNode(50);
  116.     root->right = newNode(200);
  117.     root->left->left = newNode(40);
  118.     root->left->left->left = newNode(30);
  119.     root->left->left->left->left = newNode(20);
  120.  
  121.     printf("Preorder traversal of the Splay tree is \n");
  122.     preOrder(root);
  123.     root = search(root, 20);
  124.     printf("\nPreorder traversal of after search Splay tree is \n");
  125.     preOrder(root);
  126.     return 0;
  127. }

Output:

$ gcc SplayTree.c
$ ./a.out
 
Preorder traversal of the Splay tree is 
100 50 40 30 20 200 
Preorder traversal of after search Splay tree is 
20 50 30 40 100 200

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.