类 Heap<E>

  • 所有已实现的接口:
    Iterable<E>

    public class Heap<E>
    extends Object
    implements Iterable<E>
    堆实现 1. 内部以数组方式实现堆,通过comparator决定大顶堆或小顶堆 2. 支持数据迭代,迭代器保证数据排序
    作者:
    frankcl
    • 构造器详细资料

      • Heap

        public Heap()
      • Heap

        public Heap​(Comparator<? super E> comparator)
      • Heap

        public Heap​(int initCapacity,
                    Comparator<? super E> comparator)
    • 方法详细资料

      • add

        public void add​(E e)
        添加元素
        参数:
        e - 元素,元素为空抛出异常IllegalArgumentException
      • peek

        public E peek()
        获取堆顶元素 完成操作后不影响堆元素
        返回:
        堆顶元素,如果堆为空返回null
      • poll

        public E poll()
        弹出堆顶元素 完成操作后移除堆顶元素
        返回:
        堆顶元素,如果堆为空返回null
      • remove

        public boolean remove​(E e)
        移除元素
        参数:
        e - 元素,元素为空抛出异常IllegalArgumentException
        返回:
        移除成功返回true,否则返回false
      • size

        public int size()
        元素数量
        返回:
        当前堆元素数量
      • isEmpty

        public boolean isEmpty()
        堆是否为空
        返回:
        为空返回true,否则返回false