首页 > 代码库 > 二叉堆

二叉堆

未经测试:
public class BinaryHeap {
	public static final int INIT_CAPACITY = 10;
	private int[] mArray;
	private int mLength;
	
	public BinaryHeap() {
		mArray = new int[INIT_CAPACITY + 1];
		mLength = 0;
	}
	
	private void expandArray(int length) {
		int[] arr = new int[length];
		System.arraycopy(mArray, 1, arr, 1, mLength);
		mArray = arr;
	}
	
	public static final int[] buildHeap(int arr[], int length) {
		if (null == arr || length >= arr.length) {
			return null;
		}
		int[] ret = new int[length + 1];
		System.arraycopy(arr, 0, ret, 1, length);
		// 把第i个value沉下去:
		for (int i = length / 2; i > 0; i--) {
			int index = i;
			while (true) {
				int leftIndex = 2 * index;
				int rightIndex = leftIndex + 1;
				int value = http://www.mamicode.com/ret[index];>