© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
T. MailundIntroduction to Computational Thinkinghttps://doi.org/10.1007/978-1-4842-7077-6_17

17. Priority Queues

Thomas Mailund1  
Aarhus N, Denmark
A priority queue is an abstract data structure that provides the following operations:
  • You can add elements to it.

  • We can test if it is empty.

  • You can get the smallest element in the heap.

  • You can remove the smallest element in the heap.

The operations are not hard to implement. Here is a version that uses Python’s list class :
class PriorityQueue(object):
    def __init__(self, seq = ()):
        self.list = list(seq)
    def is_empty(self):
        return len(self.list) == 0
    def __bool__(self):
        return not ...

Get Introduction to Computational Thinking: Problem Solving, Algorithms, Data Structures, and More now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.