版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32250495/article/details/76980648
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;
}
}