Question

Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components,...

Skip list, needs to be implemented in Java:

- Implement the class NodeSkipList with two components, namely key node and array of successors. You can also add a constructor, but you do not need to add any method. - In the class SkipList implement a constructor SkipList(long maxNodes).

The parameter maxNodes determines the maximal number of nodes that can be added to a skip list. Using this parameter we can determine the maximal height of a node, that is, the maximal length of the array of successors.

As the maximal height of a node it is usually taken the logarithm of the parameter. Further, it is useful to construct both sentinels of maximal height.

- In the class SkipList it is useful to write a method which simulates coin flip and returns the number of all tosses until the first head comes up. This number represents the height of a node to be inserted.

2 - In the class SkipList implement the following methods:

(i) insert, which accepts an integer and inserts it into a skip list. The method returns true, if the element is successfully inserted and false, if the element already exists in the data structure.

(ii) search, which accepts an integer and finds it in a skip list. The method returns true, if the element is successfully found and false otherwise.

(iii) delete, which accepts an integer and deletes it from a skip list. The method returns true, if the element is successfully deleted and false otherwise.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Node {
    int element;

    public Node(int value) {
        element = value;
    }

    public Node() {
    }
}

------------------------------------------------

public class NodeSkipList {
    Node key;
    Node[] successors;

    public NodeSkipList(int maxNodes) {
        key = new Node();
        successors = new Node[maxNodes];
    }
}

------------------------------------------------

public class SkipList {
    private NodeSkipList nodeSkipList;
    private int maxNodes;

    SkipList(long maxNodes) {
        this.maxNodes = (int) Math.log(maxNodes);
        nodeSkipList = new NodeSkipList(this.maxNodes);
    }

    private long simulateCoinFlip() {
        long count = 0;
        while (true) {
            if (Math.random() < 0.5)
                count++;
            else
                return count;
        }
    }

    boolean insert(int value) {
        if (search(value))
            return false;
        long height = simulateCoinFlip();
        if (maxNodes <= height)
            return false;
        nodeSkipList.successors[(int) height] = new Node(value);
        return true;
    }

    boolean search(int value) {
        for (Node node : nodeSkipList.successors) {
            if (null != node && node.element == value)
                return false;
        }
        return true;
    }

    boolean delete(int value) {
        int position = 0;
        for (Node node : nodeSkipList.successors) {
            if (null != node && node.element == value) {
                nodeSkipList.successors[position] = null;
                return true;
            }
            position++;
        }
        return false;
    }
}
Add a comment
Know the answer?
Add Answer to:
Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a c++ 'List' class to handle a list with general operations. That means you can...

    Implement a c++ 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. The list has no order, except for the order you insert or delete.   The methods you are to implement are as given: Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' ) empty returns true or false if list is empty or not. front makes current position...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName...

    Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName */ public class IntegerSet {    /** * Creates a new instance of IntegerSet    */ // TODO: implement the constructor    /** * Return a new IntegerSet containing the union of the two IntegerSet objects * passed as arguments */ // TODO: implement the union method    /** * Return a new IntegerSet containing the intersection of the two IntegerSet objects * passed...

  • Implement in Java: the Bank constructor and the accountExists method in the below class. The requirements...

    Implement in Java: the Bank constructor and the accountExists method in the below class. The requirements stated in the comments" in green" class Bank { HashSet<Integer> accounts HS; /**Initialize the hash set and copy the values from the given array accounts to the hashset accounts HS*/ Bank(Integer[] accounts) { /*Your code should go here*/ } /** Check if account exists in the hashset accounts HS * return true if exists and false otherwise*/ Boolean accountExists(Integer account) { /*Your code should...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

  • In this project, you are asked to design and implement a class for double link list...

    In this project, you are asked to design and implement a class for double link list to hold integers: – The class should be called DList, you also need a class called Node that contains an int as the data – Node needs the following data: int data; //the data held by the node Node* parent; //the parent of the Node Node* child; //the child of the Node – Node needs the following public functions: Node(int)// constructor to create a...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT