1. 程式人生 > >用java構建一個優先佇列

用java構建一個優先佇列

package Com.Tree;

public class BinaryHeap<AnyType extends Comparable<? super AnyType>> {

	//定義初始化大小
	private int currentSize;
	private final static int DEFAULT_CAPACITY=100;
	private AnyType[] array;
	public BinaryHeap()
	{
		this(DEFAULT_CAPACITY);
	}
	public BinaryHeap(int capacity)
	{
		currentSize = 0;
        array = (AnyType[]) new Comparable[ capacity + 1 ];
	}
	
	//利用一個數組建堆。
	public BinaryHeap(AnyType []items)
	{
		currentSize=items.length;
		array=(AnyType[]) new Comparable[(currentSize+2)*11/10];
		int i=1;
		for(AnyType item:items)
			array[i++]=item;
		buildHeap();
	}
	
	//具體建堆方法
	private void buildHeap()
	{
		for(int i=currentSize/2;i>0;i--)
			precolateDown(i);
	}
	
	//放大陣列
	private void enlargeArray(int capacity)
	{
		AnyType [] newArray=(AnyType[]) new Comparable[capacity];
		
		for(int i=0;i<array.length;i++)
		{
			newArray[i]=array[i];
		}
		array=newArray;
		newArray=null;
	}
	
	
	//向堆中插入一個數
	public void insert(AnyType x)
	{
		if(currentSize==array.length-1)
			enlargeArray(array.length*2+1);
		
		int hole= ++currentSize;
		
		for(array[0]=x;x.compareTo(array[hole/2])<0;hole/=2)
			array[hole]=array[hole/2];
		
		array[hole]=x;
	}
	
	//刪除最小數
	public AnyType deleteMin()
	{
		if(isEmpty())
			throw new ArrayIndexOutOfBoundsException();
		
		AnyType minItem=findMin();
		array[1]=array[currentSize--];
		precolateDown(1);
		
		return minItem;
	}
	
	//下慮操作 將不滿足堆序的數,一步步向下沉 直到滿足堆序
	private void precolateDown(int hole)
	{
		int child;
		AnyType tmp=array[hole];
		
		for(;hole*2<=currentSize;hole=child)
		{
			child=hole*2;
			//紜繚鏈夊彸瀛╁瓙
			if(child!=currentSize&&array[child+1].compareTo(array[child])<0)
				child++;
			
			if(array[child].compareTo(tmp)<0)
				array[hole]=array[child];
			else
				break;
		}
		
		array[hole]=tmp;
	}
	
	//找最小值
	public AnyType findMin(){
		 if( isEmpty( ) )
             return null;
         return array[ 1 ];
	}

	public boolean isEmpty(){return currentSize==0;}
	public void makeEmpty(){currentSize=0;}
} 

 
下面為一個左氏堆  常常用來做合併
 
 
package Com.Tree;

public class LeftistHeap {

	public static class LeftHeapNode
	{		
		LeftHeapNode left;
		LeftHeapNode right;
		Comparable element;
		int npl;
		
		public LeftHeapNode(Comparable element)
		{
			this(element,null,null);
		}
		
		public LeftHeapNode(Comparable element, LeftHeapNode left, LeftHeapNode right)
		{
			this.element=element;
			this.left=left;
			this.right=right;
			npl=0;
		}
	}
	private  LeftHeapNode root;
	public LeftistHeap()
	{
		root=null;
	}
	
	public boolean isEmpty(){return root==null;}
	public void makeEmpty(){root =null;}
	
	public Comparable findMin()
	{
		if(isEmpty())
			return null;
		return root.element;
	}
	
	//交換左右子樹
	private void swapChild(LeftHeapNode t)
	{
		LeftHeapNode tmp=t.right;
		t.right=t.left;
		t.left=tmp;
	}
	
	//插入操作
	public void insert(Comparable x){root=merge(new LeftHeapNode(x),root);}
	
	public LeftHeapNode merge(LeftHeapNode c1,LeftHeapNode c2)
	{
		if(c1==null)
		return c2;
		if(c2==null)
			return c1;
		if(c1.element.compareTo(c2.element)<0)
			return merge1(c1,c2);
		else
			return merge1(c2,c1);
		
	}
	
	private LeftHeapNode merge1(LeftHeapNode lh,LeftHeapNode rh)
	{
		if(lh.left==null)
			lh.left=rh;
		else
		{
		lh.right =merge(lh.right,rh);
		if(lh.left.npl<lh.right.npl)
			swapChild(lh);
		lh.npl=lh.right.npl+1;
		}
		return lh;
	}
	
	//合併操作
	public void merge(LeftistHeap rhs)
	{
		if(this==rhs)
			return ;
		root=merge(root,rhs.root);
		rhs.root=null; //榪涘靉鍨冨漊鍥炴敹
	}
	
	//刪除最小值
	public Comparable deleteMin()
	{
		if(isEmpty())
			return null;
		Comparable minItem=root.element;
		root=merge(root.left,root.right);
		return minItem;
	}
}