-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
184 lines (159 loc) · 4.42 KB
/
PriorityQueue.cpp
File metadata and controls
184 lines (159 loc) · 4.42 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <cstring>
#include <stdexcept>
using namespace std;
class road_network
{
private:
// Road representation with dynamic memory management
class road
{
public:
char *start;
char *end;
int priority;
road(const char *s, const char *e, int p) : priority(p)
{
start = new char[strlen(s) + 1];
end = new char[strlen(e) + 1];
strcpy(start, s);
strcpy(end, e);
}
road() {}
~road()
{
delete[] start;
delete[] end;
}
// Copy constructor
road(const road &other) : priority(other.priority)
{
start = new char[strlen(other.start) + 1];
end = new char[strlen(other.end) + 1];
strcpy(start, other.start);
strcpy(end, other.end);
}
// Assignment operator
road &operator=(const road &other)
{
if (this != &other)
{
delete[] start;
delete[] end;
priority = other.priority;
start = new char[strlen(other.start) + 1];
end = new char[strlen(other.end) + 1];
strcpy(start, other.start);
strcpy(end, other.end);
}
return *this;
}
};
// Dynamic Priority Queue with heap implementation
class priority_queue
{
private:
road *heap;
int capacity;
int size;
void resize()
{
int new_capacity = capacity == 0 ? 1 : capacity * 2;
road *new_heap = new road[new_capacity];
for (int i = 0; i < size; ++i)
{
new_heap[i] = heap[i];
}
delete[] heap;
heap = new_heap;
capacity = new_capacity;
}
void swap(road &a, road &b)
{
road temp = a;
a = b;
b = temp;
}
void heapify_up(int index)
{
while (index > 0)
{
int parent = (index - 1) / 2;
if (heap[index].priority > heap[parent].priority)
{
swap(heap[index], heap[parent]);
index = parent;
}
else
{
break;
}
}
}
void heapify_down(int index)
{
while (true)
{
int left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;
if (left < size && heap[left].priority > heap[largest].priority)
largest = left;
if (right < size && heap[right].priority > heap[largest].priority)
largest = right;
if (largest != index)
{
swap(heap[index], heap[largest]);
index = largest;
}
else
{
break;
}
}
}
public:
priority_queue() : heap(nullptr), capacity(0), size(0) {}
~priority_queue()
{
delete[] heap;
}
void push(const char *start, const char *end, int priority)
{
if (size == capacity)
{
resize();
}
heap[size] = road(start, end, priority);
heapify_up(size);
size++;
}
road pop()
{
if (size == 0)
{
throw runtime_error("Priority queue is empty");
}
road top = heap[0];
heap[0] = heap[size - 1];
size--;
if (size > 0)
{
heapify_down(0);
}
return top;
}
bool is_empty() const
{
return size == 0;
}
void print() const
{
for (int i = 0; i < size; ++i)
{
cout << "Road: " << heap[i].start << " -> " << heap[i].end
<< ", Priority: " << heap[i].priority << "\n";
}
}
};
};