Before attempting to understand the difference between a heap and a priority queue, it is important to distinguish between an abstract data type and a data structure. So what is the difference.
Abstract data type (ADT)
This is a theoretical concept that describes a data type, including how the data can look and what operations are associated with it. This definition is independent of its implementation.
Data Structure (DS)
A data structure is a concrete implementation of an abstract data type (ADT).
In simple terms, an abstract data type (ADT) is a blueprint or idea, while a data structure (DS) is the concrete implementation of that blueprint. Here is a list of how an ADT can be implemented through a data structure:
Stacks, queues, and heaps are examples of abstract data types. They are simply ideas, or "black boxes," with a defined behavior. To implement them, To implement abstract data types, you must select suitable concrete data structures. And there can be more than one ways to implement an ADT using a DS
Abstract Data Type | Example Data Structure Implementation |
---|---|
Stack | Array, Linked List |
Queue | Array, Linked List |
Priority Queue | Heap |
Set | Hash Table, Binary Search Tree |
Map | Hash Table, Binary Search Tree |
A queue is an abstract data structure that is open at both ends. One end is used to insert data (enqueue), while the other is used to remove data (dequeue). The queue operates on the principle of "first-in, first-out" (FIFO), similar to a queue at a supermarket.
Insert order: 8, 2, 7, 3 Out order: 8, 2, 7, 3 The out order is similar to the insertion order.
Here is an implementation of a queue using lists in Python. This can also be implemented using linked lists:
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)