|  | 
|  | 1 | +# Copyright 2013, Michael H. Goldwasser | 
|  | 2 | +# | 
|  | 3 | +# Developed for use with the book: | 
|  | 4 | +# | 
|  | 5 | +#    Data Structures and Algorithms in Python | 
|  | 6 | +#    Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser | 
|  | 7 | +#    John Wiley & Sons, 2013 | 
|  | 8 | +# | 
|  | 9 | +# This program is free software: you can redistribute it and/or modify | 
|  | 10 | +# it under the terms of the GNU General Public License as published by | 
|  | 11 | +# the Free Software Foundation, either version 3 of the License, or | 
|  | 12 | +# (at your option) any later version. | 
|  | 13 | +# | 
|  | 14 | +# This program is distributed in the hope that it will be useful, | 
|  | 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | +# GNU General Public License for more details. | 
|  | 18 | +# | 
|  | 19 | +# You should have received a copy of the GNU General Public License | 
|  | 20 | +# along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|  | 21 | + | 
|  | 22 | +from .priority_queue_base import PriorityQueueBase | 
|  | 23 | +from ..exceptions import Empty | 
|  | 24 | + | 
|  | 25 | +class HeapPriorityQueue(PriorityQueueBase): # base class defines _Item | 
|  | 26 | +  """A min-oriented priority queue implemented with a binary heap.""" | 
|  | 27 | + | 
|  | 28 | +  #------------------------------ nonpublic behaviors ------------------------------ | 
|  | 29 | +  def _parent(self, j): | 
|  | 30 | +    return (j-1) // 2 | 
|  | 31 | + | 
|  | 32 | +  def _left(self, j): | 
|  | 33 | +    return 2*j + 1 | 
|  | 34 | +   | 
|  | 35 | +  def _right(self, j): | 
|  | 36 | +    return 2*j + 2 | 
|  | 37 | + | 
|  | 38 | +  def _has_left(self, j): | 
|  | 39 | +    return self._left(j) < len(self._data)     # index beyond end of list? | 
|  | 40 | +   | 
|  | 41 | +  def _has_right(self, j): | 
|  | 42 | +    return self._right(j) < len(self._data)    # index beyond end of list? | 
|  | 43 | +   | 
|  | 44 | +  def _swap(self, i, j): | 
|  | 45 | +    """Swap the elements at indices i and j of array.""" | 
|  | 46 | +    self._data[i], self._data[j] = self._data[j], self._data[i] | 
|  | 47 | + | 
|  | 48 | +  def _upheap(self, j): | 
|  | 49 | +    parent = self._parent(j) | 
|  | 50 | +    if j > 0 and self._data[j] < self._data[parent]: | 
|  | 51 | +      self._swap(j, parent) | 
|  | 52 | +      self._upheap(parent)             # recur at position of parent | 
|  | 53 | +   | 
|  | 54 | +  def _downheap(self, j): | 
|  | 55 | +    if self._has_left(j): | 
|  | 56 | +      left = self._left(j) | 
|  | 57 | +      small_child = left               # although right may be smaller | 
|  | 58 | +      if self._has_right(j): | 
|  | 59 | +        right = self._right(j) | 
|  | 60 | +        if self._data[right] < self._data[left]: | 
|  | 61 | +          small_child = right | 
|  | 62 | +      if self._data[small_child] < self._data[j]: | 
|  | 63 | +        self._swap(j, small_child) | 
|  | 64 | +        self._downheap(small_child)    # recur at position of small child | 
|  | 65 | + | 
|  | 66 | +  #------------------------------ public behaviors ------------------------------ | 
|  | 67 | +  def __init__(self): | 
|  | 68 | +    """Create a new empty Priority Queue.""" | 
|  | 69 | +    self._data = [] | 
|  | 70 | + | 
|  | 71 | +  def __len__(self): | 
|  | 72 | +    """Return the number of items in the priority queue.""" | 
|  | 73 | +    return len(self._data) | 
|  | 74 | + | 
|  | 75 | +  def add(self, key, value): | 
|  | 76 | +    """Add a key-value pair to the priority queue.""" | 
|  | 77 | +    self._data.append(self._Item(key, value)) | 
|  | 78 | +    self._upheap(len(self._data) - 1)            # upheap newly added position | 
|  | 79 | +   | 
|  | 80 | +  def min(self): | 
|  | 81 | +    """Return but do not remove (k,v) tuple with minimum key. | 
|  | 82 | +
 | 
|  | 83 | +    Raise Empty exception if empty. | 
|  | 84 | +    """ | 
|  | 85 | +    if self.is_empty(): | 
|  | 86 | +      raise Empty('Priority queue is empty.') | 
|  | 87 | +    item = self._data[0] | 
|  | 88 | +    return (item._key, item._value) | 
|  | 89 | + | 
|  | 90 | +  def remove_min(self): | 
|  | 91 | +    """Remove and return (k,v) tuple with minimum key. | 
|  | 92 | +
 | 
|  | 93 | +    Raise Empty exception if empty. | 
|  | 94 | +    """ | 
|  | 95 | +    if self.is_empty(): | 
|  | 96 | +      raise Empty('Priority queue is empty.') | 
|  | 97 | +    self._swap(0, len(self._data) - 1)           # put minimum item at the end | 
|  | 98 | +    item = self._data.pop()                      # and remove it from the list; | 
|  | 99 | +    self._downheap(0)                            # then fix new root | 
|  | 100 | +    return (item._key, item._value) | 
0 commit comments