Heap

cats.collections.Heap
See theHeap companion object
sealed abstract class Heap[A]

Heap is a Purely Functional Binary Heap. Binary Heaps are not common in the functional space, especially because their implementation depends on mutable arrays in order to gain in performance. This functional binary heap is based on Vladimir Kostyukov's paper and it does support the basic operations on a heap without compromising performance.

It is important to note that we can, in fact, to create the Binary Heap in order O(n) from a List using the function heapify.

Attributes

Companion
object
Source
Heap.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def height: Int

Returns the height of the heap.

Returns the height of the heap.

Attributes

Source
Heap.scala
def isEmpty: Boolean

Verifies if the heap is empty.

Verifies if the heap is empty.

Attributes

Source
Heap.scala
def minimumOption: Option[A]

Returns min value on the heap, if it exists

Returns min value on the heap, if it exists

Attributes

Source
Heap.scala
def size: Long

Returns the size of the heap.

Returns the size of the heap.

Attributes

Source
Heap.scala

Concrete methods

def +(x: A)(implicit order: Order[A]): Heap[A]

Alias for add

Alias for add

Attributes

Source
Heap.scala
def ++(as: Iterable[A])(implicit order: Order[A]): Heap[A]

Alias for addAll

Alias for addAll

Attributes

Source
Heap.scala
def --(implicit order: Order[A]): Heap[A]

Alias for remove

Alias for remove

Attributes

Source
Heap.scala
def add(x: A)(implicit order: Order[A]): Heap[A]

Insert a new element into the heap. Order O(log n)

Insert a new element into the heap. Order O(log n)

Attributes

Source
Heap.scala
def addAll(as: Iterable[A])(implicit order: Order[A]): Heap[A]

Attributes

Source
Heap.scala
def contains(a: A)(implicit order: Order[A]): Boolean

This is O(N) in the worst case, but we use the heap property to be lazy

This is O(N) in the worst case, but we use the heap property to be lazy

Attributes

Source
Heap.scala
def exists(fn: A => Boolean): Boolean

Check to see if a predicate is ever true

Check to see if a predicate is ever true

Attributes

Source
Heap.scala
def foldLeft[B](init: B)(fn: (B, A) => B)(implicit order: Order[A]): B

do a foldLeft in the same order as toList. requires an Order[A], which prevents us from making a Foldable[Heap] instance.

do a foldLeft in the same order as toList. requires an Order[A], which prevents us from making a Foldable[Heap] instance.

prefer unorderedFoldMap if you can express your operation as a commutative monoid since it is O(N) vs O(N log N) for this method

Attributes

Source
Heap.scala
def forall(fn: A => Boolean): Boolean

Check to see if a predicate is always true

Check to see if a predicate is always true

Attributes

Source
Heap.scala
final def getMin: Option[A]

Returns min value on the heap, if it exists

Returns min value on the heap, if it exists

Attributes

Source
Heap.scala
def nonEmpty: Boolean

Return true if this is not empty

Return true if this is not empty

Attributes

Source
Heap.scala
def pop(implicit order: Order[A]): Option[(A, Heap[A])]

Remove the min element from the heap (the root) and return it along with the updated heap. Order O(log n)

Remove the min element from the heap (the root) and return it along with the updated heap. Order O(log n)

Attributes

Source
Heap.scala
def remove(implicit order: Order[A]): Heap[A]

Remove the min element from the heap (the root). Order O(log n)

Remove the min element from the heap (the root). Order O(log n)

Attributes

Source
Heap.scala
def toList(implicit order: Order[A]): List[A]

Returns a sorted list of the elements within the heap.

Returns a sorted list of the elements within the heap.

Attributes

Source
Heap.scala

convert to a PairingHeap which can do fast merges, this is an O(N) operation

convert to a PairingHeap which can do fast merges, this is an O(N) operation

Attributes

Source
Heap.scala
final def unorderedFold(implicit m: CommutativeMonoid[A]): A

Similar to unorderedFoldMap without a transformation

Similar to unorderedFoldMap without a transformation

Attributes

Source
Heap.scala
final def unorderedFoldMap[B](fn: A => B)(implicit m: CommutativeMonoid[B]): B

Aggregate with a commutative monoid, since the Heap is not totally ordered

Aggregate with a commutative monoid, since the Heap is not totally ordered

Attributes

Source
Heap.scala

Deprecated methods

def heapify(a: List[A])(implicit order: Order[A]): Heap[A]

Avoid this, it should really have been on the companion because this totally ignores this.

Avoid this, it should really have been on the companion because this totally ignores this.

Attributes

Deprecated
true
Source
Heap.scala