-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueue.java
More file actions
219 lines (182 loc) · 7.25 KB
/
Copy pathpriorityQueue.java
File metadata and controls
219 lines (182 loc) · 7.25 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class priorityQueue {
// MIN HEAP - The parent is smaller than the children.
public static void main(String args[]) throws PriorityQueueException {
priorityQueue pq = new priorityQueue();
pq.insert(52);
pq.insert(8);
pq.insert(6);
pq.insert(29);
pq.insert(36);
pq.insert(21);
pq.insert(24);
pq.insert(18);
pq.insert(42);
pq.insert(2);
List<Integer> allValues = Arrays.asList(52, 8, 6, 29, 36, 21, 24, 18, 42, 2);
ArrayList<Integer> listy = pq.inPlaceHeapSort(new ArrayList<Integer>(allValues));
System.out.println(listy.toString());
// ArrayList<Integer> aList = pq.heapSort();
// System.out.println(aList.toString());
}
private static ArrayList<Integer> heap;
public priorityQueue() {
heap = new ArrayList<Integer>();
}
public boolean isEmpty() {
return heap.isEmpty();
}
public int size() {
return heap.size();
}
public int getMin() throws PriorityQueueException {
if (heap.size() == 0)
throw new PriorityQueueException();
return heap.get(0);
}
// The node will be inserted as a leaf node from the left side, maintaining the
// CBT - Complete Binary Tree property.
public void insert(int data) {
// This is a min heap, that is the smallest element will be on the first
// position.
heap.add(data);
int childIndex = heap.indexOf(data);
int parentIndex = (childIndex - 1) / 2;
while (childIndex > 0) {
if (heap.get(childIndex) < heap.get(parentIndex)) {
// swap
int temp = heap.get(childIndex);
heap.set(childIndex, heap.get(parentIndex));
heap.set(parentIndex, temp);
childIndex = parentIndex;
parentIndex = (childIndex - 1) / 2;
} else {
return;
}
}
}
public int removeMin() throws PriorityQueueException {
if (heap.isEmpty()) {
throw new PriorityQueueException();
}
int rem = heap.get(0);
try {
heap.set(0, heap.get(heap.size() - 1));
heap.remove(heap.size() - 1);
int parent = 0;
int leftChildIndex = 1;
int rightChildIndex = 2;
int minIndex = 0;
if (rightChildIndex < heap.size() - 1 && leftChildIndex < heap.size() - 1) {
minIndex = (heap.get(rightChildIndex) > heap.get(leftChildIndex)) ? leftChildIndex
: rightChildIndex;
} else if (rightChildIndex < heap.size() - 1) {
minIndex = rightChildIndex;
} else if (leftChildIndex < heap.size() - 1) {
minIndex = leftChildIndex;
}
while (minIndex < heap.size() - 1 && heap.get(parent) > heap.get(minIndex)) {
int temp = heap.get(parent);
heap.set(parent, heap.get(minIndex));
heap.set(minIndex, temp);
parent = minIndex;
leftChildIndex = 2 * parent + 1;
rightChildIndex = 2 * parent + 2;
if (rightChildIndex < heap.size() - 1 && leftChildIndex < heap.size() - 1) {
minIndex = (heap.get(rightChildIndex) > heap.get(leftChildIndex)) ? leftChildIndex
: rightChildIndex;
} else if (rightChildIndex < heap.size() - 1) {
minIndex = rightChildIndex;
} else if (leftChildIndex < heap.size() - 1) {
minIndex = leftChildIndex;
}
}
} catch (Exception e) {
System.out.println(e);
}
return rem;
}
public ArrayList<Integer> heapSort() throws PriorityQueueException {
// The space complexity of Heap Sort is O(n)
// We have to reduce it.
ArrayList<Integer> alist = new ArrayList<Integer>();
while (!heap.isEmpty()) {
alist.add(removeMin());
}
return alist;
}
/**
* @param alist
* @return
*/
public ArrayList<Integer> inPlaceHeapSort(ArrayList<Integer> alist) {
/*
* First we will have to create a heap out of this list
* How is a heap created? - Maintaining CBT and heap property.
* CBT is already achieved. We have to add elements one by one.
*/
int index = 0, parentIndex = 0, leftChild = 0, rightChild = 0;
ArrayList<Integer> newHeap = new ArrayList<Integer>();
for (Integer element : alist) {
newHeap.add(element);
index = newHeap.size() - 1;
parentIndex = (index - 1) / 2;
while (parentIndex >= 0 && newHeap.get(parentIndex) > newHeap.get(index)) {
int temp = newHeap.get(parentIndex);
newHeap.set(parentIndex, newHeap.get(index));
newHeap.set(index, temp);
index = parentIndex;
parentIndex = (index - 1) / 2;
}
}
/*
* We have transformed the newHeap into a min heap. Now we have to
* remove elements one-by-one while maintaining the min heap property.
* Since this is an inplace version, the element removed from the top,
* will be placed at the end. So we have to keep where the heap is ending
* after every removal. :)
*/
int lastIndex = newHeap.size() - 1;
while (lastIndex >= 0) {
int current = 0;
int removedElement = newHeap.get(current);
newHeap.set(current, newHeap.get(lastIndex));
newHeap.set(lastIndex, removedElement);
leftChild = 1;
rightChild = 2;
int minIndex;
if (leftChild < lastIndex && rightChild < lastIndex) {
minIndex = (newHeap.get(leftChild) > newHeap.get(rightChild)) ? rightChild : leftChild;
} else if (leftChild <= lastIndex) {
minIndex = leftChild;
} else {
minIndex = newHeap.size();
}
lastIndex--;
while (minIndex < newHeap.size()) {
if (newHeap.get(current) > newHeap.get(leftChild) || newHeap.get(current) > newHeap.get(rightChild)) {
int temp = newHeap.get(minIndex);
newHeap.set(minIndex, newHeap.get(current));
newHeap.set(current, temp);
current = minIndex;
leftChild = 2 * current + 1;
rightChild = 2 * current + 2;
if (leftChild < lastIndex && rightChild < lastIndex) {
minIndex = (newHeap.get(leftChild) > newHeap.get(rightChild)) ? rightChild : leftChild;
} else if (leftChild <= lastIndex) {
minIndex = leftChild;
} else {
minIndex = newHeap.size();
}
} else {
break;
}
}
}
Collections.reverse(newHeap);
return newHeap;
}
}