shz.core.queue.p.MaxPQueue Maven / Gradle / Ivy
package shz.core.queue.p;
import java.util.Comparator;
public final class MaxPQueue extends RBBSTPQueue {
private static final long serialVersionUID = 7993867654979754324L;
public MaxPQueue(Comparator super E> comparator) {
super(comparator);
}
public static MaxPQueue of(Comparator super E> comparator) {
return new MaxPQueue<>(comparator);
}
@Override
public E peek() {
if (root == null) return null;
Node h = root;
while (h.right != null) h = h.right;
return h.e;
}
@Override
protected Node delTop(Node h) {
if (isRed(h.left)) h = rotateRight(h);
if (h.right == null) return h.left;
if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h);
h.right = delTop(h.right);
return balance(h);
}
}