Class | Heap |
In: |
lib/facet/heap.rb
|
Parent: | Object |
A Simple Heap structure and Sort.
To extend the heap, implement a cmp(i,j) method which compares array elements i and j and returns true iff i is larger than j, where larger or is the required heap sorting. See Heap::Min and Heap::Max for examples.
The parent,left and right methods do not check the supplied parameter, but their result is only valid if the supplied with an integer >=0 for left and right and >0 for parent.
<quote> Cormen1990: Chapter 7 (Heapsort) of 'An introduction to algorithms', by Cormen, T.H; Leiserson, C.E.; Rivest, R.L; MIT Press, Cambridge, 1990 ISBN 0-262-53091-0 </quote>
Special thanks to Nenad Ocelic for many suggestions.
VERSION | = | '1.0.0' |
Initialise the heap. If supplied an array, build the heap with the values of the array. The array can be unsorted. Note: this method can only be called by superclasses
Extract the first element from the heap. Will raise EmptyHeapException if there are no (more) elements on the heap.
Heap-sort a clone of the internal array. This will not touch the internal array. Returns the array sorted reversely on the heap condition!