In computer science, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.
- stack — elements are pulled in last-in first-out-order (e.g. a stack of papers)
- queue — elements are pulled in first-in first-out-order (e.g. a line in a cafeteria)
It is a common misconception that a priority queue is a heap. A priority queue is an abstract concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods.
A priority queue must at least support the following operations:
insert_with_priority
: add an element to the queue with an associated prioritypull_highest_priority_element
: remove the element from the queue that has the highest priority, and return it (also known as "pop_element(Off)", "get_maximum_element", or "get_front(most)_element"; some conventions consider lower priorities to be higher, so this may also be known as "get_minimum_element", and is often referred to as "get-min" in the literature; the literature also sometimes implement separate "peek_at_highest_priority_element" and "delete_element" functions, which can be combined to produce "pull_highest_priority_element")
More advanced implementations may support more complicated operations, such as pull_lowest_priority_element, inspecting the first few highest- or lowest-priority elements (peeking at the highest priority element can be made O(1) time in nearly all implementations), clearing the queue, clearing subsets of the queue, performing a batch insert, merging two or more queues into one, incrementing priority of any element, etc.
Read more about Priority Queue: Similarity To Queues, Libraries
Famous quotes containing the words priority and/or queue:
“It can be fairly argued that the highest priority for mankind is to save itself from extinction. However, it can also be argued that a society that neglects its children and robs them of their human potential can extinguish itself without an external enemy.”
—Selma Fraiberg (20th century)
“English people apparently queue up as a sort of hobby. A family man might pass a mild autumn evening by taking the wife and kids to stand in the cinema queue for a while and then leading them over for a few minutes in the sweetshop queue and then, as a special treat for the kids, saying Perhaps weve time to have a look at the Number Thirty-One bus queue before we turn in.”
—Calvin Trillin (b. 1940)