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; // Maksimum seviyeyi belirlemek iin bir sabit
private int level;
private SkipListNode head;
private Random rand;
public SkipList(){
this.level =0;
this.head = new SkipListNode(Integer.MIN_VALUE, MAX_LEVEL);
this.rand = new Random();
}
// Atlama Listesine bir deer eklemek iin kullanlan metot
public void insert(int value){
int newLevel = randomLevel();
SkipListNode newNode = new SkipListNode(value, newLevel);
SkipListNode[] update = new SkipListNode[MAX_LEVEL +1];
SkipListNode current = head;
// Her seviyede ilerleyerek eklenecek dmn uygun konumunu bulma
for (int i = level; i >=0; i--){
while (current.next[i]!= null && current.next[i].value < value){
current = current.next[i];
}
update[i]= current;
}
// Yeni dm uygun seviyelere eklemek ve referanslar gncellemek
for (int i =0; i <= newLevel; i++){
newNode.next[i]= update[i].next[i];
update[i].next[i]= newNode;
}
// Yeni seviye eklenmise, Atlama Listesi seviyesini gncelleme
if (newLevel > level){
level = newLevel;
}
}
// Verilen bir deeri aramak iin kullanlan metot
public boolean search(int value){
SkipListNode current = head;
// Her seviyede ilerleyerek aranan deeri bulma veya bulamama
for (int i = level; i >=0; i--){
while (current.next[i]!= null && current.next[i].value < value){
current = current.next[i];
}
}
// Aranan deeri bulup bulamama kontrol
current = current.next[0];
return current != null && current.value == value;
}
// Rastgele seviyeler oluturmak iin kullanlan metot
private int randomLevel(){
int level =0;
while (Math.random()<0.5 && level < MAX_LEVEL){
level++;
}
return level;
}
public static void main(String[] args){
SkipList skipList = new SkipList();
int[] values ={7,11,15,22,25,30,42,53};
// Deerleri Atlama Listesine eklemek
for (int value : values){
skipList.insert(value);
}
// Arama sonularn gstermek
System.out.println("Arama sonular:");
for (int value : values){
System.out.println(value +" bulundu mu?"+ skipList.search(value));
}
}
} BU KODDA HATA ALIYORUM VE HERHANG BR IKTI ALAMAIYORUM . LTFEN DZELTP TEKRAR ATABLR MSNZ

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_2

Step: 3

blur-text-image_3

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions