Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Trie Data Structure Tutorial
Next article icon

Insertion in Splay Tree

Last Updated : 06 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

It is recommended to refer following post as prerequisite of this post.
Splay Tree | Set 1 (Search)
As discussed in the previous post, Splay tree is a self-balancing data structure where the last accessed key is always at root. The insert operation is similar to Binary Search Tree insert with additional steps to make sure that the newly inserted key becomes the new root.
Following are different cases to insert a key k in splay tree.
1) Root is NULL: We simply allocate a new node and return it as root.
2) Splay the given key k. If k is already present, then it becomes the new root. If not present, then last accessed leaf node becomes the new root.
3) If new root's key is same as k, don't do anything as k is already present.
4) Else allocate memory for new node and compare root's key with k. 
.......4.a) If k is smaller than root's key, make root as right child of new node, copy left child of root as left child of new node and make left child of root as NULL. 
.......4.b) If k is greater than root's key, make root as left child of new node, copy right child of root as right child of new node and make right child of root as NULL.
5) Return new node as new root of tree.
Example: 
 

 
100 [20] 25
/ \ \ / \
50 200 50 20 50
/ insert(25) / \ insert(25) / \
40 ======> 30 100 ========> 30 100
/ 1. Splay(25) \ \ 2. insert 25 \ \
30 40 200 40 200
/
[20]


 

C++
#include <bits/stdc++.h> using namespace std;  // An AVL tree node  class node  {      public:     int key;      node *left, *right;  };   /* Helper function that allocates  a new node with the given key and      NULL left and right pointers. */ node* newNode(int key)  {      node* Node = new node();     Node->key = key;      Node->left = Node->right = NULL;      return (Node);  }   // A utility function to right // rotate subtree rooted with y  // See the diagram given above.  node *rightRotate(node *x)  {      node *y = x->left;      x->left = y->right;      y->right = x;      return y;  }   // A utility function to left // rotate subtree rooted with x  // See the diagram given above.  node *leftRotate(node *x)  {      node *y = x->right;      x->right = y->left;      y->left = x;      return y;  }   // This function brings the key at  // root if key is present in tree.  // If key is not present, then it  // brings the last accessed item at  // root. This function modifies the  // tree and returns the new root  node *splay(node *root, int key)  {      // Base cases: root is NULL or      // key is present at root      if (root == NULL || root->key == key)          return root;       // Key lies in left subtree      if (root->key > key)      {          // Key is not in tree, we are done          if (root->left == NULL) return root;           // Zig-Zig (Left Left)          if (root->left->key > key)          {              // First recursively bring the             // key as root of left-left              root->left->left = splay(root->left->left, key);               // Do first rotation for root,              // second rotation is done after else              root = rightRotate(root);          }          else if (root->left->key < key) // Zig-Zag (Left Right)          {              // First recursively bring              // the key as root of left-right              root->left->right = splay(root->left->right, key);               // Do first rotation for root->left              if (root->left->right != NULL)                  root->left = leftRotate(root->left);          }           // Do second rotation for root          return (root->left == NULL)? root: rightRotate(root);      }      else // Key lies in right subtree      {          // Key is not in tree, we are done          if (root->right == NULL) return root;           // Zig-Zag (Right Left)          if (root->right->key > key)          {              // Bring the key as root of right-left              root->right->left = splay(root->right->left, key);               // Do first rotation for root->right              if (root->right->left != NULL)                  root->right = rightRotate(root->right);          }          else if (root->right->key < key)// Zag-Zag (Right Right)          {              // Bring the key as root of              // right-right and do first rotation              root->right->right = splay(root->right->right, key);              root = leftRotate(root);          }           // Do second rotation for root          return (root->right == NULL)? root: leftRotate(root);      }  }   // Function to insert a new key k  // in splay tree with given root  node *insert(node *root, int k)  {      // Simple Case: If tree is empty      if (root == NULL) return newNode(k);       // Bring the closest leaf node to root      root = splay(root, k);       // If key is already present, then return      if (root->key == k) return root;       // Otherwise allocate memory for new node      node *newnode = newNode(k);       // If root's key is greater, make      // root as right child of newnode      // and copy the left child of root to newnode      if (root->key > k)      {          newnode->right = root;          newnode->left = root->left;          root->left = NULL;      }       // If root's key is smaller, make      // root as left child of newnode      // and copy the right child of root to newnode      else     {          newnode->left = root;          newnode->right = root->right;          root->right = NULL;      }       return newnode; // newnode becomes new root  }   // A utility function to print  // preorder traversal of the tree.  // The function also prints height of every node  void preOrder(node *root)  {      if (root != NULL)      {          cout<<root->key<<" ";          preOrder(root->left);          preOrder(root->right);      }  }   /* Driver code*/ int main()  {      node *root = newNode(100);      root->left = newNode(50);      root->right = newNode(200);      root->left->left = newNode(40);      root->left->left->left = newNode(30);      root->left->left->left->left = newNode(20);      root = insert(root, 25);      cout<<"Preorder traversal of the modified Splay tree is \n";      preOrder(root);      return 0;  }   // This code is contributed by rathbhupendra 
C
// This code is adopted from http://algs4.cs.princeton.edu/33balanced/SplayBST.java.html #include<stdio.h> #include<stdlib.h>  // An AVL tree node struct node {     int key;     struct node *left, *right; };  /* Helper function that allocates a new node with the given key and     NULL left and right pointers. */ struct node* newNode(int key) {     struct node* node = (struct node*)malloc(sizeof(struct node));     node->key   = key;     node->left  = node->right  = NULL;     return (node); }  // A utility function to right rotate subtree rooted with y // See the diagram given above. struct node *rightRotate(struct node *x) {     struct node *y = x->left;     x->left = y->right;     y->right = x;     return y; }  // A utility function to left rotate subtree rooted with x // See the diagram given above. struct node *leftRotate(struct node *x) {     struct node *y = x->right;     x->right = y->left;     y->left = x;     return y; }  // This function brings the key at root if key is present in tree. // If key is not present, then it brings the last accessed item at // root.  This function modifies the tree and returns the new root struct node *splay(struct node *root, int key) {     // Base cases: root is NULL or key is present at root     if (root == NULL || root->key == key)         return root;      // Key lies in left subtree     if (root->key > key)     {         // Key is not in tree, we are done         if (root->left == NULL) return root;          // Zig-Zig (Left Left)         if (root->left->key > key)         {             // First recursively bring the key as root of left-left             root->left->left = splay(root->left->left, key);              // Do first rotation for root, second rotation is done after else             root = rightRotate(root);         }         else if (root->left->key < key) // Zig-Zag (Left Right)         {             // First recursively bring the key as root of left-right             root->left->right = splay(root->left->right, key);              // Do first rotation for root->left             if (root->left->right != NULL)                 root->left = leftRotate(root->left);         }          // Do second rotation for root         return (root->left == NULL)? root: rightRotate(root);     }     else // Key lies in right subtree     {         // Key is not in tree, we are done         if (root->right == NULL) return root;          // Zig-Zag (Right Left)         if (root->right->key > key)         {             // Bring the key as root of right-left             root->right->left = splay(root->right->left, key);              // Do first rotation for root->right             if (root->right->left != NULL)                 root->right = rightRotate(root->right);         }         else if (root->right->key < key)// Zag-Zag (Right Right)         {             // Bring the key as root of right-right and do first rotation             root->right->right = splay(root->right->right, key);             root = leftRotate(root);         }          // Do second rotation for root         return (root->right == NULL)? root: leftRotate(root);     } }  // Function to insert a new key k in splay tree with given root struct node *insert(struct node *root, int k) {     // Simple Case: If tree is empty     if (root == NULL) return newNode(k);      // Bring the closest leaf node to root     root = splay(root, k);      // If key is already present, then return     if (root->key == k) return root;      // Otherwise allocate memory for new node     struct node *newnode  = newNode(k);      // If root's key is greater, make root as right child     // of newnode and copy the left child of root to newnode     if (root->key > k)     {         newnode->right = root;         newnode->left = root->left;         root->left = NULL;     }      // If root's key is smaller, make root as left child     // of newnode and copy the right child of root to newnode     else     {         newnode->left = root;         newnode->right = root->right;         root->right = NULL;     }      return newnode; // newnode becomes new root }  // A utility function to print preorder traversal of the tree. // The function also prints height of every node void preOrder(struct node *root) {     if (root != NULL)     {         printf("%d ", root->key);         preOrder(root->left);         preOrder(root->right);     } }  /* Driver program to test above function*/ int main() {     struct node *root = newNode(100);     root->left = newNode(50);     root->right = newNode(200);     root->left->left = newNode(40);     root->left->left->left = newNode(30);     root->left->left->left->left = newNode(20);     root = insert(root, 25);     printf("Preorder traversal of the modified Splay tree is \n");     preOrder(root);     return 0; } 
Java
import java.util.*;  class GFG{  // An AVL tree node  static class node  {       int key;      node left, right;  };   /* Helper function that allocates  a new node with the given key and      null left and right pointers. */ static node newNode(int key)  {      node Node = new node();     Node.key = key;      Node.left = Node.right = null;      return (Node);  }   // A utility function to right // rotate subtree rooted with y  // See the diagram given above.  static node rightRotate(node x)  {      node y = x.left;      x.left = y.right;      y.right = x;      return y;  }   // A utility function to left // rotate subtree rooted with x  // See the diagram given above.  static node leftRotate(node x)  {      node y = x.right;      x.right = y.left;      y.left = x;      return y;  }   // This function brings the key at  // root if key is present in tree.  // If key is not present, then it  // brings the last accessed item at  // root. This function modifies the  // tree and returns the new root  static node splay(node root, int key)  {      // Base cases: root is null or      // key is present at root      if (root == null || root.key == key)          return root;       // Key lies in left subtree      if (root.key > key)      {          // Key is not in tree, we are done          if (root.left == null) return root;           // Zig-Zig (Left Left)          if (root.left.key > key)          {              // First recursively bring the             // key as root of left-left              root.left.left = splay(root.left.left, key);               // Do first rotation for root,              // second rotation is done after else              root = rightRotate(root);          }          else if (root.left.key < key) // Zig-Zag (Left Right)          {              // First recursively bring              // the key as root of left-right              root.left.right = splay(root.left.right, key);               // Do first rotation for root.left              if (root.left.right != null)                  root.left = leftRotate(root.left);          }           // Do second rotation for root          return (root.left == null)? root: rightRotate(root);      }      else // Key lies in right subtree      {          // Key is not in tree, we are done          if (root.right == null) return root;           // Zig-Zag (Right Left)          if (root.right.key > key)          {              // Bring the key as root of right-left              root.right.left = splay(root.right.left, key);               // Do first rotation for root.right              if (root.right.left != null)                  root.right = rightRotate(root.right);          }          else if (root.right.key < key)// Zag-Zag (Right Right)          {              // Bring the key as root of              // right-right and do first rotation              root.right.right = splay(root.right.right, key);              root = leftRotate(root);          }           // Do second rotation for root          return (root.right == null)? root: leftRotate(root);      }  }   // Function to insert a new key k  // in splay tree with given root  static node insert(node root, int k)  {      // Simple Case: If tree is empty      if (root == null) return newNode(k);       // Bring the closest leaf node to root      root = splay(root, k);       // If key is already present, then return      if (root.key == k) return root;       // Otherwise allocate memory for new node      node newnode = newNode(k);       // If root's key is greater, make      // root as right child of newnode      // and copy the left child of root to newnode      if (root.key > k)      {          newnode.right = root;          newnode.left = root.left;          root.left = null;      }       // If root's key is smaller, make      // root as left child of newnode      // and copy the right child of root to newnode      else     {          newnode.left = root;          newnode.right = root.right;          root.right = null;      }       return newnode; // newnode becomes new root  }   // A utility function to print  // preorder traversal of the tree.  // The function also prints height of every node  static void preOrder(node root)  {      if (root != null)      {          System.out.print(root.key+" ");          preOrder(root.left);          preOrder(root.right);      }  }   /* Driver code*/ public static void main(String[] args)  {      node root = newNode(100);      root.left = newNode(50);      root.right =  newNode(200);      root.left.left =  newNode(40);      root.left.left.left =  newNode(30);      root.left.left.left.left =  newNode(20);      root = insert(root, 25);      System.out.print("Preorder traversal of the modified Splay tree is \n");      preOrder(root);  }  }    // This code is contributed by Rajput-Ji  
Python
class Node:     def __init__(self, key):         self.key = key         self.left = None         self.right = None          def newNode(key):      return Node(key)       def rightRotate(x):      y = x.left      x.left = y.right      y.right = x     return y  def leftRotate(x):      y = x.right      x.right = y.left      y.left = x     return y  def splay(root, key):      # Base cases: root is None or key is present at root      if root == None or root.key == key:          return root      # Key lies in left subtree      if root.key > key:          # Key is not in tree, we are done          if root.left == None:              return root          # Zig-Zig (Left Left)          if root.left.key > key:              # First recursively bring the key as root of left-left              root.left.left = splay(root.left.left, key)              # Do first rotation for root, second rotation is done after else              root = rightRotate(root)          elif root.left.key < key: # Zig-Zag (Left Right)              # First recursively bring the key as root of left-right              root.left.right = splay(root.left.right, key)              # Do first rotation for root.left              if root.left.right != None:                  root.left = leftRotate(root.left)          # Do second rotation for root          return root if root.left == None else rightRotate(root)      else: # Key lies in right subtree          # Key is not in tree, we are done          if root.right == None:              return root          # Zig-Zag (Right Left)          if root.right.key > key:              # Bring the key as root of right-left              root.right.left = splay(root.right.left, key)              # Do first rotation for root.right              if root.right.left != None:                  root.right = rightRotate(root.right)          elif root.right.key < key: # Zag-Zag (Right Right)              # Bring the key as root of right-right and do first rotation              root.right.right = splay(root.right.right, key)              root = leftRotate(root)          # Do second rotation for root          return root if root.right == None else leftRotate(root)   # Function to insert a new key k in splay tree with given root  def insert(root, k):      # Simple Case: If tree is empty      if (root == None):          return newNode(k)      root = splay(root, k)      # If key is already present, then return      if (root.key == k):          return root      # If key is not present, then insert this      # key into the tree      # If root's key is greater, make key as      # root of root's left subtree      if (root.key > k):          n = newNode(k)          n.right = root          n.left = root.left          root.left = None         return n      else:          # If root's key is smaller, make key as          # root of root's right subtree          n = newNode(k)          n.left = root          n.right = root.right          root.right = None         return n   # A utility function to print preorder  # traversal of the tree.  # The function also prints height of every  # node  def preOrder(root):      if (root != None):          print(root.key, end=' ')          preOrder(root.left)          preOrder(root.right)   # Driver code root = newNode(100) root.left = newNode(50) root.right = newNode(200) root.left.left = newNode(40) root.left.left.left = newNode(30) root.left.left.left.left = newNode(20) root = insert(root, 25) print("Preorder traversal of the modified Splay tree is") preOrder(root) 
C#
using System;  public class node {   public int key;   public node left, right;     }  public class GFG{    /* Helper function that allocates a new node with the given key and     null left and right pointers. */   static node newNode(int key)   {     node Node = new node();     Node.key = key;     Node.left = Node.right = null;     return (Node);   }    // A utility function to right   // rotate subtree rooted with y   // See the diagram given above.   static node rightRotate(node x)   {     node y = x.left;     x.left = y.right;     y.right = x;     return y;   }    // A utility function to left   // rotate subtree rooted with x   // See the diagram given above.   static node leftRotate(node x)   {     node y = x.right;     x.right = y.left;     y.left = x;     return y;   }    // This function brings the key at   // root if key is present in tree.   // If key is not present, then it   // brings the last accessed item at   // root. This function modifies the   // tree and returns the new root   static node splay(node root, int key)   {     // Base cases: root is null or     // key is present at root     if (root == null || root.key == key)       return root;      // Key lies in left subtree     if (root.key > key)     {       // Key is not in tree, we are done       if (root.left == null) return root;        // Zig-Zig (Left Left)       if (root.left.key > key)       {         // First recursively bring the         // key as root of left-left         root.left.left = splay(root.left.left, key);          // Do first rotation for root,         // second rotation is done after else         root = rightRotate(root);       }       else if (root.left.key < key) // Zig-Zag (Left Right)       {         // First recursively bring         // the key as root of left-right         root.left.right = splay(root.left.right, key);          // Do first rotation for root.left         if (root.left.right != null)           root.left = leftRotate(root.left);       }        // Do second rotation for root       return (root.left == null)? root: rightRotate(root);     }     else // Key lies in right subtree     {       // Key is not in tree, we are done       if (root.right == null) return root;        // Zig-Zag (Right Left)       if (root.right.key > key)       {         // Bring the key as root of right-left         root.right.left = splay(root.right.left, key);          // Do first rotation for root.right         if (root.right.left != null)           root.right = rightRotate(root.right);       }       else if (root.right.key < key)// Zag-Zag (Right Right)       {         // Bring the key as root of         // right-right and do first rotation         root.right.right = splay(root.right.right, key);         root = leftRotate(root);       }        // Do second rotation for root       return (root.right == null)? root: leftRotate(root);     }   }    // Function to insert a new key k   // in splay tree with given root   static node insert(node root, int k)   {     // Simple Case: If tree is empty     if (root == null) return newNode(k);      // Bring the closest leaf node to root     root = splay(root, k);      // If key is already present, then return     if (root.key == k) return root;      // Otherwise allocate memory for new node     node newnode = newNode(k);      // If root's key is greater, make     // root as right child of newnode     // and copy the left child of root to newnode     if (root.key > k)     {       newnode.right = root;       newnode.left = root.left;       root.left = null;     }      // If root's key is smaller, make     // root as left child of newnode     // and copy the right child of root to newnode     else     {       newnode.left = root;       newnode.right = root.right;       root.right = null;     }      return newnode; // newnode becomes new root   }    // A utility function to print   // preorder traversal of the tree.   // The function also prints height of every node   static void preOrder(node root)   {     if (root != null)     {       Console.Write(root.key+" ");       preOrder(root.left);       preOrder(root.right);     }   }    /* Driver code*/   static public void Main ()   {      node root = newNode(100);     root.left = newNode(50);     root.right =  newNode(200);     root.left.left =  newNode(40);     root.left.left.left =  newNode(30);     root.left.left.left.left =  newNode(20);     root = insert(root, 25);     Console.Write("Preorder traversal of the modified Splay tree is \n");     preOrder(root);   } }  // This code is contributed by patel2127. 
JavaScript
<script>      // An AVL tree node     class Node {         constructor(val) {             this.key = val;             this.left = null;             this.right = null;         }     }      /*      Helper function that allocates a new       node with the given key and null left      and right pointers.      */      function newNode(key) {         var node = new Node();         node.key = key;         node.left = node.right = null;         return (node);     }      // A utility function to right     // rotate subtree rooted with y     // See the diagram given above.      function rightRotate( x) {         var y = x.left;         x.left = y.right;         y.right = x;         return y;     }      // A utility function to left     // rotate subtree rooted with x     // See the diagram given above.      function leftRotate( x) {         var y = x.right;         x.right = y.left;         y.left = x;         return y;     }      // This function brings the key at     // root if key is present in tree.     // If key is not present, then it     // brings the last accessed item at     // root. This function modifies the     // tree and returns the new root      function splay( root , key) {         // Base cases: root is null or         // key is present at root         if (root == null || root.key == key)             return root;          // Key lies in left subtree         if (root.key > key) {             // Key is not in tree, we are done             if (root.left == null)                 return root;              // Zig-Zig (Left Left)             if (root.left.key > key) {                 // First recursively bring the                 // key as root of left-left                 root.left.left = splay(root.left.left, key);                  // Do first rotation for root,                 // second rotation is done after else                 root = rightRotate(root);             } else if (root.left.key < key) // Zig-Zag (Left Right)             {                 // First recursively bring                 // the key as root of left-right                 root.left.right = splay(root.left.right, key);                  // Do first rotation for root.left                 if (root.left.right != null)                     root.left = leftRotate(root.left);             }              // Do second rotation for root             return (root.left == null) ? root : rightRotate(root);         } else // Key lies in right subtree         {             // Key is not in tree, we are done             if (root.right == null)                 return root;              // Zig-Zag (Right Left)             if (root.right.key > key) {                 // Bring the key as root of right-left                 root.right.left = splay(root.right.left, key);                  // Do first rotation for root.right                 if (root.right.left != null)                     root.right = rightRotate(root.right);             } else if (root.right.key < key)// Zag-Zag (Right Right)             {                 // Bring the key as root of                 // right-right and do first rotation                 root.right.right = splay(root.right.right, key);                 root = leftRotate(root);             }              // Do second rotation for root             return (root.right == null) ? root : leftRotate(root);         }     }      // Function to insert a new key k     // in splay tree with given root      function insert( root , k) {         // Simple Case: If tree is empty         if (root == null)             return newNode(k);          // Bring the closest leaf node to root         root = splay(root, k);          // If key is already present, then return         if (root.key == k)             return root;          // Otherwise allocate memory for new node         var newnode = newNode(k);          // If root's key is greater, make         // root as right child of newnode         // and copy the left child of root to newnode         if (root.key > k) {             newnode.right = root;             newnode.left = root.left;             root.left = null;         }          // If root's key is smaller, make         // root as left child of newnode         // and copy the right child of root to newnode         else {             newnode.left = root;             newnode.right = root.right;             root.right = null;         }          return newnode; // newnode becomes new root     }      // A utility function to print     // preorder traversal of the tree.     // The function also prints height of every node     function preOrder( root) {         if (root != null) {             document.write(root.key + " ");             preOrder(root.left);             preOrder(root.right);         }     }      /* Driver code */              var root = newNode(100);         root.left = newNode(50);         root.right = newNode(200);         root.left.left = newNode(40);         root.left.left.left = newNode(30);         root.left.left.left.left = newNode(20);         root = insert(root, 25);                  document.write(         "Preorder traversal of the modified Splay tree is <br/>"         );         preOrder(root);  // This code contributed by umadevi9616   </script> 

Output: 

Preorder traversal of the modified Splay tree is
25 20 50 30 40 100 200

This is an implementation of a Splay Tree data structure, which is a self-balancing binary search tree with the ability to bring the most recently accessed node to the root of the tree. The code defines a node class and several utility functions to perform operations on the tree, such as left and right rotations and insertion of new nodes.

The splay function takes a node pointer root and an integer key, and it returns a pointer to the node that contains the key after performing a splay operation. The splay operation brings the node with the key to the root of the tree, or the last accessed node if the key is not found in the tree. The splay operation is performed by rotating the tree around a node in a zig-zag or zig-zig pattern until the target node is at the root.

The insert function takes a node pointer root and an integer k, and it returns a pointer to the root of the updated tree after inserting the new key k. The function first performs a splay operation on the node with the closest value to k and then adds a new node containing k to the tree, as a child of the splayed node.

The preOrder function performs a preorder traversal of the tree and prints the keys of each node in the order they are visited.

The code in the main function creates a sample tree, performs an insertion operation with the insert function, and then prints the preorder traversal of the modified tree with the preOrder function.


This article is compiled by Abhay Rathi.
 


Next Article
Trie Data Structure Tutorial

K

kartik
Improve
Article Tags :
  • Advanced Data Structure
  • DSA
  • Self-Balancing-BST
  • splay-tree
Practice Tags :
  • Advanced Data Structure

Similar Reads

    Advanced Data Structures
    Advanced Data Structures refer to complex and specialized arrangements of data that enable efficient storage, retrieval, and manipulation of information in computer science and programming. These structures go beyond basic data types like arrays and lists, offering sophisticated ways to organize and
    2 min read
    Generic Linked List in C
    A generic linked list is a type of linked list that allows the storage of different types of data in a single linked list structure, providing more versatility and reusability in various applications. Unlike C++ and Java, C doesn't directly support generics. However, we can write generic code using
    5 min read
    Memory efficient doubly linked list
    We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the li
    9 min read
    XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
    In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists.We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR link
    15+ min read
    XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
    In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert
    10 min read
    Skip List - Efficient Search, Insert and Delete in Linked List
    A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis.In a skip list, elements are organized in layers, with
    6 min read
    Self Organizing List | Set 1 (Introduction)
    The worst case search time for a sorted linked list is O(n). With a Balanced Binary Search Tree, we can skip almost half of the nodes after one comparison with root. For a sorted array, we have random access and we can apply Binary Search on arrays. One idea to make search faster for Linked Lists is
    3 min read
    Unrolled Linked List | Set 1 (Introduction)
    Like array and linked list, the unrolled Linked List is also a linear data structure and is a variant of a linked list. Why do we need unrolled linked list? One of the biggest advantages of linked lists over arrays is that inserting an element at any location takes only O(1). However, the catch here
    10 min read

    Splay Tree

    Searching in Splay Tree
    Splay Tree- Splay tree is a binary search tree. In a splay tree, M consecutive operations can be performed in O (M log N) time. A single operation may require O(N) time but average time to perform M operations will need O (M Log N) time. When a node is accessed, it is moved to the top through a set
    15+ min read
    Insertion in Splay Tree
    It is recommended to refer following post as prerequisite of this post.Splay Tree | Set 1 (Search)As discussed in the previous post, Splay tree is a self-balancing data structure where the last accessed key is always at root. The insert operation is similar to Binary Search Tree insert with addition
    15+ min read

    Trie

    Trie Data Structure Tutorial
    The trie data structure, also known as a prefix tree, is a tree-like data structure used for efficient retrieval of key-value pairs. It is commonly used for implementing dictionaries and autocomplete features, making it a fundamental component in many search algorithms. In this article, we will expl
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences