实现功能优先队列:从左倾堆到非可比元素的处理
1. 左倾堆的实现
左倾堆是实现优先队列的一种有效方式,它是一种堆有序的二叉树。我们将使用一个名为Heap的抽象类来实现左倾堆。与之前开发的树结构不同,right、left和head方法将返回Result类型,而不是原始值。同时,元素数量称为length,并且记忆化的length和rank将由构造函数的调用者计算,而不是构造函数本身。
以下是Heap类的基本结构:
public abstract class Heap<A extends Comparable<A>> { @SuppressWarnings("rawtypes") protected static final Heap EMPTY = new Empty(); protected abstract Result<Heap<A>> left(); protected abstract Result<Heap<A>> right(); protected abstract int rank(); public abstract Result<A> head(); public a