写了一个堆的java实现,既然可用数组来表示堆,为了方便,那么就用ArrayList吧。
抽象类Heap:
package com.my.test6;
import java.util.ArrayList;
/**
* Title: 堆的抽象实现
* Intention: 参考:https://my.oschina.net/BreathL/blog/71602
*
* Class Name: com.my.test6.Heap
* Create Date: 2017/7/1 23:31
* Project Name: MyTest
* Company: All Rights Reserved.
* Copyright © 2017
*
*
* author: GaoWei
* 1st_examiner:
* 2nd_examiner:
*
*
* @version 1.0
* @since JDK 1.7
*/
public abstract class Heap {
private ArrayList items;
public Heap (ArrayList 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();
for (int i=0;i=0;i--){
siftDown(i);
}
}
public Heap (){
items = new ArrayList();
}
/**
* 返回左孩子下标
* @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();
}
}
package com.my.test6;
import java.util.ArrayList;
/**
* Title: 大顶堆实现
* Intention:
*
* Class Name: com.my.test6.MaxHeap
* Create Date: 2017/7/2 16:50
* Project Name: MyTest
* Company: All Rights Reserved.
* Copyright © 2017
*
*
* author: GaoWei
* 1st_examiner:
* 2nd_examiner:
*
*
* @version 1.0
* @since JDK 1.7
*/
public class MaxHeap extends Heap {
public MaxHeap(){
}
public MaxHeap(ArrayList 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: 小顶堆实现
* Intention:
*
* Class Name: com.my.test6.MinHeap
* Create Date: 2017/7/2 14:45
* Project Name: MyTest
* Company: All Rights Reserved.
* Copyright © 2017
*
*
* author: GaoWei
* 1st_examiner:
* 2nd_examiner:
*
*
* @version 1.0
* @since JDK 1.7
*/
public class MinHeap extends Heap {
public MinHeap(){
}
public MinHeap(ArrayList 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:
* Intention:
*
* Class Name: com.my.test6.MyTest
* Create Date: 2017/7/2 16:51
* Project Name: MyTest
* Company: All Rights Reserved.
* Copyright © 2017
*
*
* author: GaoWei
* 1st_examiner:
* 2nd_examiner:
*
*
* @version 1.0
* @since JDK 1.7
*/
public class MyTest {
public static void main(String[] args) {
MinHeap myMinHeap = new MinHeap();
myMinHeap.push(100);
myMinHeap.push(21);
myMinHeap.push(3);
myMinHeap.push(45);
myMinHeap.push(578);
myMinHeap.push(6);
System.out.println("myMinHeap: "+myMinHeap);
MaxHeap myMaxHeap = new MaxHeap();
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 h3 = new MinHeap(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