一个堆的java实现

写了一个堆的java实现,既然可用数组来表示堆,为了方便,那么就用ArrayList吧。

抽象类Heap:

package com.my.test6;

import java.util.ArrayList;

/**
 * Title: 堆的抽象实现<br/>
 * Intention: 参考:https://my.oschina.net/BreathL/blog/71602<br/>
 * <p>
 * Class Name: com.my.test6.Heap<br/>
 * Create Date: 2017/7/1 23:31 <br/>
 * Project Name: MyTest <br/>
 * Company:  All Rights Reserved. <br/>
 * Copyright © 2017 <br/>
 * </p>
 * <p>
 * author: GaoWei <br/>
 * 1st_examiner: <br/>
 * 2nd_examiner: <br/>
 * </p>
 *
 * @version 1.0
 * @since JDK 1.7
 */
public abstract class Heap<E extends Comparable> {

	private ArrayList<E> items;

	public Heap (ArrayList<E> items) {
		if (items.contains(null)) {
			throw new IllegalArgumentException("Element can not be null");
		}
		this.items = items;
		for(int i=(items.size()-1)/2;i>=0;i--){
			siftDown(i);
		}
	}

	public Heap (E[] arr) {
		items = new ArrayList<E>();
		for (int i=0;i<arr.length;i++) {
			if (arr[i] == null) {
				throw new IllegalArgumentException("Element can not be null");
			} else {
				items.add(arr[i]);
			}
		}
		for(int i=(items.size()-1)/2;i>=0;i--){
			siftDown(i);
		}
	}

	public Heap (){
		items = new ArrayList<E>();
	}

	/**
	 * 返回左孩子下标
	 * @param i
	 * @return
	 */
	private int leftChildIndex(int i) {
		return 2*i + 1;
	}

	/**
	 * 返回右孩子下标
	 * @param i
	 * @return
	 */
	private int rightChildIndex (int i) {
		return 2*i + 2;
	}

	/**
	 * 返回父节点的下标
	 * @param i
	 * @return
	 */
	private int parentIndex (int i) {
		return (i - 1)/2;
	}

	/**
	 * 返回堆的高度
	 * @return
	 */
	public int height() {
		return  (int)(Math.log(items.size())/Math.log(2));
	}

	/**
	 * 下沉操作
	 */
	public void siftDown(int i) {
		E e = items.get(i);
		int lefIndex = leftChildIndex(i);
		while (lefIndex < items.size()) {
			E selectedE = items.get(lefIndex);
			int selectedIndex = lefIndex;
			int rightIndex = lefIndex + 1;

			if (rightIndex < items.size()) {
				if (suitableThan(items.get(rightIndex), selectedE)) {
					selectedE = items.get(rightIndex);
					selectedIndex = rightIndex;
				}
			}

			if (suitableThan(selectedE, e)) {
				items.set(i, selectedE);
				i = selectedIndex;
				lefIndex = leftChildIndex(i);
			} else {
				break;
			}
		}
		items.set(i, e);
	}

	/**
	 * 上浮操作
	 */
	public void siftUp(int i) {
		E e = items.get(i);
		while (i > 0) {
			int parentIndex = parentIndex(i);
			if (suitableThan(e, items.get(parentIndex))) {
				items.set(i, items.get(parentIndex));
				i = parentIndex;
			} else {
				break;
			}
		}
		items.set(i, e);
	}

	public abstract boolean suitableThan(E a, E b);

	/**
	 * 堆尾插入元素
	 * @param e
	 */
	public void push(E e) {
		if(e == null){
			throw new IllegalArgumentException("Element can not be null");
		}
		items.add(e);
		siftUp(items.size() -1);
	}

	/**
	 * 取代堆顶元素
	 * @param e
	 */
	public void replaceTop(E e) {
		if(e == null){
			throw new IllegalArgumentException("Element can not be null");
		}
		items.set(0, e);
		siftDown(0);
	}

	/**
	 * 弹出堆顶元素
	 */
	public E pop() {
		if (!items.isEmpty()) {
			E e = items.get(0);
			E lastElement = items.remove(items.size()-1);
			if (lastElement != null && items.size() > 0){
				items.set(0, lastElement);
				siftDown(0);
			}
			return e;
		} else {
			return null;
		}
	}

	/**
	 * 获得堆顶元素
	 */
	public E top(){
		return items.isEmpty()? null:items.get(0);
	}

	@Override
	public String toString() {
		return items.toString();
	}
}


MaxHeap:

package com.my.test6;

import java.util.ArrayList;

/**
 * Title: 大顶堆实现<br/>
 * Intention: <br/>
 * <p>
 * Class Name: com.my.test6.MaxHeap<br/>
 * Create Date: 2017/7/2 16:50 <br/>
 * Project Name: MyTest <br/>
 * Company:  All Rights Reserved. <br/>
 * Copyright © 2017 <br/>
 * </p>
 * <p>
 * author: GaoWei <br/>
 * 1st_examiner: <br/>
 * 2nd_examiner: <br/>
 * </p>
 *
 * @version 1.0
 * @since JDK 1.7
 */
public class MaxHeap<E extends Comparable> extends Heap  {

	public MaxHeap(){

	}
	public MaxHeap(ArrayList<E> items) {
		super(items);
	}

	public MaxHeap(E[] arr) {
		super(arr);
	}

	@Override
	public boolean suitableThan(Comparable a, Comparable b) {
		return a.compareTo(b) > 0;
	}
}


MinHeap:

package com.my.test6;

import java.util.ArrayList;

/**
 * Title: 小顶堆实现<br/>
 * Intention: <br/>
 * <p>
 * Class Name: com.my.test6.MinHeap<br/>
 * Create Date: 2017/7/2 14:45 <br/>
 * Project Name: MyTest <br/>
 * Company:  All Rights Reserved. <br/>
 * Copyright © 2017 <br/>
 * </p>
 * <p>
 * author: GaoWei <br/>
 * 1st_examiner: <br/>
 * 2nd_examiner: <br/>
 * </p>
 *
 * @version 1.0
 * @since JDK 1.7
 */
public class MinHeap<E extends Comparable> extends Heap {

	public MinHeap(){

	}
	public MinHeap(ArrayList<E> items) {
		super(items);
	}

	public MinHeap(E[] arr) {
		super(arr);
	}

	@Override
	public boolean suitableThan(Comparable a, Comparable b) {
		return a.compareTo(b) < 0;
	}
}


MyTest:

package com.my.test6;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Title: <br/>
 * Intention: <br/>
 * <p>
 * Class Name: com.my.test6.MyTest<br/>
 * Create Date: 2017/7/2 16:51 <br/>
 * Project Name: MyTest <br/>
 * Company:  All Rights Reserved. <br/>
 * Copyright © 2017 <br/>
 * </p>
 * <p>
 * author: GaoWei <br/>
 * 1st_examiner: <br/>
 * 2nd_examiner: <br/>
 * </p>
 *
 * @version 1.0
 * @since JDK 1.7
 */
public class MyTest {

	public static void main(String[] args) {
		MinHeap<Integer> myMinHeap = new MinHeap<Integer>();
		myMinHeap.push(100);
		myMinHeap.push(21);
		myMinHeap.push(3);
		myMinHeap.push(45);
		myMinHeap.push(578);
		myMinHeap.push(6);
		System.out.println("myMinHeap: "+myMinHeap);

		MaxHeap<Integer> myMaxHeap = new MaxHeap<Integer>();
		myMaxHeap.push(100);
		myMaxHeap.push(21);
		myMaxHeap.push(3);
		myMaxHeap.push(45);
		myMaxHeap.push(578);
		myMaxHeap.push(6);
		System.out.println("myMaxHeap: " + myMaxHeap);
		myMaxHeap.pop();
		System.out.println("myMaxHeap: " + myMaxHeap);
		System.out.println("myMaxHeap.height="+myMaxHeap.height());

		Heap<Integer> h3 = new MinHeap<Integer>(new ArrayList(Arrays.asList(new Integer[]{10,12,100,1,34,65,98,0})));
		System.out.println("h3=" + h3);
		System.out.println("h3.height="+h3.height());
		System.out.println("h3 top="+h3.top());


	}
}




参考:

1、https://my.oschina.net/BreathL/blog/71602

2、http://blog.csdn.net/zhutulang/article/details/7746033





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值