Minimum heap implementation. : Heaps « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » HeapsScreenshots 
Minimum heap implementation.
 
/**
 * Copyright (c) 2008-2010  Morten Silcowitz.
 *
 * This file is part of the Jinngine physics library
 *
 * Jinngine is published under the GPL license, available 
 * at http://www.gnu.org/copyleft/gpl.html. 
 */
//package jinngine.util;
import java.util.*;

/**
 * Minimum heap implementation. See [Cormen et al 1999] for formal theory. 
 * Maintains all elements in a min-heap, such that the minimum element will
 * be the top-most node in the heap at all times. Among many other uses, heaps are ideal for 
 * representing priority queues. 
 */
public class Heap<T> {
  private int size;
  final private List<Node> heap;
  final private Comparator<T> comparator;

  private class Node {
    public T element;
    public int position;
  }    

  /**
   * Create a new heap
   @param comparator A comparator that handles elements of type T
   */
  public HeapComparator<T> comparator ) {
    size = 0;
    //Allocate space
    heap = new ArrayList<Node>();

    //Comparator
    this.comparator = comparator;

    //initialy clear
    //for (int i=0;i<maxSize;i++) heap[i] = null;
  }

  /**
   * Insert element into the heap. O(lg n) where n is the number of elements/nodes in the heap  
   @param element new element to be inserted
   */
  public void insertfinal T element ) {
    size++;
    Node node = new Node();
    node.element = element;
    node.position = size-1;
    heap.add(node);
    decreaseKeynode );
    //return node;
  }

  public final void clear() {
    heap.clear();
    size = 0;
  }

  /**
   * Return a reference to the top-most element on the heap. The method does not change the state
   * of the heap in any way. O(k).
   @return Reference to top-most element of heap
   */
  public final T top() {
    return heap.get(0).element;
  }

  //bound check missing

  /**
   * Pop an element of the heap. O(lg n) where n is the number of elements in heap.
   */
  public T pop() {
    T returnNode = top();
    exchange0, size-);
    heap.remove(size-1);
    size--;

    //if any elements left in heap, do minHeapify
    if (size>0) {
      minHeapifyheap.get(0) );
    }
    
    return returnNode;
  }

  //  private final void reinsert( final Node n ) {
  //    if ( !decreaseKey(n) ) {
  //      minHeapify(n);
  //    }
  //  }

  public final int size() {
    return size;
  }


  private final boolean decreaseKeyfinal Node node ) {
    int index = node.position;
    boolean modified = false;

    //    while ( index>0 &&  (heap[parent(index)]).compareTo( heap[index]) >= 0 ) {
    while index>&&  comparator.compare(heap.get(parent(index)).element, heap.get(index).element >= ) {
      exchangeindex, parent(index) );
      index = parent(index);
      modified = true;
    }

    return modified;
  }

  private final void minHeapifyfinal Node node ) {
    int smallest;
    int index = node.position;
    int left = left(index);
    int right = right(index);

    //  if (left<size && (heap[left]).compareTo(heap[index]) <= 0 )
    if (left<size && comparator.compare(heap.get(left).element, heap.get(index).element<= )
      smallest= left;
    else
      smallest = index;

    //    if (right<size && (heap[right]).compareTo(heap[smallest]) <=0 )
    if (right<size && comparator.compare(heap.get(right).element, heap.get(smallest).element <=)
      smallest= right;
    if (smallest!= index) {
      exchangeindex, smallest );
      minHeapifyheap.get(smallest) );
    }
  }

  private final void exchangefinal int index, final int index2 ) {
    Node temp = heap.get(index);
    temp.position = index2;

    Node temp2 = heap.get(index2);
    temp2.position = index;

    heap.set(index, temp2 );
    heap.setindex2, temp);


    //Update posistion in Node
    //    heap.get(index).position=index;
    //    heap.get(index2).position=index2;
  }


  private final int parent(final int i) {
    return i/2;
  }

  private final int left(final int i) {
    return 2*i;
  }

  private final int right(final int i) {
    return 2*i+1;
  }

  /**
   * Returns an iterator that iterates over all elements of the heap, in no particular order
   @return
   */
  public final Iterator<T> iterator() {
    return new Iterator<T>() {
      private Iterator<Node> iterator = heap.iterator()
      @Override
      public boolean hasNext() { return iterator.hasNext()}
      @Override
      public T next() { return iterator.next().element; }
      @Override
      public void remove() { }
    };
  }

  //  public void printHeap() {
  //    int step =1;
  //    int i = 0;
  //    for (int n=0;n<size;n++) {
  //      i++;
  //      //System.out.print(""+ ((Contact)heap[n].item).relativeV + "*");
  //      if (i%step == 0 ) {
  //        step *=2; i=0;
  //        System.out.println("");
  //      }
  //    }
  //
  //    System.out.println("");
  //  }
}

   
  
Related examples in the same category
1.Demonstrates heapsDemonstrates heaps
2.Fibonacci heap data structure
3.Binary Heap Queue
4.Tree Heap
5.This class implements the heap interface using a java.util.List as the underlying data structure.
6.A heap-based priority queue, without any concurrency controlA heap-based priority queue, without any concurrency control
7.A MinMaxHeap provides a heap-like data structure that provides fast access to both the minimum and maximum elements of the heap.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.