Question: import java.util.Random; class SkipListNode { int value; SkipListNode [ ] next; public SkipListNode ( int value, int level ) { this.value = value; this.next =

import java.util.Random;
class SkipListNode {
int value;
SkipListNode[] next;
public SkipListNode(int value, int level){
this.value = value;
this.next = new SkipListNode[level +1];
}
}
public class SkipList {
private static final int MAX_LEVEL =4;
private int level;
private final SkipListNode head;
private final Random rand;
public SkipList(){
this.level =0;
this.head = new SkipListNode(Integer.MIN_VALUE, MAX_LEVEL);
this.rand = new Random();
}
public void insert(int value){
int newLevel = randomLevel();
SkipListNode newNode = new SkipListNode(value, newLevel);
SkipListNode[] update = new SkipListNode[MAX_LEVEL +1];
SkipListNode current = head;
for (int i = level; i >=0; i--){
while (current.next[i]!= null && current.next[i].value < value){
current = current.next[i];
}
update[i]= current;
}
for (int i =0; i <= newLevel; i++){
newNode.next[i]= update[i].next[i];
update[i].next[i]= newNode;
}
if (newLevel > level){
level = newLevel;
}
}
public boolean search(int value){
SkipListNode current = head;
for (int i = level; i >=0; i--){
while (current.next[i]!= null && current.next[i].value < value){
current = current.next[i];
}
}
current = current.next[0];
return current != null && current.value == value;
}
private int randomLevel(){
int randomLevel =0;
while (Math.random()<0.5 && randomLevel < MAX_LEVEL){
randomLevel++;
}
return randomLevel;
}
public static void main(String[] args){
SkipList skipList = new SkipList();
int[] values ={7,11,15,22,25,30,42,53};
for (int value : values){
skipList.insert(value);
}
System.out.println("Arama sonular:");
for (int value : values){
System.out.println(value +" bulundu mu?"+ skipList.search(value));
}
}
} BU KODDA private final Random rand; KISMINDA VARABLE RAND S NEVER EEAD HATASI ALIYORUM VE BU YUZDEN KODUM ALIMIYOR. NE YAPMAM GEREK

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!