Skip to content

Commit 8b6bea6

Browse files
author
phishman3579
committed
Found the issue with Skip List add performance!
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@423 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 995a5ce commit 8b6bea6

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/com/jwetherell/algorithms/data_structures/SkipList.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SkipList<T extends Comparable<T>> implements IList<T> {
1818
private static final Random seedGenerator = new Random();
1919

2020
// Defaults
21-
private static final int LISTS = 31;
21+
private static final int MAX = 31;
2222

2323
private int randomSeed = -1;
2424
private Node<T> head = null;
@@ -52,18 +52,22 @@ private void insertValueProbablistically(T value) {
5252
head.value = value;
5353
node.value = oldHeadValue;
5454
}
55-
for (int i=level; i>=0; i--) {
55+
for (int i=MAX; i>=0; i--) {
56+
//System.out.println(this.getString(prev.value, i));
5657
Node<T> next = prev.getNext(i);
5758
while (next!=null) {
5859
if (next.value.compareTo(value)==1) break;
5960
prev = next;
61+
//System.out.println(this.getString(prev.value, i));
6062
next = prev.getNext(i);
6163
}
62-
node.setNext(i, next);
63-
if (next!=null) next.setPrev(i, node);
64+
if (i <= level) {
65+
node.setNext(i, next);
66+
if (next!=null) next.setPrev(i, node);
6467

65-
prev.setNext(i, node);
66-
node.setPrev(i, prev);
68+
prev.setNext(i, node);
69+
node.setPrev(i, prev);
70+
}
6771
}
6872
}
6973

@@ -74,12 +78,12 @@ private void insertValueProbablistically(T value) {
7478
public boolean add(T value) {
7579
if (head==null) {
7680
// new list
77-
Node<T> node = new Node<T>(LISTS,value);
81+
Node<T> node = new Node<T>(MAX,value);
7882
head = node;
7983
} else {
8084
insertValueProbablistically(value);
8185
}
82-
// TODO: System.out.println(this.toString());
86+
//System.out.println(this.toString());
8387
size++;
8488
return true;
8589
}
@@ -239,8 +243,9 @@ public String getString(T value, int level) {
239243
node = next;
240244
}
241245
if (i>0) builder.append("\n");
242-
}
246+
}
243247
}
248+
builder.append("\n");
244249
return builder.toString();
245250
}
246251

0 commit comments

Comments
 (0)