PRQuadTree / src / SkipNode.java
SkipNode.java
Raw
/**
 * Inspiration from openDSA-textbook: chapter 18.01
 * https://canvas.vt.edu/courses/141775/assignments/1272244?module_item_id=1550563
 * 
 * @author nickgrifasi
 * @version 9/20/21
 * @param <K> Generic key, used in comparison
 * @param <E> generic element
 */
public class SkipNode<K extends Comparable<K>, E> {

    /**
     * temporary array to hold values while doing
     * SkipList operations
     */
    private SkipNode<K, E>[] forward;
    private KVPair<K, E> rec;
    private int level;

    /**
     * getter method that returns key of the KVPair
     * 
     * @return generic key value. null if condition fails.
     */
    public K key() {
        return rec.getKey();
    }

    /**
     * getter method that returns element of the forward node
     * 
     * @return E generic element value. null if condition fails.
     */
    public E element() {
        if (rec == null) {
            return null;
        }
        return rec.getNodeData();
    }

    /**
     * Constructor that creates SkipNode stored with KVPair
     *@param kvpair KVPair to be constructed
     *@param levels number of levels within current SkipList
     *iteration
     */
    @SuppressWarnings("unchecked")
    public SkipNode(KVPair<K, E> kvpair, int levels) {
        level = levels;
        rec = kvpair;
        forward = (SkipNode<K, E>[]) new SkipNode[levels + 1];
        for (int i = 0; i < level; i++) {
            forward[i] = null;
        }
    }

    /**
     * Getter method that returns level of SkipNode
     * @return int level of SkipNode
     */
    public int getLevel() {
        return level;
    }

    /**
     * Getter method that returns the KVPair of SkipNode
     * @return returns the KVPair<K, E> of the SkipNode.
     * null if it condition fails. 
     */
    public KVPair<K, E> getPair() {
        return rec;
    }
    
    /**
     * Getter method that returns the forward[]
     * array.
     * @return forward array used for SkipList traversal
     */
    public SkipNode<K, E>[] getForward()
    {
        return forward;
    }
}