用java构建一个优先队列

来源:互联网 发布:2017欧文季后赛数据 编辑:程序博客网 时间:2024/06/05 15:46
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];elsebreak;}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);elsereturn 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;}}
 
原创粉丝点击