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 SKP LST KODUNU ALITIRMAK STEDMDE private SkipListNode head;
private Random rand; KISIMLARINDA "FEDL HEAD CAN BE FNAL" HATASI ALIYORUM VE NT LEVEL=0 KISMINDA SE "LOCAL VARABLE HDES A FELD" HATASI ALIYORUM.LTFEN BU KODU TEKRAR DORU EKLDE DZELTP ATABLR MSNZ.SKP LST'E EKLEME VE ARAMA YNTEMLERNN DORU EKLDE ALIMASINI STYORUN

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

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

Students also viewed these Databases questions

Question

=+How is CSR different from strategic CSR?

Answered: 1 week ago