Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions

Question

Which form of proof do you find least persuasive? Why?

Answered: 1 week ago