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
  • Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components,...

    Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components, namely key node and array of successors. You can also add a constructor, but you do not need to add any method. - In the class SkipList implement a constructor SkipList(long maxNodes). The parameter maxNodes determines the maximal number of nodes that can be added to a skip list. Using this parameter we can determine the maximal height of a node, that is, the...

  • 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++ 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...

  • 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....

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • 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...

  • Bar Stool Economics Suppose that every day, ten men go out for beer and the bill...

    Bar Stool Economics Suppose that every day, ten men go out for beer and the bill for all ten comes to $100. If they paid their bill the way we pay our taxes, it would go something like this: The first four men (the poorest) would pay nothing. The fifth would pay $1. The sixth would pay $3. The seventh would pay $7. The eighth would pay $12. The ninth would pay $18. The tenth man (the richest) would pay...

  • i need a help pleassssse? 5. What is the probability that the sum of the numbers...

    i need a help pleassssse? 5. What is the probability that the sum of the numbers on two dice is even when they are rolled? 6. What is the probability that a card selected at random from a standard deck of 52 cards is an ace or a heart? What is the probability that a positive integer not exceed- 100 selected at random is divisible by 5 or 7? nd the probability of winning a lottery by selecting the t...

  • NEED HELP ASAP Please answer all please. a People with higher incomes might not own TVs or computers with higher incomes likely will have high-speed Internet access, which will lead to spending...

    NEED HELP ASAP Please answer all please. a People with higher incomes might not own TVs or computers with higher incomes likely will have high-speed Internet access, which will lead to spending more time on-i O People with lower incomes might not own TVs or computers. D People who spend time at home on the Internet never watch TV r the students in your statistics class as the population and suppose they are seated in four rows of 10 comes...

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
ADVERTISEMENT