包含泛型的大顶堆

/**
 * @author zfl
 * @param <AnyType>
 * 包含泛型的大顶堆
 */
public class Heap<AnyType extends Comparable<? super AnyType>> {
	private Node<AnyType>[] heapArray;
	private int maxSize;
	private int currentSize;

	public Heap(int maxsize) {
		maxSize = maxsize;
		currentSize = 0;
		heapArray = new Node[maxSize];
	}

	public boolean isEmpty() {
		return currentSize == 0;
	}

	public boolean isFull() {
		return currentSize == maxSize;
	}

	public boolean insert(AnyType value) {
		if (isFull())
			return false;
		heapArray[currentSize] = new Node<AnyType>(value);
		percolateUp(currentSize++);
		return true;
	}

	// 上滤
	private void percolateUp(int index) {
		int parent = (index - 1) >> 1;
		Node<AnyType> tmp = heapArray[index];
		while (index > 0 && tmp.nData.compareTo(heapArray[parent].nData) > 0) {
			heapArray[index] = heapArray[parent];
			index = parent;
			parent = (index - 1) >> 1;
		}
		heapArray[index] = tmp;
	}

	public Node<AnyType> remove() {
		Node<AnyType> root = heapArray[0];
		heapArray[0] = heapArray[--currentSize];
		percolateDown(0);
		return root;
	}

	// 下滤
	private void percolateDown(int index) {
		Node<AnyType> tmp = heapArray[index];
		int child, lchild, rchild;
		while (index < currentSize / 2) {
			lchild = 2 * index + 1;
			rchild = lchild + 1;
			if (rchild < currentSize && heapArray[lchild].nData.compareTo(heapArray[rchild].nData) < 0) {
				child = rchild;
			} else
				child = lchild;
			heapArray[index] = heapArray[child];
			index = child;
		}
		heapArray[index] = tmp;
	}

	// 堆元素修改
	public boolean change(int index, AnyType newValue) {
		if (index < 0 || index >= currentSize) {
			return false;
		}
		AnyType oldValue = heapArray[index].nData;
		heapArray[index].setnData(newValue);
		if (newValue.compareTo(oldValue) > 0)
			percolateUp(index);
		else
			percolateDown(index);
		return true;
	}

	// 打印堆元素
	public void displayHeap() {
		System.out.println("heapArray:");
		int maxline = (int) (Math.log(currentSize) / Math.log(2));
		for (int i = 0, n = 1; i < currentSize; i++) {
			if (i == Math.pow(2, n - 1) - 1) {
				for (int t = 0; t <= maxline - n; t++)
					System.out.print("\t");
			}
			if (i == Math.pow(2, n) - 2) {
				System.out.println(heapArray[i].nData);
				n++;
			} else
				System.out.print(heapArray[i].nData + "\t  ");
		}

	}

	static class Node<AnyType extends Comparable<? super AnyType>> {
		private AnyType nData;

		public Node(AnyType nData) {
			super();
			this.nData = nData;
		}

		public AnyType getnData() {
			return nData;
		}

		public void setnData(AnyType nData) {
			this.nData = nData;
		}
	}

	// 测试
	public static void main(String args[]) {
		Heap<Integer> h = new Heap<Integer>(20);
		h.insert(2);
		h.insert(5);
		h.insert(9);
		h.insert(12);
		h.insert(3);
		h.insert(7);
		h.insert(6);
		h.insert(2);
		h.insert(23);
		h.displayHeap();
	}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值