Peter
-
I've made a few changes to the code, the diffs of which are below... In short, I've change the private modifier on the two comparators to public (from private) so that they can be used in the constructor, I've changed the 'isMinHeap' constructor to use the comparable constructor to eliminate duplication of code, I've added a size() accessor, and I've added comments to the stuff
I've added.
Attached is the
latest & greatest BinaryHeap.java + a TestBinaryHeap.java
that contains
some of the code I used to test my previous n-ary heap.
Note
that I wrote
the n-ary heap quite a while ago using my own unit test
harness,
and I've done
little work with JUnit... perhaps you have the time to put it
in
an format
acceptable to JUnit. You'll notice also that I commented out the
code
that tests inserting a null element - because the current BinaryHeap does
not
prevent inserting null elements - something that I think you might want to
change...
Below you'll
find the diff of the BinaryHeap.java file from that in CVS.
Thanks, Chad D:\dev\cvs\cvs.exe diff -r HEAD
BinaryHeap.java
Index: BinaryHeap.java =================================================================== RCS file: /home/cvspublic/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/BinaryHeap.java,v retrieving revision 1.4 diff -r1.4 BinaryHeap.java 45,46c45,56 < private static final Comparator MIN_COMPARATOR = new MinComparator(); < private static final Comparator MAX_COMPARATOR = new MaxComparator(); --- > /** > * Comparator used to instantiate a min heap - assumes contents implement > * the Comparable interface. > */ > public static final Comparator MIN_COMPARATOR = new MinComparator(); > > /** > * Comparator used to instantiate a max heap - assumes contents implement > * the Comparable interface. > */ > public static final Comparator MAX_COMPARATOR = new MaxComparator(); > 62a73,74 > * > * @param capacity the size of the heap 71a84,85 > * > * @param comparator to order the contents of the heap 80a95,97 > * > * @param capacity the size of the heap > * @param comparator to order the contents of the heap 110,114c127 < //+1 as 0 is noop < m_elements = new Object[ capacity + 1 ]; < < if( isMinHeap ) m_comparator = MIN_COMPARATOR; < else m_comparator = MAX_COMPARATOR; --- > this( capacity, isMinHeap ? MIN_COMPARATOR : MAX_COMPARATOR ); 143a157,166 > } > > /** > * Returns the number of elements currently on the heap. > * > * @return the size of the heap. > */ > public int size() > { > return m_size; CVS command finished
execution
|
BinaryHeap.java
Description: Binary data
TestBinaryHeap.java
Description: Binary data
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>