© 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  
(1)
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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.