java数据结构堆_java数据结构—堆 | 学步园

1.  由于用数组来模拟堆的数据存放比较方便,且数组0号位置作为哨兵

package com.jzm.HeapMap;

public class Maxheap >{

private T[] heap; // 存放堆 元素的数组

private int lastIndex; //最后一个元素的索引

private static final int default_capacity = 20;

public Maxheap(){

this(default_capacity);

}

public Maxheap(int size){

heap = (T[])new Comparable[size];

lastIndex = 0;

} //end constructor

public boolean isEmpty(){

return lastIndex < 1;

}//判断是否为空

public void doubleArray(){

int enlarge = heap.length*2;

T[] a = (T [])new Comparable[enlarge];

for(int i=1;i<=lastIndex;i++){

a[i] = heap[i];

}

heap = a;

}//扩大数组空间

public void add(T newEntry){

lastIndex++;

if (lastIndex >= heap.length) {

doubleArray();

}

int newIndex = lastIndex;

int parentIndex = newIndex/2;

while(newIndex >1 && newEntry.compareTo(heap[parentIndex])>0){

heap[newIndex] = heap[parentIndex];

newIndex = parentIndex;

parentIndex = newIndex/2;

}//end while

heap[newIndex] = newEntry;

}//end add

private void reheap(int rootIndex){

boolean done = false;

T orphanT = heap[rootIndex];

int largerChildIndex = 2*rootIndex;

while(!done && largerChildIndex <= lastIndex)

{

int leftChildIndex = largerChildIndex;

int rightChildIndex = leftChildIndex+1;

if(rightChildIndex <= lastIndex

&&heap[rightChildIndex].compareTo(heap[largerChildIndex])>0){

largerChildIndex = rightChildIndex;

} //找到最大的孩子

if(orphanT.compareTo(heap[largerChildIndex])<0){

heap[rootIndex] = heap[largerChildIndex];

rootIndex = largerChildIndex;

largerChildIndex = 2*rootIndex;

}else{

done = true;

}

}//end while

heap[rootIndex] = orphanT;

}//end reheap

public T removeMax(){

T root = null;

if(!isEmpty()){

root = heap[1]; //返回值

heap[1] = heap[lastIndex]; //形成半堆

lastIndex--; //堆的大小减1

reheap(1); //转化为堆

}//end if

return root;

} //end removeMax

public T getMax(){

T root = null;

if (!isEmpty()) {

root = heap[1];

}

return root;

}//end getMax 得到大顶堆顶数

public int getSize(){

return lastIndex;

}//得到堆的大小

public void clear(){

for(; lastIndex >= 0; lastIndex--){

heap[lastIndex]=null;

lastIndex = 0;

}

}//end clear 清空堆

public void display(){

for (int i = 1; i <=lastIndex; i++) {

System.out.println(" "+heap[i]);

}

}

public static void main(String[] args) {

Maxheap maxheap = new Maxheap();

maxheap.add(1);

maxheap.add(2);

maxheap.add(5);

maxheap.add(6);

maxheap.add(3);

maxheap.add(4);

maxheap.display();

maxheap.removeMax();

System.out.println("---------------");

maxheap.display();

}

}

2.   堆排序

package com.jzm.HeapMap;

public class HeapSort{

private static> void reheap(T []heap,int rootIndex,int lastIndex){

boolean done = false;

T orphanT = heap[rootIndex];

int largerChildIndex = 2*rootIndex;

while(!done && largerChildIndex <= lastIndex)

{

int leftChildIndex = largerChildIndex;

int rightChildIndex = leftChildIndex+1;

if(rightChildIndex <= lastIndex

&&heap[rightChildIndex].compareTo(heap[largerChildIndex])>0){

largerChildIndex = rightChildIndex;

} //找到最大的孩子

if(orphanT.compareTo(heap[largerChildIndex])<0){

heap[rootIndex] = heap[largerChildIndex];

rootIndex = largerChildIndex;

largerChildIndex = 2*rootIndex;

}else{

done = true;

}

}//end while

heap[rootIndex] = orphanT;

}//end reheap

private static > void swap(T []a,int i,int j){

T t = a[i];

a[i]= a[j];

a[j] = t;

}

public static> void heapSort(T []array,int n){

//创建初始堆

for (int rootIndex = n/2-1; rootIndex >= 0; rootIndex--){

reheap(array,rootIndex,n-1);

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

System.out.print(" "+array[i]);

}

System.out.println("\n---------------------------");

}

swap(array,0,n-1);

for (int lastIndex =n-2; lastIndex>0 ; lastIndex--){

reheap(array, 0, lastIndex);

swap(array,0,lastIndex);

}//end for

}//end heapSort

public static void main(String[] args) {

Integer [] a={20,40,30,10,90,70};

heapSort(a,a.length);

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

System.out.print(" "+a[i]);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值