forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.cs
219 lines (183 loc) · 6.17 KB
/
PriorityQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
namespace DataStructures.Heaps
{
/// <summary>
/// Implements the Priority Queue Data Structure.
/// <typeparam name="V">Node's Value type</typeparam>
/// <typeparam name="P">Node's Priority type</typeparam>
/// </summary>
public class PriorityQueue<V, P> where P : IComparable<P>
{
/// <summary>
/// Instance variables
/// </summary>
private BinaryMaxHeap<PriorityQueueNode<V, P>> _heap { get; set; }
private Comparer<PriorityQueueNode<V, P>> _priorityComparer { get; set; }
/// <summary>
/// CONSTRUCTOR
/// </summary>
public PriorityQueue() : this(0, null) { }
/// <summary>
/// CONSTRUCTOR
/// </summary>
/// <param name="capacity">Capacity of priority queue.</param>
public PriorityQueue(int capacity) : this(capacity, null) { }
/// <summary>
/// CONSTRUCTOR
/// </summary>
/// <param name="capacity">Capacity of priority queue.</param>
/// <param name="priorityComparer">The node's priority comparer.</param>
public PriorityQueue(int capacity, Comparer<PriorityQueueNode<V, P>> priorityComparer)
{
if (capacity >= 0)
{
if (priorityComparer == null)
{
_priorityComparer = Comparer<PriorityQueueNode<V, P>>.Default;
}
else
{
_priorityComparer = priorityComparer;
}
_heap = new BinaryMaxHeap<PriorityQueueNode<V, P>>(capacity, this._priorityComparer);
}
else
{
throw new ArgumentOutOfRangeException("Please provide a valid capacity.");
}
}
/// <summary>
/// Returns the count of elements in the queue.
/// </summary>
public int Count()
{
return _heap.Count();
}
/// <summary>
/// Checks if the queue is empty
/// <returns>True if queue is empty; false otherwise.</returns>
/// </summary>
public bool IsEmpty()
{
return _heap.IsEmpty();
}
/// <summary>
/// Returns the highest priority element.
/// </summary>
/// <returns>The at highest priority.</returns>
public V PeekAtHighestPriority()
{
if (_heap.IsEmpty())
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
return _heap.Peek().Value;
}
/// <summary>
/// Enqueue the specified value without priority.
/// </summary>
/// <param name="value">Value.</param>
public void Enqueue(V value)
{
Enqueue(value, default(P));
}
/// <summary>
/// Enqueue the specified key, value and priority.
/// </summary>
/// <param name="value">Value.</param>
/// <param name="priority">Priority.</param>
public void Enqueue(V value, P priority)
{
var newNode = new PriorityQueueNode<V, P>(value, priority);
_heap.Add(newNode);
}
/// <summary>
/// Dequeue this instance.
/// </summary>
public V Dequeue()
{
if (_heap.IsEmpty())
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
return _heap.ExtractMax().Value;
}
/// <summary>
/// Sets the priority.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="newPriority">New priority.</param>
public void SetPriority(int index, P newPriority)
{
// Handle boundaries errors
if (_heap.IsEmpty())
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
else if (index < 0 || index >= _heap.Count())
{
throw new ArgumentOutOfRangeException("Please provide a valid index.");
}
_heap[index].Priority = newPriority;
}
/// <summary>
/// Removes the node that has the specified value.
/// </summary>
/// <param name="value">Value.</param>
//public void Remove(V value)
//{
// if (_heap.IsEmpty)
// {
// throw new ArgumentOutOfRangeException ("Queue is empty.");
// }
//
// var valueComparer = Comparer<V>.Default;
//
// Predicate<PriorityQueueNode<V, P>> match =
// new Predicate<PriorityQueueNode<V, P>> (
// item => valueComparer.Compare(item.Value, value) == 0);
//
// _heap.RemoveAll (match);
//}
/// <summary>
/// Clear this priority queue.
/// </summary>
public void Clear()
{
_heap.Clear();
}
}
/// <summary>
/// The Priority-queue node.
/// </summary>
/// <typeparam name="K">Node's Key type</typeparam>
/// <typeparam name="V">Node's Value type</typeparam>
public class PriorityQueueNode<V, P> : IComparable<PriorityQueueNode<V, P>> where P : IComparable<P>
{
public V Value { get; set; }
public P Priority { get; set; }
public PriorityQueueNode() : this(default(V), default(P)) { }
public PriorityQueueNode(V value, P priority)
{
this.Value = value;
this.Priority = priority;
}
public int CompareTo(PriorityQueueNode<V, P> other)
{
if (other == null)
return -1;
return this.Priority.CompareTo(other.Priority);
}
}//end-of-node-class
/// <summary>
/// Priority-queue node comparer.
/// </summary>
public class PriorityQueueNodeComparer<V, P> : IComparer<PriorityQueueNode<V, P>> where P : IComparable<P>
{
public int Compare(PriorityQueueNode<V, P> first, PriorityQueueNode<V, P> second)
{
return first.Priority.CompareTo(second.Priority);
}
}//end-of-comparer-class
}