// Changes the value of the item stored in the pairing heap. // pos is any Position returned by insert. // newVal is the new value, which must be smaller // than the currently stored value. // Throws ArgumentException if pos is null. // Throws IllegalValueException if new value is larger than old. public void DecreaseKey(IPriorityQueuePosition <AnyType> pos, AnyType newVal) { if (pos == null) { throw new ArgumentException("null Position passed to decreaseKey"); } PairNode p = (PairNode)pos; if (p.element.CompareTo(newVal) < 0) { throw new IllegalValueException("newVal/oldval: " + newVal + " /" + p.element); } p.element = newVal; if (p != root) { if (p.nextSibling != null) { p.nextSibling.prev = p.prev; } if (p.prev.leftChild == p) { p.prev.leftChild = p.nextSibling; } else { p.prev.nextSibling = p.nextSibling; } p.nextSibling = null; root = CompareAndLink(root, p); } }
// Throws InvalidOperationException because no IPriorityQueuePosition are returned // by the Insert method for BinaryHeap. public void DecreaseKey(IPriorityQueuePosition <AnyType> p, AnyType newVal) { throw new InvalidOperationException("Cannot use decreaseKey for binary heap"); }