Squeak Class Documentation category index | class index  
 
Heap
  category: Collections-Sequenceable
  superclass: SequenceableCollection
  subclasses:

Class Heap implements a special data structure commonly referred to as 'heap'. Heaps are more efficient than SortedCollections if:
a) Elements are only removed at the beginning
b) Elements are added with arbitrary sort order.
The sort time for a heap is O(n log n) in all cases.

Instance variables:
array <Array> the data repository
tally <Integer> the number of elements in the heap
sortBlock <Block|nil> a two-argument block defining the sort order,
or nil in which case the default sort order is
[:element1 :element2| element1 <= element2]

instance methods
  accessing
  at:
at:put:
first
reSort
size
sortBlock
sortBlock:

  adding
  add:

  comparing
  =

  enumerating
  do:

  growing
  grow
growSize
growTo:
trim

  private
  array
privateRemoveAt:
setCollection:
setCollection:tally:
species

  private-heap
  downHeap:
downHeapSingle:
upHeap:

  removing
  remove:ifAbsent:
removeAt:
removeFirst

  testing
  isEmpty
sorts:before:

class methods
  examples
  heapExample
heapSortExample

  instance creation
  new
new:
sortBlock:
withAll:
withAll:sortBlock:

instance methods
  accessing top  
 

at:

Return the element at the given position within the receiver


 

at:put:

Heaps are accessed with #add: not #at:put:


 

first

Return the first element in the receiver


 

reSort

Resort the entire heap


 

size

Answer how many elements the receiver contains.


 

sortBlock


 

sortBlock:


  adding top  
 

add:

Include newObject as one of the receiver's elements. Answer newObject.


  comparing top  
 

=

Answer true if my and aHeap's species are the same,
and if our blocks are the same, and if our elements are the same.


  enumerating top  
 

do:

Evaluate aBlock with each of the receiver's elements as the argument.


  growing top  
 

grow

Become larger.


 

growSize

Return the size by which the receiver should grow if there are no empty slots left.


 

growTo:

Grow to the requested size.


 

trim

Remove any empty slots in the receiver.


  private top  
 

array


 

privateRemoveAt:

Remove the element at the given index and make sure the sorting order is okay


 

setCollection:


 

setCollection:tally:


 

species

Answer the preferred class for reconstructing the receiver. For example,
collections create new collections whenever enumeration messages such as
collect: or select: are invoked. The new kind of collection is determined by
the species of the original collection. Species and class are not always the
same. For example, the species of Interval is Array.


  private-heap top  
 

downHeap:

Check the heap downwards for correctness starting at anIndex.
Everything above (i.e. left of) anIndex is ok.


 

downHeapSingle:

This version is optimized for the case when only one element in the receiver can be at a wrong position. It avoids one comparison at each node when travelling down the heap and checks the heap upwards after the element is at a bottom position. Since the probability for being at the bottom of the heap is much larger than for being somewhere in the middle this version should be faster.


 

upHeap:

Check the heap upwards for correctness starting at anIndex.
Everything below anIndex is ok.


  removing top  
 

remove:ifAbsent:

Remove oldObject as one of the receiver's elements. If several of the
elements are equal to oldObject, only one is removed. If no element is
equal to oldObject, answer the result of evaluating anExceptionBlock.
Otherwise, answer the argument, oldObject.


 

removeAt:

Remove the element at given position


 

removeFirst

Remove the first element from the receiver


  testing top  
 

isEmpty

Answer whether the receiver contains any elements.


 

sorts:before:

Return true if element1 should be sorted before element2.
This method defines the sort order in the receiver


class methods
  examples top  
 

heapExample

Heap heapExample


 

heapSortExample

Heap heapSortExample


  instance creation top  
 

new

Answer a new instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable.


 

new:

Answer an instance of this class with the number of indexable
variables specified by the argument, sizeRequested.


 

sortBlock:

Create a new heap sorted by the given block


 

withAll:

Create a new heap with all the elements from aCollection


 

withAll:sortBlock:

Create a new heap with all the elements from aCollection