Question

SKIP LIST IMPLEMENTATION (C++) Described a Skip List implementation where every SkipNode had four pointers: up,...

SKIP LIST IMPLEMENTATION (C++)

Described a Skip List implementation where every SkipNode had four pointers: up, down, prev, and next. Describe how we can insert into a Skip List where the SkipNode has only down and next pointers instead. You may assume, for this problem, that you will never flip a coin with enough heads in a row to increase the height of the Skip List.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implementing the skip list data structure

  • The link list element structure used to implement a Skip List
    • The link list element used to implement the skip list has 4 links (not including the data portion):
      skip-list20.gif
  • The Entry strcuture in a Skip List (the SkipListEntry class)
    • Skip List entry:
      
        public class SkipListEntry
        {
           public String key;
           public Integer value;
      
           public SkipListEntry up;       // up link
           public SkipListEntry down;     // down link
           public SkipListEntry left;     // left link
           public SkipListEntry right;    // right link        
             
           ...
           (methods)
        }
      

      Note:

      • As you can see, my entry type is again very specific (no generic types):
        • String key
        • Integer value             
      • When I write the demo program, I will do it using specific types (classes), not parameterized classes

        I have showed you how to convert a specific class into a parameterized class, so you can write one if you want to

      • Reason for using specific classes:
        • My choice is didactic in nature; I don't want to spend time analyzing the overly complex syntax of parameterized classes
        • I want to spend my time teaching algorithms, not Java syntax
  • Making (and using) the special −∞ and +∞ elements
    • Representing the −∞ element and the +∞ element:
      • The −∞ and the +∞ is just an ordinary Skip List Entry containing a special value for the key field.
    • We can accommodate the −∞ element and the +∞ element by defining 2 special key value:
        public class SkipListEntry
        {
           public String key;                                              
           public Integer value;                                                   
      
           public SkipListEntry up, down, left, right;                             
                                                                              
           public static String negInf = "-oo";  // -inf key value
           public static String posInf = "+oo";  // +inf key value
         
         
           ....
        }
      
    • How to instantiate a Skip List entry containing +∞:
           SkipListEntry x = new SkipListEntry( SkipListEntry.posInf, null );      
      

      How to check if an Skip List entry x contains +∞:

             key == SkipListEntry.posInf             
      

    OK, now we move on to the Skip list itself....

  • Structure (class) to represent a Skip List
    • Remember that a Skip List is a very complicated list

      But.... It is never the less a list

      • To represent a list, we only use a pointer (that points to the first element)
      • Often, we use more pointers for improve efficiency (such as a tail pointer)
    • Variables in the SkipList class:
      
         public class SkipList
         {
           public SkipListEntry head;    // First element of the top level
           public SkipListEntry tail;    // Last element of the top level
          
          
           public int n;                 // number of entries in the Skip List   
          
           public int h;       // Height
           public Random r;    // Coin toss
          
           ....
          
         }
      

      Note:

      • The Random object r is used to determine the height of a newly added entry           

        (We use r to simulate a coin toss experiment)

    • Example illustrating how the variables are used:
      skip-list22.gif

      Note:

      • Since the logical top level does not contain any entries:
        • The implementation will omit the logical top layer
      • The variables head and tail provide quick access to the end elements of the real top layer

        Usage of head and tail:

        • They allow us to easily add an new layer above the top layer
  • Constructing a Skip List object
    • The constructor will construct an empty Skip List which looks like this:
      skip-list24.gif
    • Constructor code:
      
        public SkipList()     // Constructor...
        {
           SkipListEntry p1, p2;
      
           /* -----------------------------------
              Create an -oo and an +oo object
              ----------------------------------- */
           p1 = new SkipListEntry(SkipListEntry.negInf, null);
           p2 = new SkipListEntry(SkipListEntry.posInf, null);
      
      
           /* --------------------------------------
              Link the -oo and +oo object together
              --------------------------------------- */
           p1.right = p2;
           p2.left = p1;
      
           /* --------------------------------------
              Initialize "head" and "tail"
              --------------------------------------- */
           head = p1;
           tail = p2;
      
           /* --------------------------------------
              Other initializations
              --------------------------------------- */
           n = 0;                   // No entries in Skip List
           h = 0;                   // Height is 0
      
           r = new Random();        // Make random object to simulate coin toss
        }
      
    • The SkipList class so far:
         public class SkipList
         {
           public SkipListEntry head;    // First element of the top level
           public SkipListEntry tail;    // Last element of the top level
          
           public int n;                 // number of entries in the Skip List    
          
           public int h;       // Height
           public Random r;    // Coin toss
          
           public SkipList()    // Constructor...
           {
              SkipListEntry p1, p2;
          
              p1 = new SkipListEntry(SkipListEntry.negInf, null);
              p2 = new SkipListEntry(SkipListEntry.posInf, null);
          
              head = p1;
              tail = p2;
          
              p1.right = p2;
              p2.left = p1;
          
              n = 0;
          
              h = 0;
              r = new Random();
           }
          
           ...
         }
      
  • Implementing the basic Map operations
    • Basic Map operations:
      • get()
      • put()
      • remove()            

      Notice that each basic operation must first find (search) the appropriate entry (using a key) before the operation can be completed.

      So we must learn how to search a Skip List for a given key first....

  • Search operation in a skip list
    • Consider the links traversed to locate the key 50:
      skip-list16.gif
    • Psuedo code:
      
         p = head;
      
         repeat
         {
      
            Move to the right until your right neighbor node         
            contains a key that is greater than k     
      
            if ( not lowest level )
               Drop down one level
            else
               exit
         }
      
      
    • Search algorithm for Skip List:
      
        /* ------------------------------------------------------
           findEntry(k): find the largest key x <= k
                         on the LOWEST level of the Skip List
           ------------------------------------------------------ */
      
        public SkipListEntry findEntry(String k)
        {
           SkipListEntry p;
      
           /* -----------------
              Start at "head"
              ----------------- */
           p = head;
      
           while ( true )
           {
              /* ------------------------------------------------
                 Search RIGHT until you find a LARGER entry
      
                 E.g.: k = 34
      
                           10 ---> 20 ---> 30 ---> 40
                                            ^
                                            |
                                            p must stop here
                      p.right.key = 40
                 ------------------------------------------------ */           
              while ( (p.right.key) != SkipListEntry.posInf &&
                      (p.right.key).compareTo(k) <= 0 )
              {
                 p = p.right;         // Move to right
              }
      
              /* ---------------------------------
                 Go down one level if you can...
                 --------------------------------- */
              if ( p.down != null )
              {  
                 p = p.down;          // Go downwards
              }
              else
              {
                 break;       // We reached the LOWEST level... Exit...
              }
           }
      
           return(p);         // Note: p.key <= k
        }
      
    • Note:
      • If the key k is found in the Skip List, findEntry(k) will return the reference to the entry containg the key k
      • If the key k is not found in the Skip List, findEntry(k) will return the reference to the floorEntry(k) entry containg a key that is smaller than k

        Example: findEntry(42) will return the reference to 39:

        skip-list31.gif
  • Implementing the "get(Key k)" method
    • get(k):
      
        /** Returns the value associated with a key. */               
      
        public Integer get (String k)
        {
           SkipListEntry p;
      
           p = findEntry(k);
      
           if ( k.equals( p.key ) )
              return(p.value);
           else
              return(null);
        }
      
  • Put(k,v): inserting into a Skip List
    • Pseudo code for put(k,v):
          put(k, v)
          {
             SkipListEntry p;
      
             p = findEntry(k);
      
             if ( k.equals( p.key ) )    // Found !
             {
                p.value = v;             // Update the value
Add a comment
Know the answer?
Add Answer to:
SKIP LIST IMPLEMENTATION (C++) Described a Skip List implementation where every SkipNode had four pointers: up,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • At the back of ly deaths. About 58 million people die every year each day, on average? Wake-up ca...

    at the back of ly deaths. About 58 million people die every year each day, on average? Wake-up call. Suppose you has a one-in-a-thousand the event you thought of in the morning the chances you will not experience your What are the chances you will pass an entire year without of these coincidences? About how many die 2. wake up each morning and chance of happening that day. think of an event that What are the will not that day7...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • c++ code In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head,...

    c++ code In mythology, the Hydra was a monster with many heads. Every time the hero chopped off a head, two smaller heads would grow in its place. Fortunately for the hero, If the head was small enough, he could chop it off without two more growing in its place. To kill the Hydra, all the hero needed to do was to chop off all the heads. Write a program that simulates the Hydra. Instead of heads, we will use...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Fill in code on right in c++ and follow all constraints and directions. 1 hode expand...

    Fill in code on right in c++ and follow all constraints and directions. 1 hode expand (node* head) // fill in code here This problem has you modify a linked lists composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL next pointer 4 The linked lists for this lab store string data. Some of the strings...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

  • PLEASE ANSWER ALL QUESTIONS: Your help is appreciated, THANK YOU :) 5. List four personal characteristics...

    PLEASE ANSWER ALL QUESTIONS: Your help is appreciated, THANK YOU :) 5. List four personal characteristics of a Professional Rescuer: 6. What are the two classifications of responders that are more advanced then an EMR? more advanced Pre-hospital Care Personnel? 7. Upon arrival at the scene of an emergency, the first responder’s first priority is— 8. What agency is concerned with regulations dealing with exposure to blood born pathogens? 9. What are the two major diseases of concern caused by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT