1 /***********************************************************************
2 * Software License Agreement (BSD License)
4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *************************************************************************/
42 * Priority Queue Implementation
44 * The priority queue is implemented with a heap. A heap is a complete
45 * (full) binary tree in which each parent is less than both of its
46 * children, but the order of the children is unspecified.
47 * Note that a heap uses 1-based indexing to allow for power-of-2
48 * location of parents and children. We ignore element 0 of Heap array.
54 * Storage array for the heap.
55 * Type T must be comparable.
61 * Number of element in the heap
78 heap = new T[length]; // heap uses 1-based indexing
102 * Tests if the heap is empty
104 * Returns: true is heap empty, false otherwise
121 * Insert a new element in the heap.
123 * We select the next empty leaf node, and then keep moving any larger
124 * parents down until the right location is found to store this element.
127 * value = the new element to be inserted in the heap
131 /* If heap is full, then return without adding this element. */
132 if (count == length-1) {
136 int loc = ++(count); /* Remember 1-based indexing. */
138 /* Keep moving parents down until a place is found for this node. */
139 int par = loc / 2; /* Location of parent. */
140 while (par > 0 && value < heap[par]) {
141 heap[loc] = heap[par]; /* Move parent down to loc. */
145 /* Insert the element at the determined location. */
152 * Returns the node of minimum value from the heap (top of the heap).
155 * value = out parameter used to return the min element
156 * Returns: false if heap empty
158 bool popMin(T& value)
164 /* Switch first node with last. */
165 swap(heap[1],heap[count]);
168 heapify(1); /* Move new node 1 to right position. */
170 value = heap[count + 1];
171 return true; /* Return old last node. */
176 * Reorganizes the heap (a parent is smaller than its children)
177 * starting with a node.
180 * parent = node form which to start heap reorganization.
182 void heapify(int parent)
186 /* Check the left child */
187 int left = 2 * parent;
188 if (left <= count && heap[left] < heap[parent]) {
192 /* Check the right child */
193 int right = left + 1;
194 if (right <= count && heap[right] < heap[minloc]) {
198 /* If a child was smaller, than swap parent with it and Heapify. */
199 if (minloc != parent) {
200 swap(heap[parent],heap[minloc]);