Chapter 11 · Simulation

Priority Queue Simulator

← Chapter 11

Service order

Heap array

IndexValuePriorityArrival #Parent

History

  1. No operations yet.

Priority Queue Simulator

A priority queue always serves the highest-priority item next, no matter when it arrived. This one is a max-heap ordered by priority; among equal priorities, the item that arrived first (the smaller arrival number) is served first.

Controls

  • Type a Value and a Priority (0–99) and press Enqueue, or use Add Random. The queue holds at most 10 items.
  • Dequeue removes the highest-priority item; Peek only shows it.
  • Change Priority gives the item at Heap index the New priority and repairs the heap by moving the item up or down.
  • Load Example brings back the sample queue, Clear empties it, and Cancel stops the running operation and puts the queue back the way it was.

Reading the screen

  • Service order: the logical order in which items will leave the queue.
  • The tree and the Heap array hold the same data: the parent of the item at index i is at index ⌊(i − 1) / 2⌋.
  • History lists the most recent operations.

Complexity

OperationTime
peekO(1)
enqueue, dequeue, change priorityO(log n)