您的位置:首页 > 编程语言 > Java开发

Java heap的实现 最小堆的实现 代码简洁

2014-02-18 10:25 344 查看
public class HeapMin {
	private int[] Heap;
	private int maxsize;
	private int size;

	public HeapMin(int max) {
		maxsize = max;
		Heap = new int[maxsize];
		size = 0;
		Heap[0] = Integer.MIN_VALUE;
	}

	private int leftchild(int pos) {
		return 2 * pos;
	}

	private int rightchild(int pos) {
		return 2 * pos + 1;
	}

	private int parent(int pos) {
		return pos / 2;
	}

	private boolean isleaf(int pos) {
		return ((pos > size / 2) && (pos <= size));
	}

	private void swap(int pos1, int pos2) {
		int tmp;
		tmp = Heap[pos1];
		Heap[pos1] = Heap[pos2];
		Heap[pos2] = tmp;
	}

	public void insert(int elem) {
		size++;
		Heap[size] = elem;
		int current = size;
		while (Heap[current] < Heap[parent(current)]) {
			swap(current, parent(current));
			current = parent(current);
		}
	}

	public void print() {
		int i;
		for (i = 1; i <= size; i++)
			System.out.print(Heap[i] + " ");
		System.out.println();
	}

	public int removemin() {
		swap(1, size);
		size--;
		if (size != 0)
			pushdown(1);
		return Heap[size + 1];
	}

	private void pushdown(int position) {
		int smallestchild;
		while (!isleaf(position)) {
			smallestchild = leftchild(position);
			if ((smallestchild < size)
					&& (Heap[smallestchild] > Heap[smallestchild + 1]))
				smallestchild = smallestchild + 1;
			if (Heap[position] <= Heap[smallestchild])
				return;
			swap(position, smallestchild);
			position = smallestchild;
		}
	}

	public static void main(String args[])
	{
		HeapMin hm = new HeapMin(6);
		hm.insert(1);
		hm.insert(30);
		hm.insert(50);
		hm.insert(20);
		hm.insert(70);
		hm.print();
		
	}
}


insert函数包含了ShiftUp的过程。pushDown就是ShiftDown的过程。

要变成最大堆,只需要修改这两个过程的大于小于

来源:http://wenwen.soso.com/z/q278758109.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: