堆(heap)實現

package Heap;

import java.util.Comparator;

//使用數組實現最大堆
//以節點第i個元素爲例
//左右子節點分別爲2*i+1, 2*i+2 父節點爲(i-1)/2
public class Heap {
	
	//增加元素
	public static <T> void pushHeap(T[] arr, int last,T item, Comparator<? super T> comp){
		int currPos,parentPos;
		currPos = last;
		parentPos = (currPos-1)/2;
		
		while(currPos != 0){
			//父節點的元素小於當前元素,則把父節點的值,複製到子節點
			if( comp.compare( item, arr[parentPos] ) < 0 ){
				arr[currPos] = arr[parentPos];
				currPos = parentPos;
				parentPos= (currPos-1)/2;
			}
			else{
				break;
			}
		}//end while
		arr[currPos] = item;
	}//end pushHeap
	
	//刪除元素
	public static <T> T popHeap(T[] arr, int last,Comparator<? super T> comp){
		T temp = arr[0];
		arr[0] = arr[last-1];
		arr[last-1] = temp;
		
		adjustHeap(arr, 0, last-1, comp);
		return temp;
	}
	
	//調整堆頂點使其成爲最大堆
	public static <T> void adjustHeap(T[] arr, int first, int last,Comparator<? super T> comp){
		int currPos,childPos;
		T target;
		currPos = first;
		target = arr[first];
		
		childPos = currPos*2+1;
		while(childPos < last){
			if((childPos+1 < last) &&comp.compare(arr[childPos+1], arr[childPos]) < 0){
				childPos =childPos + 1;
			}
			if (comp.compare(arr[childPos],target) < 0){
				arr[currPos] = arr[childPos];
				currPos = childPos;
				childPos = currPos*2 +1;
			}
			else{
				break;
			}
		}//end whiel
		
		arr[currPos] = target;
		
	}//end abjustHeap()
	
	//數組的堆化,最大堆要求每個節點的值都大於子節點,因此葉子節點都滿足最大堆
	//最後一個內節點爲((last-1)-1)/2=(last-2)/2
	//把每個子堆都變成最大堆,最後就形成了一個最大堆
	public static<T> void makeHeap(T arr[],Comparator<? super T> comp){
		
		int heapPos, lastPos;
		lastPos = arr.length;
		heapPos = (lastPos-2)/2;
		
		while(heapPos >= 0){
			adjustHeap(arr, heapPos, lastPos, comp);
			heapPos --;
		}
	}//end makeheap()
	
	//堆排序
	public static <T> void heapSort(T[] arr, Comparator<? super T> comp){
		Heap.makeHeap(arr, comp);
		
		int i, n = arr.length;
		
		for(i = n ; i > 1 ; i--){
			Heap.popHeap(arr, i, comp);
		}
	}

}


package Heap;

import java.util.Comparator;
import java.util.NoSuchElementException;

import 算法.PQueue;

//優先隊列的實現
public class HeapPQueue<T> implements PQueue<T> {
	private T[] heapElt;
	private int numElt;
	private Comparator<T> comp;
	
	public HeapPQueue(){
		comp = new Less<T>();
		numElt = 0;
		heapElt =(T[])  new Object[10];
	}
	
	public void push(T item) {
		if(numElt == heapElt.length){
			enlargeCapacity();
		}
		Heap.pushHeap(heapElt, numElt, item, comp);
		numElt ++;
	}

	private void enlargeCapacity() {
		T[] oldHeapElt = heapElt;
		T[] newHeapElt =(T[]) new Object[heapElt.length * 2];
		for(int i = 0; i < oldHeapElt.length; i++){
			newHeapElt[i] = oldHeapElt[i];
		}
		heapElt = newHeapElt;
		oldHeapElt = null;
		
	}

	@Override
	public T pop() {
		if(numElt == 0){
			throw new NoSuchElementException();
		}
		T top = Heap.popHeap(heapElt, numElt, comp);
		numElt -- ;
		return top;
	}

	@Override
	public T peek() {
		if(numElt == 0){
			throw new NoSuchElementException();
		}
		return heapElt[0];
	}

	@Override
	public boolean isEmpty() {
		return numElt == 0;
	}

	@Override
	public int size() {
		return numElt;
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章