patternjavaMinor
Custom SkipList implementation in Java
Viewed 0 times
customimplementationskiplistjava
Problem
I recently studied
package skiplists.skiplist;
public class SkipList> {
private SkipNode start;
private SkipNode end;
private SkipNode supportStart;
private SkipNode supportEnd;
private int size;
private int sizeSupport;
public T getStart() {
return start.getData();
}
public T getEnd() {
return end.getData();
}
public int getSize() {
return size;
}
public void add(T data){
if(start == null){
insertAsFirstElement(data);
}
else{
insert(data);
}
}
private void insertAsFirstElement(T data){
SkipNode node = new SkipNode<>(data);
start = node;
end = node;
size++;
SkipNode supportNode = new SkipNode<>(data);
supportStart = supportNode;
supportEnd = supportNode;
supportNode.setDown(node);
sizeSupport++;
}
//Adding element in the end assuming user enters data in ascending order
private void insert(T data){
SkipNode node = new SkipNode<>(data);
end.setNext(node);
node.setPrevious(end);
end = node;
size++;
int expectedSupportSize = (int) Math.sqrt(size);
if(sizeSupport supportNode = new SkipNode<>(data);
supportEnd.setNext(supportNode);
supportNode.setPrevious(supportEnd);
supportEnd = supportNode;
supportNode.setDown(node);
sizeSupport++;
if(sizeSupport > 2)
reAjustSupportList();
}
}
/*readjusting the support list so that they point to the correct nodes when new
*support nodes are added
*/
private void reAjustSupportList(){
SkipNode navigationNode = supportStart.getNext();
int i = 1;
while(navigationNode != supportEnd){
SkipNode tempNode = navigationNode.getDown();
for(int j = 1 ; j navigationNode = supportStart;
if(data.compareTo(navigationNode.getData()) 0 || data.co
SkipList and thought I would create one on my own. This implementation uses a 2 layer SkipList where the size of the support list is roughly square root of the size of the original list.
``package skiplists.skiplist;
public class SkipList> {
private SkipNode start;
private SkipNode end;
private SkipNode supportStart;
private SkipNode supportEnd;
private int size;
private int sizeSupport;
public T getStart() {
return start.getData();
}
public T getEnd() {
return end.getData();
}
public int getSize() {
return size;
}
public void add(T data){
if(start == null){
insertAsFirstElement(data);
}
else{
insert(data);
}
}
private void insertAsFirstElement(T data){
SkipNode node = new SkipNode<>(data);
start = node;
end = node;
size++;
SkipNode supportNode = new SkipNode<>(data);
supportStart = supportNode;
supportEnd = supportNode;
supportNode.setDown(node);
sizeSupport++;
}
//Adding element in the end assuming user enters data in ascending order
private void insert(T data){
SkipNode node = new SkipNode<>(data);
end.setNext(node);
node.setPrevious(end);
end = node;
size++;
int expectedSupportSize = (int) Math.sqrt(size);
if(sizeSupport supportNode = new SkipNode<>(data);
supportEnd.setNext(supportNode);
supportNode.setPrevious(supportEnd);
supportEnd = supportNode;
supportNode.setDown(node);
sizeSupport++;
if(sizeSupport > 2)
reAjustSupportList();
}
}
/*readjusting the support list so that they point to the correct nodes when new
*support nodes are added
*/
private void reAjustSupportList(){
SkipNode navigationNode = supportStart.getNext();
int i = 1;
while(navigationNode != supportEnd){
SkipNode tempNode = navigationNode.getDown();
for(int j = 1 ; j navigationNode = supportStart;
if(data.compareTo(navigationNode.getData()) 0 || data.co
Solution
For data structures it would be really nice to have some testing code as
well, just to play with it and obviously for you to make sure it works.
Also, the
would be easy to implement at least
Anyway I'll just start with some points I noticed; in general I think
this looks good though.
doesn't use the same amount of space between methods and the two
comments can be formatted nicer.
comparison.
each condition in a single line, or move it into a method if you don't
like that.
For the algorithmic part, well I guess since you only add at the end
this is okay, but from what I gather
from Wikipedia there is a bit
more to skiplists in general; in particular the readjustment shouldn't
be necessary (please correct me if I'm wrong on that one).
well, just to play with it and obviously for you to make sure it works.
Also, the
SkipList doesn't implement any interface, but I'm sure itwould be easy to implement at least
List, Iterable, oh yeah andtoString would be nice as well.Anyway I'll just start with some points I noticed; in general I think
this looks good though.
- Formatting is a bit off, but that be copy&paste issues;
SkipNode
doesn't use the same amount of space between methods and the two
comments can be formatted nicer.
- Using
<>is great.
- Avoid
Math.sqrt, you can simply squaresizeSupportinstead for the
comparison.
reAjustSupportListis missing a "d".
- The single extremely long line in
searchis not nice. Either put
each condition in a single line, or move it into a method if you don't
like that.
For the algorithmic part, well I guess since you only add at the end
this is okay, but from what I gather
from Wikipedia there is a bit
more to skiplists in general; in particular the readjustment shouldn't
be necessary (please correct me if I'm wrong on that one).
Context
StackExchange Code Review Q#71432, answer score: 8
Revisions (0)
No revisions yet.