了解优先级队列中的SORT方法

时间:2018-07-08 12:00:47

标签: java heap priority-queue

我有一个同学写的代码。

问题是我无法理解排序方法的作用。

据我所知,它命令优先级队列没有被修改。 相反, heapify方法是一种有助于操纵堆的方法(在本例中为MIN HEAP),它允许元素A [i]向下滚动堆,直到到达正确的位置。

我错了吗?

package priorityQueue;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.NoSuchElementException;


public class BinaryHeap 
{
protected static <P,E> Element<P,E> extractMinimum(ArrayList<Element<P, E>> priorityQueue, Comparator<Element<P,E>> priorityComparator, HashMap<E, Integer> mapping) throws NoSuchElementException 
{
    if (priorityQueue.size() == 0) 
        throw new NoSuchElementException();
    if (priorityQueue.size() == 1) 
        return priorityQueue.remove(0);

    Element<P,E> first = priorityQueue.get(0);
    mapping.remove(first.getElement());

    Element<P,E> newFirst = priorityQueue.get(priorityQueue.size()-1);
    mapping.remove(newFirst.getElement());

    priorityQueue.set(0, priorityQueue.remove(priorityQueue.size()-1));
    mapping.put(newFirst.getElement() , 0);
    heapify(priorityQueue, priorityComparator, mapping,0);
    return first;
}

protected static <P,E> void sort (ArrayList<Element<P, E>> priorityQueue, Comparator<Element<P,E>> priorityComparator, HashMap<E, Integer> mapping, int index) 
{
    int i = index;
    while(i > 0)
    {
        int middle = (i - 1)/2;
        Element<P,E> element = priorityQueue.get(i);
        Element<P,E> parent = priorityQueue.get(middle);
        if(priorityComparator.compare(element, parent)<0) 
        {
            mapping.replace(element.getElement(), middle);
            mapping.replace(parent.getElement(), i);
            priorityQueue.set(i, parent);
            priorityQueue.set(middle, element);
            i = middle;
        } 
        else 
            break;
    }
}

protected static <P,E> void heapify(ArrayList<Element<P, E>> priorityQueue, Comparator<Element<P,E>> priorityComparator, HashMap<E,Integer> map, int index) 
{
    int left = (2*index) + 1;
    while (left < priorityQueue.size()) 
    {
        int max=left;
        int right = left+1;
        if (right < priorityQueue.size()) 
        { // there is a right child
            if (priorityComparator.compare(priorityQueue.get(right), priorityQueue.get(left)) < 0) 
                max = max + 1;
        }
        if (priorityComparator.compare(priorityQueue.get(index), priorityQueue.get(max)) > 0) 
        {
            Element<P,E> temp = priorityQueue.get(index);
            swap(priorityQueue,map,temp,index,max,left);
        } 
        else 
            break;
    }
}

protected static <P,E> boolean insert(ArrayList<Element<P, E>> priorityQueue, Element<P, E> element, Comparator<Element<P,E>> priorityComparator, HashMap<E, Integer> mapping)
{
    if(mapping.containsKey(element.getElement()) == false) 
    {
        priorityQueue.add(element);
        mapping.put(element.getElement(), priorityQueue.size()-1);
        sort(priorityQueue, priorityComparator, mapping, priorityQueue.size()-1);
        return true;
    }
    else
        return false;
}

protected static <P,E> ArrayList<Element<P,E>> heapSort(ArrayList<Element<P,E>> priorityQueue, Comparator<Element<P,E>> priorityComparator, HashMap<E, Integer> mapping)
{
    ArrayList<Element<P,E>> minimumHeap = new ArrayList<>();
    while(priorityQueue.isEmpty() == false)
        minimumHeap.add(BinaryHeap.extractMinimum(priorityQueue, priorityComparator, mapping));
    return minimumHeap;
}

protected static <T,E> void changePriority(ArrayList<Element<T,E>> priorityQueue, Element<T, E> element, Element<T,E> newElementPriority, HashMap<E, Integer> map, Comparator<Element<T,E>> priorityComparator) throws NoSuchElementException
{
    if(map.containsKey(element.getElement()) == false)
        throw new NoSuchElementException();

    int index = map.get(element.getElement());
    boolean topDown = priorityComparator.compare(priorityQueue.get(index), newElementPriority) > 0;
    priorityQueue.get(index).setPriority(newElementPriority.getPriority());

    if(topDown) 
        sort(priorityQueue, priorityComparator, map, index);
    else 
        heapify(priorityQueue, priorityComparator, map, index);
}

private static<P,E> void swap(ArrayList<Element<P, E>> priorityQueue, HashMap<E,Integer> map,Element<P, E> temp, int index, int max, int left) 
{
    map.replace(priorityQueue.get(index).getElement(), max);
    map.replace(priorityQueue.get(max).getElement(), index);
    priorityQueue.set(index, priorityQueue.get(max));
    priorityQueue.set(max, temp);
    index = max;
    left = (2*index) + 1;
}
}

请帮助

1 个答案:

答案 0 :(得分:0)

通常使用sort方法称为heapifyUpbubbleUpsiftUp之类的方法。它与heapify的不同之处在于它使节点在树中向上移动。 heapify将节点向下移动到树上。其他人称为将节点向下移动sink的函数,而将节点向上移动的函数称为swim

无论您怎么说,它的工作方式都非常简单:

while node_index > 0
    if the node is smaller than its parent
      swap the node with its parent
      node_index = parent_index

添加项目时使用此方法。例如,给定此堆:

    2
 3     5
4 7   6

您想要将值1添加到堆中。因此,您将其放置在数组中最低,最左侧的位置。在这种情况下,作为5的右子:

    2
 3     5
4 7   6 1

然后,将新节点与其父节点进行比较。从1 <5开始,交换节点:

    2
 3     1
4 7   6 5

节点索引仍然不是0(根),因此您再次进行比较。 1 <2,所以您再次交换:

    1
 3     2
4 7   6 5

现在您又有了一个有效的堆。