-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.java
More file actions
157 lines (131 loc) · 5.12 KB
/
Copy pathMinHeap.java
File metadata and controls
157 lines (131 loc) · 5.12 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
// Min Heap data structure.
public class MinHeap {
// Heap array containing Node objects.
private HeapNode[] heap;
// Max capacity for the heap array.
private int capacity;
// Number of currently inserted items into the heap array.
private int size;
// Index of the top-most item of the heap.
private static final int TOP = 1;
// Constructor.
public MinHeap(int capacity) {
this.capacity = capacity;
initHeap(capacity);
}
// Initialize heap array with the given capacity.
private void initHeap(int capacity) {
this.heap = new HeapNode[capacity + 1];
this.heap[0] = null;
size++;
}
// Return true if the heap is empty, false otherwise.
public boolean isEmpty() {
return size == 1;
}
// Returns true if the given position has parent, false otherwise.
private boolean hasParent(int position) {
return position != 1;
}
// Returns true if the item at the given position is a leaf node, false otherwise.
private boolean isLeaf(int position) {
return (position >= size/2 && position <= size);
}
// Returns the index of the parent of the given position.
private int getParentPosition(int position) {
return (position / 2);
}
// Returns the index of the left child of the given position.
private int getLeftChildPosition(int position) {
return (position * 2);
}
// Returns the index of the right child of the given position.
private int getRightChildPosition(int position) {
return (position * 2) + 1;
}
// Swap two items in the heap array.
private void swap(int i, int j) {
HeapNode temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
// Inserts node into heap array.
public void insert(HeapNode node) {
if(size > capacity) {
throw new IllegalStateException("Error: Capacity Exceeded. Cannot insert more than " + capacity + " items.");
}
heap[size] = node;
heapifyUp(size);
}
// Rearranges the heap array as per heap property whenever a new item is inserted.
private void heapifyUp(int position) {
while(hasParent(position) &&
heap[position].getrideCost() <= heap[getParentPosition(position)].getrideCost()) {
if(heap[position].getrideCost() < heap[getParentPosition(position)].getrideCost() ||
heap[position].gettripDuration() < heap[getParentPosition(position)].gettripDuration()) {
swap(position, getParentPosition(position));
position = getParentPosition(position);
}
else {
break;
}
}
size++;
}
// Removes and returns the minimum item from the heap.
public HeapNode extractMin() {
if(isEmpty()) {
return null;
}
HeapNode item = heap[TOP];
// Copies the last heap item at top, and then heapify it down.
heap[TOP] = heap[size - 1];
heapifyDown(TOP);
heap[size - 1] = null;
size--;
return item;
}
// finds the heap index that contains given rideNumber
private int find(int rideNumber){
for(int i=1; i <= size; i++ ){
if(heap[i].getrideNumber() == rideNumber)
return i;
}
return -1;
}
// deletes the heap node with given rideNumber
public RedBlackNode delete(int rideNumber){
int index = find(rideNumber);
if(index ==-1 )
return null ;
RedBlackNode rbnode = heap[index].getRbtReference();
heap[index] = heap[size-1];
size--;
heapifyDown(index);
return rbnode;
}
// Set the node at given position to it's appropriate index by recursively performing heapify operation.
private void heapifyDown(int position) {
// Break the recursion when the item is leaf node.
if(isLeaf(position)) return;
// Left and Right child indexes.
int left = getLeftChildPosition(position);
int right = getRightChildPosition(position);
// Check which child has smaller value. Tie is broken by comparing building number if executed time is same.
boolean isLeftSmaller = (heap[left].getrideCost() != heap[right].getrideCost()) ?
heap[left].getrideCost() < heap[right].getrideCost() :
heap[left].gettripDuration() < heap[right].gettripDuration();
if(isLeftSmaller && heap[position].getrideCost() >= heap[left].getrideCost() &&
(heap[position].getrideCost() > heap[left].getrideCost() ||
heap[position].gettripDuration() > heap[left].gettripDuration())) {
swap(position, left);
heapifyDown(left);
}
else if(heap[position].getrideCost() >= heap[right].getrideCost() &&
(heap[position].getrideCost() > heap[right].getrideCost() ||
heap[position].gettripDuration() > heap[right].gettripDuration())) {
swap(position, right);
heapifyDown(right);
}
}
}