1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* SI 486D Spring 2013
   This is a skeleton for a skip list class that only supports
   insertion of height-1 nodes.
 
   Nodes are stored in "towers" which contain a list of forward-facing
   links ("flinks"), as well as a key and a value.
 
   To demo on a small random example, run 'javac SkipList.java && java SkipList"
*/
 
import java.util.*;
 
public class SkipList<V> {
  class Tower {
    double key;
    V value;
    ArrayList<Tower> flinks;
 
    Tower(int h, double key, V value) {
      this.key = key;
      this.value = value;
      this.flinks = new ArrayList<Tower>(Collections.nCopies(h+1, (Tower)null));
    }
  }
 
  Tower start;
  Tower end;
  int thesize = 0;
 
  public SkipList() {
    this.start = new Tower(0, Double.NEGATIVE_INFINITY, null);
    this.end = new Tower(0, Double.POSITIVE_INFINITY, null);
    this.start.flinks.set(0, this.end);
  }
 
  public int height() {
    return this.start.flinks.size()- 1;
  }
 
  public int size() {
    return this.thesize;
  }
 
  public void insert(double key, V value) {
    int h = 1; // TODO: choose height randomly!
    Tower newTower = new Tower(h, key, value);
 
    // Grow height of start/end as needed
    while (h >= this.height()) {
      this.start.flinks.add(this.end);
      this.end.flinks.add(null);
    }
 
    Tower currentTower = this.start;
    int level = currentTower.flinks.size()-1;
    while (level >= 0) {
      if (key > currentTower.flinks.get(level).key) {
        // Move right if we can on this level
        currentTower = currentTower.flinks.get(level);
      } 
      else {
        // Otherwise go down a level
        if (level <= h) {
          // Insert the new tower on this level
          newTower.flinks.set(level, currentTower.flinks.get(level));
          currentTower.flinks.set(level, newTower);
        }
        level -= 1;
      }
    }
 
    this.thesize += 1;
  }
 
  public String toString() {
    /* Returns a nice pretty representation of the skip list */
    String toret = "";
    for (int level = this.height(); level >= 0; --level) {
      Tower currentTower = this.start;
      while (currentTower != this.end) {
        String rep = "(" + Double.toString(currentTower.key) + ")";
        if (level >= currentTower.flinks.size()) {
          for (int i=0; i < rep.length(); ++i) toret += "-";
        }
        else { toret = toret + rep; }
        toret = toret + "--";
        currentTower = currentTower.flinks.get(0);
      }
      toret = toret + "(" + Double.toString(currentTower.key) + ")\n";
    }
    return toret;
  }
 
  public static void main(String[] args) {
    Random r = new Random();
    // Randomly insert 10 numbers and print the resulting skip list
    SkipList<String> S = new SkipList<String>();
    for (int i=0; i<7; ++i) {
      S.insert(r.nextInt(100), "this is the value");
    }
    System.out.println(S);
  }
}