-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cs
More file actions
153 lines (134 loc) · 3.94 KB
/
PriorityQueue.cs
File metadata and controls
153 lines (134 loc) · 3.94 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollisionSimulation
{
public class PriorityQueue //heap starts @ index of 1
{
private Event[] pq = null;
private int numItems = 0;
public PriorityQueue(Event[] pq)//creates pq from given array
{
numItems = pq.Length;
this.pq = new Event[pq.Length + 1];
for (int i = 0; i < numItems; i++)
{
this.pq[i + 1] = pq[i];//shifts over array indicies 1 higher; pq starts @ index 1
}
for (int i = numItems / 2; i >= 1; i--)//creates heap bottom up
{
sink(i);
}
}
public PriorityQueue(int pqSize)//creates empty pq with given initial capacity
{
this.pq = new Event[pqSize];
}
public PriorityQueue()//creates empty pq of size 100
{
this.pq = new Event[100];
}
public Event PQMin()
{
if (numItems == 0)
{
throw new IndexOutOfRangeException("Priority queue is empty");
}
else
{
return pq[1];
}
}
private void resize(int capacity)//resizes array
{
if (capacity > numItems)
{
Event[] temp = new Event[capacity];
for (int i = 0; i <= numItems; i++)
{
temp[i] = pq[i];
}
pq = temp;
}
else
{
throw new Exception("Capacity argument < current number of items; cannot resize");
}
}
public void insertEvent(Event e)
{
if (numItems == pq.Length - 1)
{
resize((pq.Length) * 2);
}
numItems++;
pq[numItems] = e; //increments then does operation
swim(numItems);
}
public Event delMin()
{
if (numItems == 0)
{
throw new IndexOutOfRangeException("Priority queue is empty");
}
else
{
Event min = pq[1];
swap(1, numItems--); //does operation, then decrements
sink(1);
pq[numItems + 1] = null; //derefrences min
if (numItems > 0 && (pq.Length - 1) / 4 == numItems)
{
resize(numItems * 2); //halves array if numItems is 1/4 of array size
}
return min;
}
}
private void swim(int index) //pq value moving up pq
{
while (index > 1 && (pq[index].time) < (pq[(index / 2)].time))
{
swap(index, index / 2);
index = index / 2;
}
}
private void sink(int index) //pq value moving down pq
{
while (index * 2 <= numItems)
{
int childIndex = index * 2;
if (childIndex < numItems && pq[childIndex].time > pq[childIndex + 1].time) //finds which is the smaller child node
{
childIndex++;
}
if (pq[index].time < pq[childIndex].time)
{
break;
}
swap(index, childIndex);
index = childIndex;
}
}
public void swap(int a, int b)
{
Event temp = pq[a];
pq[a] = pq[b];
pq[b] = temp;
}
public int getNumItems()
{
return numItems;
}
public int getArraySize()
{
return pq.Length;
}
public Event[] getPQCopy()
{
Event[] temp = pq;
return temp;
}
}
}