Package

org.taffy.core.container

Object Hierarchy
What is it?

A Heap is a container that acts as either a min-heap or max-heap.

Quick Example
h = [Heap newMax]

h insert: 2
h insert: 1
h insert: 3

h pop
==> 3

h pop
==> 2

Table of Contents:

Constructing a Heap

We use the Heap newMin method to construct a new min-heap, and the Heap newMax method to construct a new max-heap:

minHeap = [Heap newMin]
maxHeap = [Heap newMax]

Inserting and Popping

The insert: method inserts an object, and the pop method pops the head off the heap. Calling pop on an empty heap returns nil.

h = [Heap newMin]
h insert: 1
h insert: 3
h insert: 2

h pop
==> 1

h pop
==> 2

h pop
==> 3

h pop
==> nil

Querying the Size of a Heap

The size method returns the size of a heap:

h = [Heap newMin]

h size
==> 0

h insert: 2
h size
==> 1

Element Properties

A min-heap’s elements must support the operator #operator(<):, and a max-heap must support the operator #operator(>):.