java数据结构之MaxHeap

 MaxHeap:一个完全二叉树,父节点比子节点大,可用数组表示

设父节点为i,则左子节点为2*i,右子节点为2*i+1

 

public interface MyMaxHeapInterf<T extends Comparable<? super T>> {

public void add(T Entry);

public void clear();

public T removeMax();

public T getMax();

public boolean isEmpty();

public int getSize();

}

 

public class MyMaxHeap<T extends Comparable<?super T>> implements MyMaxHeapInterf<T>, Serializable {

 

private static final int CAP=5;  //CAP为节点个数,长为

//默认10个

 

private T[] heap;//数组 heap.length=节点个数加一,index=0的保留为哨兵

// _________________________________

//  |           |      5    |    3      |     1    |     4  |     2  |

//_________________________________

 //         0         1          2           3           4        5          <----index

                                                                          ^

                                                                          |

                                                                     lastIndex

 

private int lastIndex;           //节点索引,从一开始

 

// public void doubleArray(){

// int oldlen=heap.length;

// T[] tmpheap=(T[])new Comparable[CAP+heap.length];

// for(int i=0;i<oldlen;i++)

// // tmpheap[i]=new Comparable(heap[i]);

// for(;oldlen>0;oldlen--){

// heap[oldlen]=null;

// }

// //lastIndex=tmpheap.length;

//

// }

 

//加入时就建好堆

public void add(T Entry) {

lastIndex++;

//if( (lastIndex+1)>=heap.length)

//doubleArray();

 

int newIndex=lastIndex;

int parent=newIndex/2;

while(newIndex>1&&Entry.compareTo(heap[parent])>0){

heap[newIndex]=heap[parent];

newIndex=parent;

parent=newIndex/2;

}

heap[newIndex]=Entry;

}

public MyMaxHeap(){

this(CAP);//用默认

}

 

public MyMaxHeap(int cap){

heap=(T[])new Comparable[cap+1];            //多生成一个,cap+1

lastIndex=0;

}

 

public MyMaxHeap(T[] arr){

heap=arr;

lastIndex=arr.length-1;

}

public void clear() {

for(;lastIndex>-1;lastIndex--){

heap[lastIndex]=null;

}

lastIndex=0;

}

public T removeMax() {

T root=heap[1];

heap[1]=heap[lastIndex];

lastIndex--;

reheap(1);

return root;

}

//最大堆,第一个就是Max

public T getMax() {

T root=null;

if(!isEmpty()){

root=heap[1];

}

return root;

}

public boolean isEmpty() {

return lastIndex<1;

}

public int getSize() {

 

return lastIndex;

}

public void reheap(int rootIndex ){

boolean done=false;

T orphan=heap[rootIndex];

int largeChild=2*rootIndex;

 

// reheap(1)时,根节点 rootIndex=1 ,leftChild=2 heap[rootIndex]=20 heap[2]=90 heap[3]=70

 

        // 20

      //     /   \

    //     90  70

//        / \     /

//   10 40 30

 

int rightChild=0;

int leftChild=0;

//第一次循环时 rootIndex=1,largeChild=2 ,rightChild=3

// orphan.compareTo(heap[largeChild])<0,则orphan=老的根节点20小于大的子节点90, 90上移 heap[rootIndex]=90

//heap[rootIndex]=heap[largeChild];

 

// rootIndex=largeChild; rootIndex下移重新赋值=2

//largeChild=rootIndex*2; largeChild=4 //rightChild=5

//

 

while(!done&&(largeChild<=lastIndex))

{

leftChild =largeChild;

rightChild=leftChild+1;

if(rightChild<=lastIndex&&heap[rightChild].compareTo(heap[largeChild])>0)

{

largeChild=rightChild;

}

if(orphan.compareTo(heap[largeChild])<0)

{heap[rootIndex]=heap[largeChild];

rootIndex=largeChild;

largeChild=rootIndex*2;

}

}

heap[rootIndex]=orphan;

}

public String print() {

StringBuffer sb = new StringBuffer();

for(int i=0; i<heap.length; i++) {

sb.append(heap[i]);

if(i != heap.length-1) {

sb.append(", ");

}

}

return sb.toString();

}

public static void main(String[]args){

 

//方式一,用reheap()建堆

Integer g[]=new Integer[5+1];

g[1]=1;

g[2]=2;

g[3]=3;

g[4]=4;

g[5]=5;

MyMaxHeap<Integer> h=new MyMaxHeap<Integer>(g);

for(int i=h.lastIndex/2;i>0;i--)

h.reheap(i);

System.out.println(h.print());

 

 

//方式二,用add()建堆

 

// h.add(new Integer(9));

// h.add(new Integer(6));

// h.add(new Integer(12));

// h.add(new Integer(34));

// h.add(new Integer(35));

//

 

System.out.println(h.lastIndex);

System.out.println(h.getSize());

System.out.println(h.print());

 

// heap.add(new Integer(19));

// heap.add(new Integer(16));

// heap.add(new Integer(112));

// heap.add(new Integer(134));

// heap.add(new Integer(135));

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值