DESY ACOP Beans Home

com.cosylab.util
Class Heap

java.lang.Object
  extended by com.cosylab.util.Heap

public class Heap
extends java.lang.Object

Simple heap implementation. This implementation uses Comparable interface to maintain ascending order. Heap will grow and shrink based on number of elements. If total number of elements equals internal array size, this array is expanded by factor of 2. If number of elements is less than one quarter the array size, array will be shrunk to by half.
Implementation is synchronized and can be considered thread-safe.

Version:
$id$
Author:
Ales Pucelj

Constructor Summary
Heap()
          Default constructor for Heap().
Heap(int initialSize)
          Alternate constructor for heap that allows specification of initial size.
 
Method Summary
 void add(java.lang.Comparable c)
          Adds new element to the heap.
 java.lang.Comparable get(int index)
          Returns element at index.
 java.lang.Comparable getFirst()
          Returns smallest element or null if empty.
 boolean isEmpty()
          True if heap is empty.
static void main(java.lang.String[] args)
          Run test applet.
 java.lang.Comparable remove()
          Removes smallest element.
 int size()
          Size of the heap.
 java.lang.String toString()
          DOCUMENT ME!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Heap

public Heap()
Default constructor for Heap(). Creates new heap with 16 elements.


Heap

public Heap(int initialSize)
Alternate constructor for heap that allows specification of initial size.

Parameters:
initialSize -
Method Detail

add

public void add(java.lang.Comparable c)
Adds new element to the heap.

Parameters:
c - Element to add.

remove

public java.lang.Comparable remove()
Removes smallest element.

Returns:
Element or null if empty.

get

public java.lang.Comparable get(int index)
Returns element at index.

Parameters:
index - Index of the element.
Returns:
synchronized

size

public int size()
Size of the heap.

Returns:
synchronized

isEmpty

public boolean isEmpty()
True if heap is empty.

Returns:
synchronized

getFirst

public java.lang.Comparable getFirst()
Returns smallest element or null if empty.

Returns:
synchronized

toString

public java.lang.String toString()
DOCUMENT ME!

Overrides:
toString in class java.lang.Object
Returns:
DOCUMENT ME!

main

public static void main(java.lang.String[] args)
Run test applet.

Parameters:
args - command line parameters


Copyright © 2010. All Rights Reserved.