Priority Queue in C#
C#
doesn’t have a default PriorityQueue
implementation as part of the .NET Base Class Libary (BCL), so I started a collection of
some data structure implementations, which include several Priority Queue classes in this repository.
The official tracking issue for adding a PriorityQueue
implementation to .NET Core can be found here.
Code Example
In Java
, the default PriorityQueue orders the values according to their natural ordering
, which would be ascending order for integers.
You can find more information about it from the PriorityQueue documentation.
You can create a simple max-heap with a priority queue using:
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
The equivalent implementation using one of the classes from the above repository collection in C#
would be:
var queue = new Trill.PriorityQueue<int>(Comparer<int>.Create((a, b) => b.CompareTo(a));