-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySiftDownMain.java
More file actions
283 lines (268 loc) · 10.5 KB
/
ArraySiftDownMain.java
File metadata and controls
283 lines (268 loc) · 10.5 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import java.util.Arrays;
import components.map.Map;
import components.map.Map1L;
import components.random.Random;
import components.random.Random1L;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
/**
* Program to test {@code siftDown} on int array.
*
* @mathdefinitions <pre>
* SUBTREE_IS_HEAP (
* a: string of integer,
* start: integer,
* stop: integer,
* r: binary relation on T
* ) : boolean is
* [the subtree of a (when a is interpreted as a complete binary tree) rooted
* at index start and only through entry stop of a satisfies the heap
* ordering property according to the relation r]
*
* SUBTREE_ARRAY_ENTRIES (
* a: string of integer,
* start: integer,
* stop: integer
* ) : finite multiset of T is
* [the multiset of entries in a that belong to the subtree of a
* (when a is interpreted as a complete binary tree) rooted at
* index start and only through entry stop]
* </pre>
*
* @author Put your name here
*
*/
public final class ArraySiftDownMain {
/**
* Private constructor so this utility class cannot be instantiated.
*/
private ArraySiftDownMain() {
}
/**
* Number of junk entries at the end of the array.
*/
private static final int JUNK_SIZE = 5;
/**
* Checks if the subtree of the given {@code array} rooted at the given
* {@code top} is a heap.
*
* @param array
* the complete binary tree
* @param top
* the index of the root of the "subtree"
* @param last
* the index of the last entry in the heap
* @return true if the subtree of the given {@code array} rooted at the
* given {@code top} is a heap; false otherwise
* @requires <pre>
* 0 <= top and last < |array.entries| and
* [subtree rooted at {@code top} is a complete binary tree]
* </pre>
* @ensures isHeap = SUBTREE_IS_HEAP(heap, top, last, <=)
*/
private static boolean isHeap(int[] array, int top, int last) {
assert array != null : "Violation of: array is not null";
assert 0 <= top : "Violation of: 0 <= top";
assert last < array.length : "Violation of: last < |array|";
/*
* No need to check the other requires clause, because it must be true
* when using the Array representation for a complete binary tree.
*/
int left = 2 * top + 1;
boolean isHeap = true;
if (left <= last) { // there is non-empty left subtree
isHeap = (array[top] <= array[left]) && isHeap(array, left, last);
int right = left + 1;
if (isHeap && (right <= last)) { // there is non-empty right subtree
isHeap = (array[top] <= array[right])
&& isHeap(array, right, last);
}
}
return isHeap;
}
/**
* Finds {@code item} in DOMAIN({@code m}) and, if such exists, adds 1 to
* the value in {@code m} associated with key {@code item}; otherwise places
* new key {@code item} in {@code m} with associated value 1.
*
* @param <K>
* the type of the map's key
* @param item
* the item whose count is to be incremented
* @param m
* the {@code Map} to be updated
* @aliases reference item
* @updates m
* @ensures <pre>
* if item is in DOMAIN(m) then
* there exists count: integer ((item, count) is in #m
* and m = (#m \ (item, count)) union {(item, count + 1)})
* else
* m = #m union {(item, 1)}
* </pre>
*/
private static <K> void incrementCountFor(K item, Map<K, Integer> m) {
assert item != null : "Violation of: item is not null";
assert m != null : "Violation of: m is not null";
if (m.hasKey(item)) {
Map.Pair<K, Integer> pair = m.remove(item);
m.add(pair.key(), pair.value() + 1);
} else {
m.add(item, 1);
}
}
/**
* Exchanges entries at indices {@code i} and {@code j} of {@code array}.
*
* @param array
* the array whose entries are to be exchanged
* @param i
* one index
* @param j
* the other index
* @updates array
* @requires 0 <= i < |array| and 0 <= j < |array|
* @ensures array = [#array with entries at indices i and j exchanged]
*/
private static void exchangeEntries(int[] array, int i, int j) {
assert array != null : "Violation of: array is not null";
assert 0 <= i : "Violation of: 0 <= i";
assert i < array.length : "Violation of: i < |array|";
assert 0 <= j : "Violation of: 0 <= j";
assert j < array.length : "Violation of: j < |array|";
if (i != j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
/**
* Given an array that represents a complete binary tree and an index
* referring to the root of a subtree that would be a heap except for its
* root, sifts the root down to turn that whole subtree into a heap.
*
* @param array
* the complete binary tree
* @param top
* the index of the root of the "subtree"
* @param last
* the index of the last entry in the heap
* @updates array
* @requires <pre>
* 0 <= top and last < |array.entries| and
* SUBTREE_IS_HEAP(array, 2 * top + 1, last, <=) and
* SUBTREE_IS_HEAP(array, 2 * top + 2, last, <=)
* [subtree rooted at {@code top} is a complete binary tree]
* </pre>
* @ensures <pre>
* SUBTREE_IS_HEAP(array, top, last, <=) and
* perms(array, #array) and
* SUBTREE_ARRAY_ENTRIES(array, top, last) =
* SUBTREE_ARRAY_ENTRIES(#array, top, last) and
* [the other entries in array are the same as in #array]
* </pre>
*/
private static void siftDown(int[] array, int top, int last) {
assert array != null : "Violation of: array is not null";
assert 0 <= top : "Violation of: 0 <= top";
assert last < array.length : "Violation of: last < |array|";
assert isHeap(array, 2 * top + 1, last) : ""
+ "Violation of: SUBTREE_IS_HEAP(array, 2 * top + 1, last, <=)";
assert isHeap(array, 2 * top + 2, last) : ""
+ "Violation of: SUBTREE_IS_HEAP(array, 2 * top + 2, last, <=)";
/*
* No need to check the other requires clause, because it must be true
* when using the array representation for a complete binary tree.
*/
// TODO - fill in body using recursive algorithm from slides
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
/*
* Input array size from user
*/
out.print("Enter (non-negative) heap size: ");
int heapSize = in.nextInteger();
/*
* Construct array as follows. Make its length be heapSize + JUNK_SIZE.
* The prefix of length heapSize of array represents a complete binary
* tree and contains pseudo-random integers in the range [JUNK_SIZE,
* heapSize + JUNK_SIZE). The suffix of length JUNK_SIZE is junk. These
* junk values are specifically set so as to count down from one less
* than JUNK_SIZE to 0.
*
* Also, build a Map<Integer, Integer> named original to represent the
* multiset of values present in the initial complete binary tree.
*/
Map<Integer, Integer> original = new Map1L<>();
Random rnd = new Random1L();
int[] array = new int[heapSize + JUNK_SIZE];
for (int i = 0; i < heapSize; i++) {
int entry = JUNK_SIZE + ((int) (rnd.nextDouble() * heapSize));
array[i] = entry;
incrementCountFor(entry, original);
}
for (int i = heapSize; i < heapSize + JUNK_SIZE; i++) {
array[i] = heapSize + JUNK_SIZE - i - 1;
}
/*
* Output initial array
*/
out.println(" initial array: " + Arrays.toString(array));
/*
* Heapify the heapSize-length prefix of array by repeatedly calling
* siftDown (this is an iterative implementation of heapify--it should
* start with i = heapSize / 2 - 1, but since it is intended to test
* siftDown, we call it on the leaves as well)
*/
for (int i = heapSize - 1; i >= 0; i--) {
siftDown(array, i, heapSize - 1);
}
/*
* Make sure the heapSize-length prefix of array is now a heap
*/
assert isHeap(array, 0, heapSize - 1) : ""
+ "Violation of: SUBTREE_IS_HEAP(array, 0, heapSize - 1, <=)";
/*
* Make sure the current multiset of values in the heapSize-length
* prefix of array is the same as the original
*/
Map<Integer, Integer> current = original.newInstance();
for (int i = 0; i < heapSize; i++) {
incrementCountFor(array[i], current);
}
assert current.equals(original) : ""
+ "Method siftDown caused different values to be in the heap "
+ "than were in the original complete binary tree, "
+ "perhaps by failing to ignore the junk at "
+ "the far end of the array.";
/*
* Make sure the junk at the far end of array was not changed by
* siftDown
*/
for (int i = heapSize; i < heapSize + JUNK_SIZE; i++) {
assert heapSize + JUNK_SIZE - i - 1 == array[i] : ""
+ "Method siftDown changed the junk at "
+ "the far end of the array: Expected "
+ (heapSize + JUNK_SIZE - i - 1) + " but was " + array[i];
}
/*
* If everything worked, output the array with a heapified prefix
*/
out.println("array with heapified prefix: " + Arrays.toString(array));
/*
* Close streams
*/
in.close();
out.close();
}
}