Methods in the Heap Interface

 

 

Heap Interface

boolean

add (Object item)

Adds an item to the heap.  Throws an exception if the item is non-comparable.  Returns true if the item is actually added.  An implementation can choose not to allow duplicates, where items are considered duplicates if they are equal with respect to the compareTo method.  An implementation determines the ordering of duplicates when they are allowed.

void

clear ()

Removes all items from this heap.

Object

clone ()

Returns a shallow copy of this heap.

Collection

collectionView ()

Returns a collection view of the items contained in this heap.

boolean

equals (Object other)

Returns true if the specified item is a heap that equals this heap.  Two heaps are considered equal if their items are equal when taken in descending order.

int

hashCode ()

Returns the hash code value for this heap.

boolean

isEmpty ()

Returns true if this heap contains no elements.

Iterator

iterator ()

Returns an iterator over the items contained in this heap.  The iterator provides the items sorted in descending order.  The iterator should not support removal.

Object

peek ()

Returns the item at the head of this heap without removing it from the heap.  Throws an exception if the heap is empty.

Object

pop ()

Returns the item at the head of this heap and removes it from the heap.  Throws an exception if the heap is empty.

int

size ()

Returns the number of items in this heap.

static Object [ ]

sort (Object a[])

Returns an array of items sorted in ascending order.  The items are taken from the specified input array.  Throws an exception if the specified array is null or has length zero or if the array contains non-comparable items.  (Actually, static methods cannot be placed in an interface.  Therefore, this merely acts as a reminder.  The user must cast the heap to an abstract heap before using this method.)

static Object [ ]

sort (Collection col)

Returns an array of items sorted in ascending order.  The items are taken from the specified input collection.  Throws an exception if the specified collection is null or has size zero or if the collection contains non-comparable items.  (Actually, static methods cannot be placed in an interface.  Therefore, this merely acts as a reminder.  The user must cast the heap to an abstract heap before using this method.)

Object[ ]

toArray ()

Returns an array containing all the items in this heap.  The array is sorted in descending order.

String

toString ()

Returns a string that lists the items in this heap sorted in descending order.

 

martin@cc.wwu.edu
Disclaimer

Copyright Martin Osborne 1998-2001
  All rights reserved