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.
Implementing the skip list data structure
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:
|
|
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 .... } |
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....
But.... It is never the less a list
|
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:
|
Note:
|
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 } |
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(); } ... } |
|
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....
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 } |
/* ------------------------------------------------------ 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 } |
|
/** 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) { SkipListEntry p; p = findEntry(k); if ( k.equals( p.key ) ) // Found ! { p.value = v; // Update the value |
SKIP LIST IMPLEMENTATION (C++) Described a Skip List implementation where every SkipNode had four pointers: up,...
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 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, 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 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 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 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 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 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 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 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...