数据结构(Java)——堆的应用
生活总是会有大喜大悲,人生总是会有大起大落!不以物喜,不以己悲~
1.优先级队列的实现
优先级队列是遵循两个排序规则的集合。首先,具有更高优先级的项目在先。第二,具有相同优先级的项目使用先进先出的方法确定其排序。
首先我们创建一个PrioritizedObject对象,它存储的是将被放置在队列中的元素,该元素的优先级以及该元素放进队列的顺序。然后我们需要为它定义一个compareTo方法。
package ds.java.ch12;
/**
* PrioritizedObject represents a node in a priority queue containing a
* comparable object, arrival order, and a priority value.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PrioritizedObject<T> implements Comparable<PrioritizedObject>
{
private static int nextOrder = 0;
private int priority;
private int arrivalOrder;
private T element;
/**
* Creates a new PrioritizedObject with the specified data.
*
* @param element the element of the new priority queue node
* @param priority the priority of the new queue node
*/
public PrioritizedObject(T element, int priority)
{
this.element = element;
this.priority = priority;
arrivalOrder = nextOrder;
nextOrder++;
}
/**
* Returns the element in this node.
*
* @return the element contained within the node
*/
public T getElement()
{
return element;
}
/**
* Returns the priority value for this node.
*
* @return the integer priority for this node
*/
public int getPriority()
{
return priority;
}
/**
* Returns the arrival order for this node.
*
* @return the integer arrival order for this node
*/
public int getArrivalOrder()
{
return arrivalOrder;
}
/**
* Returns a string representation for this node.
*
*/
public String toString()
{
return (element + " " + priority + " " + arrivalOrder);
}
/**
* Returns 1 if the this object has higher priority than
* the given object and -1 otherwise.
*
* @param obj the object to compare to this node
* @return the result of the comparison of the given object and
* this one
*/
public int compareTo(PrioritizedObject obj)
{
int result;
if (priority > obj.getPriority())
result = 1;
else if (priority < obj.getPriority())
result = -1;
else if (arrivalOrder > obj.getArrivalOrder())
result = 1;
else
result = -1;
return result;
}
}
优先级队列的实现
package ds.java.ch12;
import ds.java.ch12.heapImpl.ArrayHeap;
/**
* PriorityQueue implements a priority queue using a heap.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PriorityQueue<T> extends ArrayHeap<PrioritizedObject<T>>
{
/**
* Creates an empty priority queue.
*/
public PriorityQueue()
{
super();
}
/**
* Adds the given element to this PriorityQueue.
*
* @param object the element to be added to the priority queue
* @param priority the integer priority of the element to be added
*/
public void addElement(T object, int priority)
{
PrioritizedObject<T> obj = new PrioritizedObject<T>(object, priority);
super.addElement(obj);
}
/**
* Removes the next highest priority element from this priority
* queue and returns a reference to it.
*
* @return a reference to the next highest priority element in this queue
*/
public T removeNext()
{
PrioritizedObject<T> obj = (PrioritizedObject<T>)super.removeMin();
return obj.getElement();
}
}
2.堆排序的实现
package ds.java.ch12;
import ds.java.ch12.heapImpl.ArrayHeap;
/**
* HeapSort sorts a given array of Comparable objects using a heap.
*
* @author Lewis and Chase
* @version 4.0
*/
public class HeapSort<T> {
/**
* Sorts the specified array using a Heap
*
* @param data
* the data to be added to the heapsort
*/
public void HeapSort(T[] data) {
ArrayHeap<T> temp = new ArrayHeap<T>();
// copy the array into a heap
for (int i = 0; i < data.length; i++)
temp.addElement(data[i]);
// place the sorted elements back into the array
int count = 0;
while (!(temp.isEmpty())) {
data[count] = temp.removeMin();
count++;
}
}
public static void main(String[] args) {
Integer[] data = {3,1,2,5,8,7,6,9,0,4};
HeapSort hs = new HeapSort<Integer>();
hs.HeapSort(data);
for(int i=0;i<data.length;i++){
System.out.println(data[i]);
}
}
}
3.堆一些知识点总结
- 如果一个堆是平衡的,即所有的叶子都位于层h或h-1,其中h为log2n且n是树中的元素数目,以及所有层h中的叶子都位于树的左边,那么该堆就是完全的。
- 一个完全堆是平衡的并且添加删除算法保持了该堆的平衡性。
- add操作必须确定插入点的双亲,这样能够将该结点的某一个孩子指针设定为指向新结点。数组实现不一定需要确定新结点的双亲,因为数组实现是拥有count位置来定位的。
- 数组实现的堆的插入和删除都是更高效的。
踏实 踏踏实实~