PriorityQueue<TElement, TPriority> · C#
Peek / TryPeek
Returns the minimum element without removing it.
public TElement Peek(); public bool TryPeek(out TElement element, out TPriority priority)
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(1) |
The heap invariant keeps the minimum at index 0.
Tested example
var queue = new PriorityQueue<string, int>();
Assert.False(queue.TryPeek(out _, out _));
queue.Enqueue("only", 9);
Assert.Equal("only", queue.Peek()); // no removal
Assert.Equal(1, queue.Count);
Behaviour
- Returns
- The element with the smallest priority.
- Errors and preconditions
- Peek: InvalidOperationException if empty.
- Changes
- Nothing.
- Memory and invalidation
- No allocation.