最大堆的实现

/** max heap */ package dataStructures; import javax.lang.model.element.Element; import utilities.*; public class MaxHeap implements MaxPriorityQueue { // data members Comparable [] heap; // array for complete binary tree int size; // number of elements in heap // constructors /** create a heap with the given initial capacity * @throws IllegalArgumentException when * initialCapacity < 1 */ public MaxHeap(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException ("initialCapacity must be >= 1"); heap = new Comparable [initialCapacity + 1]; size = 0; } /** create a heap with initial capacity 10 */ public MaxHeap() {this(10);} // methods /** @return true iff the heap is empty */ public boolean isEmpty() {return size == 0;} /** @return number of elements in the heap */ public int size() {return size;} /** @return maximum element * @return null if the heap is empty */ public Comparable getMax() {return (size == 0) ? null : heap[1];} /** put theElement into the heap */ public void put(Comparable theElement) { //increase the size if necessary if(size == heap.length -1) heap = (Comparable[]) ChangeArrayLength.changeLength1D(heap, 2* heap.length); //find the place for the current node int currentNode = ++size; while(currentNode!=1 && heap[currentNode/2].compareTo(theElement) < 0 ){ heap[currentNode] = heap[currentNode/2]; //move element down currentNode /= 2; //move to parent } heap[currentNode] = theElement; } /** remove max element and return it */ public Comparable removeMax() { // if heap is empty return null if (size == 0) return null; // heap empty Comparable maxElement = heap[1]; // max element // reheapify Comparable lastElement = heap[size--]; // find place for lastElement starting at root int currentNode = 1, child = 2; // child of currentNode while (child <= size) { // heap[child] should be larger child of currentNode if (child < size && heap[child].compareTo(heap[child + 1]) < 0) child++; // can we put lastElement in heap[currentNode]? if (lastElement.compareTo(heap[child]) >= 0) break; // yes // no heap[currentNode] = heap[child]; // move child up currentNode = child; // move down a level child *= 2; } heap[currentNode] = lastElement; return maxElement; } /** initialize max heap to element array theHeap */ public void initialize(Comparable [] theHeap, int theSize) { heap = theHeap; size = theSize; // heapify for (int root = size / 2; root >= 1; root--) { Comparable rootElement = heap[root]; // find place to put rootElement int child = 2 * root; // parent of child is target // location for rootElement while (child <= size) { // heap[child] should be larger sibling if (child < size && heap[child].compareTo(heap[child + 1]) < 0) child++; // can we put rootElement in heap[child/2]? if (rootElement.compareTo(heap[child]) >= 0) break; // yes // no heap[child / 2] = heap[child]; // move child up child *= 2; // move down a level } heap[child / 2] = rootElement; } } public String toString() { StringBuffer s = new StringBuffer(); s.append("The " + size + " elements are ["); if (size > 0) {// nonempty heap // do first element s.append(heap[1]); // do remaining elements for (int i = 2; i <= size; i++) s.append(", " + heap[i]); } s.append("]"); return new String(s); } /** test program */ public static void main(String [] args) { // test constructor and put MaxHeap h = new MaxHeap(4); h.put(new Integer(10)); h.put(new Integer(20)); h.put(new Integer(5)); // test toString System.out.println("Elements in array order are"); System.out.println(h); System.out.println(); h.put(new Integer(15)); h.put(new Integer(30)); System.out.println("Elements in array order are"); System.out.println(h); System.out.println(); // test remove max System.out.println("The max element is " + h.getMax()); System.out.println("Deleted max element " + h.removeMax()); System.out.println("Deleted max element " + h.removeMax()); System.out.println("Elements in array order are"); System.out.println(h); System.out.println(); // test initialize Comparable [] z = new Comparable [10]; for (int i = 1; i < 10; i++) z[i] = new Integer(i); h.initialize(z, 9); System.out.println("Elements in array order are"); System.out.println(h); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值