Open In App
Related Articles

Skip List | Set 3 (Searching and Deletion)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In previous article Skip List | Set 2 (Insertion) we discussed the structure of skip nodes and how to insert an element in the skip list. In this article we will discuss how to search and delete an element from skip list.

Searching an element in Skip list

Searching an element is very similar to approach for searching a spot for inserting an element in Skip list. The basic idea is if –

  1. Key of next node is less than search key then we keep on moving forward on the same level.
  2. Key of next node is greater than the key to be inserted then we store the pointer to current node i at update[i] and move one level down and continue our search.

At the lowest level (0), if the element next to the rightmost element (update[0]) has key equal to the search key, then we have found key otherwise failure. Following is the pseudo code for searching element –

Search(list, searchKey)
x := list -> header
-- loop invariant: x -> key level downto 0 do
while x -> forward[i] -> key forward[i]
x := x -> forward[0]
if x -> key = searchKey then return x -> value
else return failure

Consider this example where we want to search for key 17- 

searching key

Deleting an element from the Skip list

Deletion of an element k is preceded by locating element in the Skip list using above mentioned search algorithm. Once the element is located, rearrangement of pointers is done to remove element form list just like we do in singly linked list. We start from lowest level and do rearrangement until element next to update[i] is not k. After deletion of element there could be levels with no elements, so we will remove these levels as well by decrementing the level of Skip list. Following is the pseudo code for deletion –

Delete(list, searchKey)
local update[0..MaxLevel+1]
x := list -> header
for i := list -> level downto 0 do
while x -> forward[i] -> key forward[i]
update[i] := x
x := x -> forward[0]
if x -> key = searchKey then
for i := 0 to list -> level do
if update[i] -> forward[i] ? x then break
update[i] -> forward[i] := x -> forward[i]
free(x)
while list -> level > 0 and list -> header -> forward[list -> level] = NIL do
list -> level := list -> level – 1

Consider this example where we want to delete element 6 – 

deletion

Here at level 3, there is no element (arrow in red) after deleting element 6. So we will decrement level of skip list by 1. Following is the code for searching and deleting element from Skip List – 

Implementation:

C++

// C++ code for searching and deleting element in skip list
 
#include <bits/stdc++.h>
using namespace std;
 
// Class to implement node
class Node
{
public:
    int key;
 
    // Array to hold pointers to node of different level
    Node **forward;
    Node(int, int);
};
 
Node::Node(int key, int level)
{
    this->key = key;
 
    // Allocate memory to forward
    forward = new Node*[level+1];
 
    // Fill forward array with 0(NULL)
    memset(forward, 0, sizeof(Node*)*(level+1));
};
 
// Class for Skip list
class SkipList
{
    // Maximum level for this skip list
    int MAXLVL;
 
    // P is the fraction of the nodes with level
    // i pointers also having level i+1 pointers
    float P;
 
    // current level of skip list
    int level;
 
    // pointer to header node
    Node *header;
public:
    SkipList(int, float);
    int randomLevel();
    Node* createNode(int, int);
    void insertElement(int);
    void deleteElement(int);
    void searchElement(int);
    void displayList();
};
 
SkipList::SkipList(int MAXLVL, float P)
{
    this->MAXLVL = MAXLVL;
    this->P = P;
    level = 0;
 
    // create header node and initialize key to -1
    header = new Node(-1, MAXLVL);
};
 
// create random level for node
int SkipList::randomLevel()
{
    float r = (float)rand()/RAND_MAX;
    int lvl = 0;
    while(r < P && lvl < MAXLVL)
    {
        lvl++;
        r = (float)rand()/RAND_MAX;
    }
    return lvl;
};
 
// create new node
Node* SkipList::createNode(int key, int level)
{
    Node *n = new Node(key, level);
    return n;
};
 
// Insert given key in skip list
void SkipList::insertElement(int key)
{
    Node *current = header;
 
    // create update array and initialize it
    Node *update[MAXLVL+1];
    memset(update, 0, sizeof(Node*)*(MAXLVL+1));
 
    /*    start from highest level of skip list
        move the current pointer forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
    */
    for(int i = level; i >= 0; i--)
    {
        while(current->forward[i] != NULL &&
              current->forward[i]->key < key)
            current = current->forward[i];
        update[i] = current;
    }
 
    /* reached level 0 and forward pointer to
       right, which is desired position to
       insert key.
    */
    current = current->forward[0];
 
    /* if current is NULL that means we have reached
       to end of the level or current's key is not equal
       to key to insert that means we have to insert
       node between update[0] and current node */
    if (current == NULL || current->key != key)
    {
        // Generate a random level for node
        int rlevel = randomLevel();
 
        /* If random level is greater than list's current
           level (node with highest level inserted in
           list so far), initialize update value with pointer
           to header for further use */
        if(rlevel > level)
        {
            for(int i=level+1;i<rlevel+1;i++)
                update[i] = header;
 
            // Update the list current level
            level = rlevel;
        }
 
        // create new node with random level generated
        Node* n = createNode(key, rlevel);
 
        // insert node by rearranging pointers
        for(int i=0;i<=rlevel;i++)
        {
            n->forward[i] = update[i]->forward[i];
            update[i]->forward[i] = n;
        }
        cout<<"Successfully Inserted key "<<key<<"\n";
    }
};
 
// Delete element from skip list
void SkipList::deleteElement(int key)
{
    Node *current = header;
 
    // create update array and initialize it
    Node *update[MAXLVL+1];
    memset(update, 0, sizeof(Node*)*(MAXLVL+1));
 
    /*    start from highest level of skip list
        move the current pointer forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
    */
    for(int i = level; i >= 0; i--)
    {
        while(current->forward[i] != NULL  &&
              current->forward[i]->key < key)
            current = current->forward[i];
        update[i] = current;
    }
 
    /* reached level 0 and forward pointer to
       right, which is possibly our desired node.*/
    current = current->forward[0];
 
    // If current node is target node
    if(current != NULL and current->key == key)
    {
        /* start from lowest level and rearrange
           pointers just like we do in singly linked list
           to remove target node */
        for(int i=0;i<=level;i++)
        {
            /* If at level i, next node is not target
               node, break the loop, no need to move
              further level */
            if(update[i]->forward[i] != current)
                break;
 
            update[i]->forward[i] = current->forward[i];
        }
 
        // Remove levels having no elements
        while(level>0 &&
              header->forward[level] == 0)
            level--;
         cout<<"Successfully deleted key "<<key<<"\n";
    }
};
 
// Search for element in skip list
void SkipList::searchElement(int key)
{
    Node *current = header;
 
    /*    start from highest level of skip list
        move the current pointer forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
    */
    for(int i = level; i >= 0; i--)
    {
        while(current->forward[i] &&
               current->forward[i]->key < key)
            current = current->forward[i];
 
    }
 
    /* reached level 0 and advance pointer to
       right, which is possibly our desired node*/
    current = current->forward[0];
 
    // If current node have key equal to
    // search key, we have found our target node
    if(current and current->key == key)
        cout<<"Found key: "<<key<<"\n";
};
 
// Display skip list level wise
void SkipList::displayList()
{
    cout<<"\n*****Skip List*****"<<"\n";
    for(int i=0;i<=level;i++)
    {
        Node *node = header->forward[i];
        cout<<"Level "<<i<<": ";
        while(node != NULL)
        {
            cout<<node->key<<" ";
            node = node->forward[i];
        }
        cout<<"\n";
    }
};
 
// Driver to test above code
int main()
{
    // Seed random number generator
    srand((unsigned)time(0));
 
    // create SkipList object with MAXLVL and P
    SkipList lst(3, 0.5);
 
    lst.insertElement(3);
    lst.insertElement(6);
    lst.insertElement(7);
    lst.insertElement(9);
    lst.insertElement(12);
    lst.insertElement(19);
    lst.insertElement(17);
    lst.insertElement(26);
    lst.insertElement(21);
    lst.insertElement(25);
    lst.displayList();
 
    //Search for node 19
    lst.searchElement(19);
 
    //Delete node 19
    lst.deleteElement(19);
    lst.displayList();
}

                    

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
// Java code for inserting element in skip list
 
class GFG {
 
    // Class to implement node
    static class Node {
        int key;
 
        // Array to hold pointers to node of different level
        Node forward[];
 
        Node(int key, int level)
        {
            this.key = key;
 
            // Allocate memory to forward
            forward = new Node[level + 1];
        }
    };
 
    // Class for Skip list
    static class SkipList {
        // Maximum level for this skip list
        int MAXLVL;
 
        // P is the fraction of the nodes with level
        // i pointers also having level i+1 pointers
        float P;
 
        // current level of skip list
        int level;
 
        // pointer to header node
        Node header;
 
        SkipList(int MAXLVL, float P)
        {
            this.MAXLVL = MAXLVL;
            this.P = P;
            level = 0;
 
            // create header node and initialize key to -1
            header = new Node(-1, MAXLVL);
        }
 
        int randomLevel()
        {
            float r = (float)Math.random();
            int lvl = 0;
            while (r < P && lvl < MAXLVL) {
                lvl++;
                r = (float)Math.random();
            }
            return lvl;
        }
 
        Node createNode(int key, int level)
        {
            Node n = new Node(key, level);
            return n;
        }
 
        // Insert given key in skip list
 
        void insertElement(int key)
        {
            Node current = header;
 
            // create update array and initialize it
            Node update[] = new Node[MAXLVL + 1];
 
            /* start from highest level of skip list
                    move the current pointer forward while
            key is greater than key of node next to
            current Otherwise inserted current in update
            and move one level down and continue search
            */
            for (int i = level; i >= 0; i--) {
                while (current.forward[i] != null && current.forward[i].key < key)
                    current = current.forward[i];
                update[i] = current;
            }
 
            /* reached level 0 and forward pointer to
            right, which is desired position to
            insert key.
            */
            current = current.forward[0];
 
            /* if current is NULL that means we have reached
            to end of the level or current's key is not
            equal to key to insert that means we have to
            insert node between update[0] and current node
        */
            if (current == null || current.key != key) {
                // Generate a random level for node
                int rlevel = randomLevel();
 
                // If random level is greater than list's
                // current level (node with highest level
                // inserted in list so far), initialize
                // update value with pointer to header for
                // further use
                if (rlevel > level) {
                    for (int i = level + 1; i < rlevel + 1;
                         i++)
                        update[i] = header;
 
                    // Update the list current level
                    level = rlevel;
                }
 
                // create new node with random level
                // generated
                Node n = createNode(key, rlevel);
 
                // insert node by rearranging pointers
                for (int i = 0; i <= rlevel; i++) {
                    n.forward[i] = update[i].forward[i];
                    update[i].forward[i] = n;
                }
                System.out.println(
                        "Successfully Inserted key " + key);
            }
        }
 
        // Display skip list level wise
        void displayList()
        {
            System.out.println("\n*****Skip List*****");
            for (int i = 0; i <= level; i++) {
                Node node = header.forward[i];
                System.out.print("Level " + i + ": ");
                while (node != null) {
                    System.out.print(node.key + " ");
                    node = node.forward[i];
                }
                System.out.println();
            }
        }
        void deleteElement(int key){
            Node current = header;
            // create update array and initialize it
            Node[] update = new Node[MAXLVL+1];
 
            /* start from highest level of skip list
                    move the current pointer forward while
            key is greater than key of node next to
            current Otherwise inserted current in update
            and move one level down and continue search
            */
            for (int i = level; i >= 0; i--) {
                while (current.forward[i] != null && current.forward[i].key < key)
                    current = current.forward[i];
                update[i] = current;
            }
 
            /* reached level 0 and forward pointer to
            right, which is desired position to
            delete key.
            */
            current=current.forward[0];
 
            if(current!=null && current.key==key){
 
                // delete node by rearranging pointers
                for (int i = 0; i < level; i++) {
                    if (update[i].forward[i] != current)
                        break;
                    update[i].forward[i] = current.forward[i];
                }
 
                //if it was the only node in that level and you deleted it, delete the level
                while(level>0 && header.forward[level] ==null){
                    level --;
                }
                System.out.println("Successfully deleted key "+ key);
            }
 
        }
 
        void Search(int key){
 
             Node current = header;
 
 
 
            /* start from highest level of skip list
                    move the current pointer forward while
            key is greater than key of node next to
            current    and move one level down and continue search
            */
            for (int i = level; i >=0 ; i--) {
                while (current.forward[i] != null && current.forward[i].key < key) {
                    current = current.forward[i];
                }
            }
                current = current.forward[0];
            // current has to be the key if it is present
                if(current !=null && current.key==key) {
                    System.out.println("Key found");
                     
                }
                else{
                    System.out.println("Key not found");
                }
 
 
             
        }
 
    }
 
    // Driver to test above code
    public static void main(String[] args)
    {
        // create SkipList object with MAXLVL and P
        SkipList lst = new SkipList(3, 0.5f);
 
        lst.insertElement(3);
        lst.insertElement(6);
        lst.insertElement(7);
        lst.insertElement(9);
        lst.insertElement(12);
        lst.insertElement(19);
        lst.insertElement(17);
        lst.insertElement(26);
        lst.insertElement(21);
        lst.insertElement(25);
        lst.displayList();
        lst.deleteElement(9);
        lst.deleteElement(12);
        lst.displayList();
        lst.Search(9);
 
    }
}

                    

Python

# Python3 code for searching and deleting element in skip list
 
import random
 
class Node(object):
    '''
    Class to implement node
    '''
    def __init__(self, key, level):
        self.key = key
 
        # list to hold references to node of different level
        self.forward = [None]*(level+1)
 
class SkipList(object):
    '''
    Class for Skip list
    '''
    def __init__(self, max_lvl, P):
        # Maximum level for this skip list
        self.MAXLVL = max_lvl
 
        # P is the fraction of the nodes with level
        # i references also having level i+1 references
        self.P = P
 
        # create header node and initialize key to -1
        self.header = self.createNode(self.MAXLVL, -1)
 
        # current level of skip list
        self.level = 0
     
    # create  new node
    def createNode(self, lvl, key):
        n = Node(key, lvl)
        return n
     
    # create random level for node
    def randomLevel(self):
        lvl = 0
        while random.random()<self.P and \
              lvl<self.MAXLVL:lvl += 1
        return lvl
 
    # insert given key in skip list
    def insertElement(self, key):
        # create update array and initialize it
        update = [None]*(self.MAXLVL+1)
        current = self.header
 
        '''
        start from highest level of skip list
        move the current reference forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
        '''
        for i in range(self.level, -1, -1):
            while current.forward[i] and \
                  current.forward[i].key < key:
                current = current.forward[i]
            update[i] = current
 
        '''
        reached level 0 and forward reference to
        right, which is desired position to
        insert key.
        '''
        current = current.forward[0]
 
        '''
        if current is NULL that means we have reached
           to end of the level or current's key is not equal
           to key to insert that means we have to insert
           node between update[0] and current node
       '''
        if current == None or current.key != key:
            # Generate a random level for node
            rlevel = self.randomLevel()
 
            '''
            If random level is greater than list's current
            level (node with highest level inserted in
            list so far), initialize update value with reference
            to header for further use
            '''
            if rlevel > self.level:
                for i in range(self.level+1, rlevel+1):
                    update[i] = self.header
                self.level = rlevel
 
            # create new node with random level generated
            n = self.createNode(rlevel, key)
 
            # insert node by rearranging references
            for i in range(rlevel+1):
                n.forward[i] = update[i].forward[i]
                update[i].forward[i] = n
 
            print("Successfully inserted key {}".format(key))
 
    def deleteElement(self, search_key):
 
        # create update array and initialize it
        update = [None]*(self.MAXLVL+1)
        current = self.header
 
        '''
        start from highest level of skip list
        move the current reference forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
        '''
        for i in range(self.level, -1, -1):
            while(current.forward[i] and \
                  current.forward[i].key < search_key):
                current = current.forward[i]
            update[i] = current
 
        '''
        reached level 0 and advance reference to
        right, which is possibly our desired node
        '''
        current = current.forward[0]
 
        # If current node is target node
        if current != None and current.key == search_key:
 
            '''
            start from lowest level and rearrange references
            just like we do in singly linked list
            to remove target node
            '''
            for i in range(self.level+1):
 
                '''
                If at level i, next node is not target
                node, break the loop, no need to move
                further level
                '''
                if update[i].forward[i] != current:
                    break
                update[i].forward[i] = current.forward[i]
 
            # Remove levels having no elements
            while(self.level>0 and\
                  self.header.forward[self.level] == None):
                self.level -= 1
            print("Successfully deleted {}".format(search_key))
 
    def searchElement(self, key):
        current = self.header
 
        '''
        start from highest level of skip list
        move the current reference forward while key
        is greater than key of node next to current
        Otherwise inserted current in update and
        move one level down and continue search
        '''
        for i in range(self.level, -1, -1):
            while(current.forward[i] and\
                  current.forward[i].key < key):
                current = current.forward[i]
 
        # reached level 0 and advance reference to
        # right, which is possibly our desired node
        current = current.forward[0]
 
        # If current node have key equal to
        # search key, we have found our target node
        if current and current.key == key:
            print("Found key ", key)
 
 
    # Display skip list level wise
    def displayList(self):
        print("\n*****Skip List******")
        head = self.header
        for lvl in range(self.level+1):
            print("Level {}: ".format(lvl), end=" ")
            node = head.forward[lvl]
            while(node != None):
                print(node.key, end=" ")
                node = node.forward[lvl]
            print("")
 
# Driver to test above code
def main():
    lst = SkipList(3, 0.5)
    lst.insertElement(3)
    lst.insertElement(6)
    lst.insertElement(7)
    lst.insertElement(9)
    lst.insertElement(12)
    lst.insertElement(19)
    lst.insertElement(17)
    lst.insertElement(26)
    lst.insertElement(21)
    lst.insertElement(25)
    lst.displayList()
 
    # Search for node 19
    lst.searchElement(19)
 
    # Delete node 19
    lst.deleteElement(19)
    lst.displayList()
 
main()

                    

C#

using System;
using System.Collections.Generic;
 
class Node
{
    // Class to implement node
    public int Key;
    public Node[] Forward;
 
    public Node(int key, int level)
    {
        Key = key;
        Forward = new Node[level + 1];
    }
}
 
class SkipList
{
    // Class for Skip list
    private const int MaxLevel = 3; // Maximum level for this skip list
    private readonly double P; // The fraction of nodes with level
                               // i references also having level i+1 references
    private Node Header; // Header node
    private int Level; // Current level of skip list
 
    public SkipList(double p)
    {
        P = p;
        Header = new Node(-1, MaxLevel);
        Level = 0;
    }
 
    // Create new node
    private Node CreateNode(int level, int key)
    {
        return new Node(key, level);
    }
 
    // Generate random level for a node
    private int RandomLevel()
    {
        int level = 0;
        while (new Random().NextDouble() < P && level < MaxLevel)
        {
            level++;
        }
        return level;
    }
 
    // Insert given key into skip list
    public void InsertElement(int key)
    {
        // Create an update array and initialize it
        var update = new Node[MaxLevel + 1];
        var current = Header;
 
        // Start from the highest level of skip list
        // Move the current reference forward while the key
        // is greater than the key of the node next to current
        // Otherwise, insert current into update and move one level down and continue search
        for (var i = Level; i >= 0; i--)
        {
            while (current.Forward[i] != null && current.Forward[i].Key < key)
            {
                current = current.Forward[i];
            }
            update[i] = current;
        }
 
        // Reached level 0 and forward reference to the right, which is the desired position to insert the key.
        current = current.Forward[0];
 
        // If current is null or its key is not equal to the key to insert, insert the node between update[0] and current node.
        if (current == null || current.Key != key)
        {
            // Generate a random level for the node
            var randomLevel = RandomLevel();
 
            // If the random level is greater than the list's current level, initialize update values with references to the header for further use
            if (randomLevel > Level)
            {
                for (var i = Level + 1; i <= randomLevel; i++)
                {
                    update[i] = Header;
                }
                Level = randomLevel;
            }
 
            // Create a new node with the random level generated
            var newNode = CreateNode(randomLevel, key);
 
            // Insert the node by rearranging references
            for (var i = 0; i <= randomLevel; i++)
            {
                newNode.Forward[i] = update[i].Forward[i];
                update[i].Forward[i] = newNode;
            }
 
            Console.WriteLine($"Successfully inserted key {key}");
        }
    }
 
    // Delete an element with the given search key
    public void DeleteElement(int searchKey)
    {
        // Create an update array and initialize it
        var update = new Node[MaxLevel + 1];
        var current = Header;
 
        // Start from the highest level of skip list
        // Move the current reference forward while the key
        // is greater than the key of the node next to current
        // Otherwise, insert current into update and move one level down and continue search
        for (var i = Level; i >= 0; i--)
        {
            while (current.Forward[i] != null && current.Forward[i].Key < searchKey)
            {
                current = current.Forward[i];
            }
            update[i] = current;
        }
 
        // Reached level 0 and advanced reference to the right, which is possibly our desired node
        current = current.Forward[0];
 
        // If the current node is the target node
        if (current != null && current.Key == searchKey)
        {
            // Start from the lowest level and rearrange references
            // just like we do in a singly linked list to remove the target node
            for (var i = 0; i <= Level; i++)
            {
                // If at level i, the next node is not the target node, break the loop
                if (update[i].Forward[i] != current)
                {
                    break;
                }
                update[i].Forward[i] = current.Forward[i];
            }
 
            // Remove levels having no elements
            while (Level > 0 && Header.Forward[Level] == null)
            {
                Level--;
            }
 
            Console.WriteLine($"Successfully deleted {searchKey}");
        }
    }
 
    // Search for an element with the given key
    public void SearchElement(int key)
    {
        var current = Header;
 
        // Start from the highest level of skip list
        // Move the current reference forward while the key
        // is greater than the key of the node next to current
        // Otherwise, insert current into update and move one level down and continue search
        for (var i = Level; i >= 0; i--)
        {
            while (current.Forward[i] != null && current.Forward[i].Key < key)
            {
                current = current.Forward[i];
            }
        }
 
        // Reached level 0 and advanced reference to the right, which is possibly our desired node
        current = current.Forward[0];
 
        // If the current node has a key equal to the search key, we have found our target node
        if (current != null && current.Key == key)
        {
            Console.WriteLine($"Found key {key}");
        }
    }
 
    // Display the skip list level-wise
    public void DisplayList()
    {
        Console.WriteLine("\n***** Skip List ******");
        var head = Header;
        for (var lvl = 0; lvl <= Level; lvl++)
        {
            Console.Write($"Level {lvl}: ");
            var node = head.Forward[lvl];
            while (node != null)
            {
                Console.Write(node.Key + " ");
                node = node.Forward[lvl];
            }
            Console.WriteLine();
        }
    }
}
 
class Program
{
    public static void Main(string[] args)
    {
        var lst = new SkipList(0.5);
        lst.InsertElement(3);
        lst.InsertElement(6);
        lst.InsertElement(7);
        lst.InsertElement(9);
        lst.InsertElement(12);
        lst.InsertElement(19);
        lst.InsertElement(17);
        lst.InsertElement(26);
        lst.InsertElement(21);
        lst.InsertElement(25);
        lst.DisplayList();
 
        // Search for node 19
        lst.SearchElement(19);
 
        // Delete node 19
        lst.DeleteElement(19);
        lst.DisplayList();
    }
}

                    

Output
Successfully Inserted key 3
Successfully Inserted key 6
Successfully Inserted key 7
Successfully Inserted key 9
Successfully Inserted key 12
Successfully Inserted key 19
Successfully Inserted key 17
Successfully Inserted key 26
Successfully Inserted key 21
Successfully Inserted key 25

*****Skip List*****
Level 0: 3 6 7 9 12 17 19 21 25 26 
Level 1: 6 9 19 26 
Level 2: 19 
Found key: 19
Successfully deleted key 19

*****Skip List*****
Level 0: 3 6 7 9 12 17 21 25 26 
Level 1: 6 9 26 




Time complexity of both searching and deletion is same – Time complexity Worst case:

  • Access – O(n)
  • Search – O(n)
  • Insert – O(n)
  • Space – O(nlogn)
  • Delete – O(n)

 



Last Updated : 21 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads