Java实现堆(最大堆,最小堆)
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现堆(最大堆,最小堆)相关的知识,希望对你有一定的参考价值。
最大堆代码如下:
package MaxHeap;
import java.util.Scanner;
public class Heap {
private int data[];
private int size;
private int capacity;
private final int maxNum = 999999999;
public Heap()
{
data = new int[10+1];
size = 0;
capacity= 10;
data[0] = maxNum;
}
public Heap(int n)
{
data = new int[n+1];
size = 0;
capacity = n;
data[0] = maxNum;
}
public boolean isFull()
{
return size==capacity;
}
public boolean isEmpty()
{
return size==0;
}
public boolean insertHeap(int e)
{
if (isFull())
{
System.out.println("The heap is full");
return false;
}
int i = ++size;
for (;data[i/2] < e;i = i/2 )
{
data[i] = data[i/2];
}
data[i] = e;
return true;
}
public int deleteMax()
{
if (isEmpty())
{
System.out.println("The heap is empty");
return -1;
}
int maxData = data[1];
int x = data[size--];
int parent,child;
for (parent = 1;parent * 2<=size;parent = child)
{
child = parent*2;
if (child!=size && data[child] < data[child+1])
{
child++;
}
if (x >= data[child]) break;
else data[parent] = data[child];
}
data[parent] = x;
return maxData;
}
public void percDown(int p)
{
int parent,child;
int x = data[p];
for (parent = p;parent*2<=size;parent = child)
{
child = 2*parent;
if (child !=size && data[child] <data[child+1])
{
child++;
}
if (x >= data[child]) break;
else data[parent] = data[child];
}
data[parent] = x;
}
public void buildHeap(int n)
{
Scanner sc = new Scanner(System.in);
for (int i = 1;i<=n;i++)
{
data[i] = sc.nextInt();
size++;
}
for (int i = size/2;i>0;i--)
{
percDown(i);
}
}
}
测试类如下:
package MaxHeap;
public class TestHeap {
public static void main(String[] args)
{
Heap h = new Heap(10);
h.buildHeap(5);
h.insertHeap(9999);
System.out.println(h.deleteMax());
System.out.println(h.deleteMax());
}
}
最小堆代码如下:
package MinHeap;
import java.util.Scanner;
public class Heap {
private int data[];
private int size;
private int capacity;
private final int minNum = -999999999;
public Heap()
{
data = new int[10+1];
size = 0;
capacity = 10;
data[0] = minNum;
}
public Heap(int n)
{
data = new int[n+1];
size = 0;
capacity = n;
data[0] = minNum;
}
public boolean isFull()
{
return size==capacity;
}
public boolean isEmpty()
{
return size==0;
}
public boolean insertHeap(int e)
{
if (isFull())
{
System.out.println("The heap is full");
return false;
}
int i = ++size;
for (;data[i/2] > e;i = i/2)
{
data[i] = data[i/2];
}
data[i] = e;
return true;
}
public int deleteMin()
{
if (isEmpty())
{
System.out.println("The heap is empty");
return -1;
}
int parent,child;
int minData = data[1];
int x = data[size--];
for (parent = 1;parent*2 <= size;parent = child)
{
child = parent*2;
if (child !=size && data[child] > data[child+1])
{
child++;
}
if (x <= data[child]) break;
else data[parent] = data[child];
}
data[parent] = x;
return minData;
}
public void percDown(int p)
{
int parent,child;
int x = data[p];
for (parent = p;parent*2<=size;parent = child)
{
child = 2*parent;
if (child !=size && data[child] > data[child+1])
{
child++;
}
if (x <= data[child]) break;
else data[parent] = data[child];
}
data[parent] = x;
}
public void bulidHeap(int n)
{
Scanner sc = new Scanner(System.in);
for (int i = 1;i<=n;i++)
{
data[i] = sc.nextInt();
size++;
}
for (int i = size/2;i>0;i--)
{
percDown(i);
}
}
}
测试类如下:
package MinHeap;
public class TestHeap {
public static void main(String[] args)
{
Heap h = new Heap();
h.bulidHeap(5);
h.insertHeap(-8);
System.out.println(h.deleteMin());
System.out.println(h.deleteMin());
}
}
以上是关于Java实现堆(最大堆,最小堆)的主要内容,如果未能解决你的问题,请参考以下文章