Back to Library QID: #115046

Solution for QID #115046: Add support for the method public V getPos(int k) that retur | StudyHelpMe

Subject: Computer Science
Status: Made to Order
Add support for the method public V getPos(int k) that returns the value of the kth smallest key (starting at 1) in the collection of keys in this (if k is less than 1 or exceeds the number of items in the collection, throw a NoSuchElementException reporting the size of the collection and the value of k). Your implementation must run in O(log N) time. To achieve this level of efficiency, you will need to include more information in each node. For key values k with multiple nodes (due to its promotion to higher levels), you do not need to update the additional additional information in every node with key value k.   import java.util.Iterator; import java.util.Random; public class SkipList<K extends Comparable<K>, V> implements Iterable<K> { protected static final Random randomGenerator = new Random(); protected static final double DEFAULT_PROBABILITY = 0.5; private Node<K, V> head; private double probability; private int size; public SkipList() { this(DEFAULT_PROBABILITY); } public SkipList(double probability) { this.head = new Node<K, V>(null, null, 0); this.probability = probability; this.size = 0; } public V get(K key) { checkKeyValidity(key); Node<K, V> node = findNode(key); if (node.getKey() == null) { return null; } if (node.getKey().compareTo(key) == 0) return node.getValue(); else return null; } public void add(K key, V value) { checkKeyValidity(key); Node<K, V> node = findNode(key); if (node.getKey() != null && node.getKey().compareTo(key) == 0) node.setValue(value); Node<K, V> newNode = new Node<K, V>(key, value, node.getLevel()); horizontalInsert(node, newNode); int currentLevel = node.getLevel(); int headLevel = head.getLevel(); while (isBuildLevel()) { if (currentLevel >= headLevel) { Node<K, V> newHead = new Node<K, V>(null, null, headLevel + 1); verticalLink(newHead, head); head = newHead; headLevel = head.getLevel(); } while (node.getUp() == null) { node = node.getPrevious(); } node = node.getUp(); Node<K, V> tmp = new Node<K, V>(key, value, node.getLevel()); horizontalInsert(node, tmp); verticalLink(tmp, newNode); newNode = tmp; currentLevel++; } size++; } public boolean remove(K key) { checkKeyValidity(key); Node<K, V> node = findNode(key); if (node == null || node.getKey().compareTo(key) != 0) return false; while (node.getDown() != null) Node<K, V> prev = null; Node<K, V> next = null; for (; node != null; node = node.getUp()) { prev = node.getPrevious(); next = node.getNext(); if (prev != null) prev.setNext(next); if (next != null) next.setPrevious(prev); } while (head.getNext() == null && head.getDown() != null) { head = head.getDown(); head.setUp(null); } size--; return true; } public boolean contains(K key) { return get(key) != null; } public int size() { return size; } public boolean empty() { return size == 0; } protected Node<K, V> findNode(K key) { Node<K, V> node = head; Node<K, V> next = null; Node<K, V> down = null; K nodeKey = null; while (true) { next = node.getNext(); while (next != null && lessThanOrEqual(next.getKey(), key)) { node = next; next = node.getNext(); } nodeKey = node.getKey(); if (nodeKey != null && nodeKey.compareTo(key) == 0) break; down = node.getDown(); if (down != null) node = down; else break; } return node; } protected void checkKeyValidity(K key) { if (key == null) throw new IllegalArgumentException("Key must be not null!"); } protected boolean lessThanOrEqual(K a, K b) { return a.compareTo(b) <= 0; } protected boolean isBuildLevel() { return randomGenerator.nextDouble() < probability; } protected void horizontalInsert(Node<K, V> x, Node<K, V> y) { y.setPrevious(x); y.setNext(x.getNext()); if (x.getNext() != null) x.getNext().setPrevious(y); x.setNext(y); } protected void verticalLink(Node<K, V> x, Node<K, V> y) { x.setDown(y); y.setUp(x); } @Override public String toString() { StringBuilder sb = new StringBuilder(); Node<K, V> node = head; while (node.getDown() != null) node = node.getDown(); while (node.getPrevious() != null) node = node.getPrevious(); if (node.getNext() != null) node = node.getNext(); while (node != null) { sb.append(node.toString()).append("n"); node = node.getNext(); } return sb.toString(); } @Override public Iterator<K> iterator() { return new SkipListIterator<K, V>(head); } protected static class SkipListIterator<K extends Comparable<K>, V> implements Iterator<K> { private Node<K, V> node; public SkipListIterator(Node<K, V> node) { while (node.getDown() != null) node = node.getDown(); while (node.getPrevious() != null) node = node.getPrevious(); if (node.getNext() != null) node = node.getNext(); this.node = node; } @Override public boolean hasNext() { return this.node != null; } @Override public K next() { K result = node.getKey(); node = node.getNext(); return result; } @Override public void remove() { throw new UnsupportedOperationException(); } } protected static class Node<K extends Comparable<K>, V> { private K key; private V value; private int level; private Node<K, V> up, down, next, previous; public Node(K key, V value, int level) { setKey(key); setValue(value); setLevel(level); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Node[") .append("key:"); if (this.key == null) sb.append("None"); else sb.append(this.key.toString()); sb.append(" value:"); if (this.value == null) sb.append("None"); else sb.append(this.value.toString()); sb.append("]"); return sb.toString(); } public K getKey() { return key; } public void setKey(K key) { this.key = key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public Node<K, V> getUp() { return up; } public void setUp(Node<K, V> up) { this.up = up; } public Node<K, V> getDown() { return down; } public void setDown(Node<K, V> down) { this.down = down; } public Node<K, V> getNext() { return next; } public void setNext(Node<K, V> next) { this.next = next; } public Node<K, V> getPrevious() { return previous; } public void setPrevious(Node<K, V> previous) { this.previous = previous; } } }
ZERO AI
Human Written
PHD EXPERTS
Verified
TURNITIN
Clean Report
FAST DELIVERY
Instant/Hourly