forked from chintan-27/RedBlackTree-MinHeap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minHeap.h
131 lines (122 loc) · 3.42 KB
/
minHeap.h
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
// {
// CREATED BY: CHINTAN ACHARYA
// DATE: 6 APRIL 2023
// }
#include <vector>
using namespace std;
// STORES THE DATA OF THE HEAP NODE
class MinHeapNode
{
public:
int rideNumber;
int rideCost;
int tripDuration;
MinHeapNode(int rn, int rc, int td) : rideNumber(rn), rideCost(rc), tripDuration(td) {}
};
class MinHeap
{
// DEFINING THE BASIC HEAP FUNCTIONS
private:
vector<MinHeapNode> heap;
int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int parent(int i) { return (i - 1) / 2; }
void swap(MinHeapNode &a, MinHeapNode &b)
{
MinHeapNode temp = a;
a = b;
b = temp;
}
public:
// INSERTS INTO HEAP
void insert(MinHeapNode &node)
{
heap.push_back(node);
int i = heap.size() - 1;
while (i > 0 && heap[i].rideCost < heap[parent(i)].rideCost ||
(heap[i].rideCost == heap[parent(i)].rideCost && heap[i].tripDuration < heap[parent(i)].tripDuration))
{
swap(heap[i], heap[parent(i)]);
i = parent(i);
}
}
// EXTRACT MIN FROM THE HEAP
MinHeapNode extractMin()
{
MinHeapNode root = heap[0];
heap[0] = heap.back();
heap.pop_back();
int i = 0;
while (left(i) < heap.size())
{
int j = left(i);
if (right(i) < heap.size() &&
(heap[right(i)].rideCost < heap[j].rideCost ||
(heap[right(i)].rideCost == heap[j].rideCost && heap[right(i)].tripDuration < heap[j].tripDuration)))
{
j = right(i);
}
if (heap[i].rideCost < heap[j].rideCost ||
(heap[i].rideCost == heap[j].rideCost && heap[i].tripDuration <= heap[j].tripDuration))
{
break;
}
swap(heap[i], heap[j]);
i = j;
}
return root;
}
// RETURNS THE MIN NODE
MinHeapNode getMin()
{
return heap[0];
}
// RETURNS IS EMPTY
bool isEmpty()
{
return heap.size() == 0;
}
// DELETE HEAP NODE BY RIDENUMBER
void deleteHeapNode(int rideNumber)
{
int index = -1;
for (int i = 0; i < heap.size(); i++)
{
if (heap[i].rideNumber == rideNumber)
{
index = i;
break;
}
}
if (index == -1)
{
return;
}
heap[index] = heap.back();
heap.pop_back();
int i = index;
while (i > 0 && heap[i].rideCost < heap[parent(i)].rideCost ||
(heap[i].rideCost == heap[parent(i)].rideCost && heap[i].tripDuration < heap[parent(i)].tripDuration))
{
swap(heap[i], heap[parent(i)]);
i = parent(i);
}
while (left(i) < heap.size())
{
int j = left(i);
if (right(i) < heap.size() &&
(heap[right(i)].rideCost < heap[j].rideCost ||
(heap[right(i)].rideCost == heap[j].rideCost && heap[right(i)].tripDuration < heap[j].tripDuration)))
{
j = right(i);
}
if (heap[i].rideCost < heap[j].rideCost ||
(heap[i].rideCost == heap[j].rideCost && heap[i].tripDuration <= heap[j].tripDuration))
{
break;
}
swap(heap[i], heap[j]);
i = j;
}
}
};