-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityque.cs
More file actions
166 lines (165 loc) · 4.93 KB
/
priorityque.cs
File metadata and controls
166 lines (165 loc) · 4.93 KB
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
using System;
namespace DiGraph
{
public class PriorityQueue<T>
{
class Node
{
public T data;
public int priority;
public static bool operator <(Node n1, Node n2)
{
return (n1.priority < n2.priority); } public static bool operator >(Node n1, Node n2)
{
return (n1.priority > n2.priority);
}
public static bool operator <=(Node n1, Node n2)
{
return (n1.priority <= n2.priority); } public static bool operator >=(Node n1, Node n2)
{
return (n1.priority >= n2.priority);
}
public static bool operator ==(Node n1, Node n2)
{
return (n1.priority == n2.priority && (dynamic)n1.data == (dynamic)n2.data);
}
public static bool operator !=(Node n1, Node n2)
{
return (n1.priority != n2.priority && (dynamic)n1.data != (dynamic)n2.data);
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
Node[] arr;
private int count;
private const int arrSize = 100;
public PriorityQueue()
{
arr = new Node[arrSize];
count = 0;
}
public void Enqueue(T nData, int priority)
{
Node nObj = new Node();
nObj.data = nData;
nObj.priority = priority;
if (count == arr.Length)
{
throw new Exception("Priority Queue is at full capacity");
}
else
{
arr[count] = nObj;
count++;
siftUp(count - 1);
}
}
private void siftUp(int index)
{
int parentIndex;
Node temp;
if (index != 0)
{
parentIndex = getParentIndex(index);
if (arr[parentIndex] > arr[index])
{
temp = arr[parentIndex];
arr[parentIndex] = arr[index];
arr[index] = temp;
siftUp(parentIndex);
}
}
}
private int getParentIndex(int index)
{
return (index - 1) / 2;
}
public void decrease_priority(T target, int newPriority)
{
//find the target first
Node n = new Node();
n.data = target;
for (int i = 0; i < arr.Length; i++) { if (arr[i] == n) { if (newPriority > arr[i].priority)
{
throw new Exception("New priority assignment must be less than current priority");
}
else
{
arr[i].priority = newPriority;
siftUp(i);
}
break;
}
}
}
public T dequeue_min()
{
Node nObj;
if (count == 0)
{
throw new Exception("Priority Queue is empty");
}
else
{
nObj = arr[0];
arr[0] = arr[count - 1];
count--;
if (count > 0)
{
siftDown(0);
}
}
arr[count] = null;//set the last index to null after a dequeue min
return nObj.data;
}
public void siftDown(int nodeIndex)
{
int leftChildIndex, rightChildIndex, minIndex;
Node temp;
leftChildIndex = getLeftChildIndex(nodeIndex);
rightChildIndex = getRightChildIndex(nodeIndex);
if (rightChildIndex >= count)
{
if (leftChildIndex >= count)
{
return;
}
else
{
minIndex = leftChildIndex;
}
}
else
{
if (arr[leftChildIndex] <= arr[rightChildIndex]) { minIndex = leftChildIndex; } else { minIndex = rightChildIndex; } } if (arr[nodeIndex] > arr[minIndex])
{
temp = arr[minIndex];
arr[minIndex] = arr[nodeIndex];
arr[nodeIndex] = temp;
siftDown(minIndex);
}
}
private int getLeftChildIndex(int index)
{
return (2 * index) + 1;
}
private int getRightChildIndex(int index)
{
return (2 * index) + 2;
}
public T peek()
{
return arr[0].data;
}
public bool empty()
{
return (count == 0);
}
}
}