Skip to content

Commit 92db845

Browse files
committed
heapSort cleanup
1 parent 37d83ea commit 92db845

File tree

2 files changed

+44
-63
lines changed

2 files changed

+44
-63
lines changed

src/algorithms/controllers/heapSort.js

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
1+
// Heapsort animation
2+
//
3+
// It's worth looking at this code if you are planning to write any new
4+
// modules.
5+
//
6+
// This was the first animation done and the code is reasonably simple -
7+
// the abstractions supported match what we need for this algorithm.
8+
// For various other algorithms, the code seems much more messy - maybe
9+
// the abstractions for the data structures/rendering are not quite what
10+
// is needed or the coding is done with a sledgehammer, so to speak.
11+
//
12+
// The original version of this code was not quite right in the way it
13+
// adapted (or didn't adapt) to expansion/collapse of code blocks. This
14+
// was added later in a reasonably simple way (again, other algorithms
15+
// may use the sledgehammer style).
16+
//
17+
// One thing that could make the code here more readable is to use
18+
// meaningful strings for bookmarks rather than numbers.
19+
120
/* eslint-disable no-multi-spaces,indent,prefer-destructuring,brace-style */
221
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
322
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
423
import {areExpanded} from './collapseChunkPlugin';
524

625
// k displayed only if first BuildHeap is expanded
26+
// Note: This is only needed in the last chunk of BuildHeap. The code
27+
// looks like it displays k throughout BuildHeap but when BuildHeap is
28+
// collapsed, only the last chunk is rendered so the other chunks don't
29+
// matter and we can avoid testing what is expanded there. Another
30+
// approach would be to use a wrapper function for assigning to k, which
31+
// checks isBuildHeapExpanded() (it doesn't generalise well for i and j
32+
// though).
733
function isBuildHeapExpanded() {
834
return areExpanded(['BuildHeap']);
935
}
1036

1137
// i, j (in build) displayed only if first DownHeap is expanded
38+
// See Note in isBuildHeapExpanded()
1239
function isDownHeapkExpanded() {
1340
return areExpanded(['BuildHeap', 'DownHeapk']);
1441
}
1542

16-
// i, j (in sort) displayed only if second DownHeap is expanded
17-
function isDownHeap1Expanded() {
18-
return areExpanded(['SortHeap', 'DownHeap1']);
19-
}
20-
2143
export default {
2244
initVisualisers() {
2345
return {
@@ -85,22 +107,21 @@ export default {
85107
}, [n1, n2]);
86108
};
87109

88-
/** NOTE: In Linda's code, array index starts from 1
110+
/** NOTE: In Lee's code, array index starts from 1
89111
* however, in JS, array index naturally starts from 0
90112
* index start from 0:
91113
* parent = k , left child = 2*k + 1, right child = 2*k + 2
92114
* index start from 1:
93115
* parent = k , left child = 2*k, right child = 2*k + 1
94116
*/
95117

96-
let lastiHighlight = null;
118+
// keep track of last node highlighted due to i (or k) so we can
119+
// unhighlight it of buildHeap is collapsed
120+
let lastiHighlight;
97121

98122
// build heap
99123
// start from the last non-leaf node, work backwards to maintain the heap
100124
for (let k = Math.floor(n / 2) - 1; k >= 0; k -= 1) {
101-
// chunker.add(4, (vis, index) => {
102-
// vis.array.assignVariable('k', index);
103-
// }, [k]);
104125

105126
let j;
106127
const tmp = i;
@@ -117,12 +138,6 @@ export default {
117138
}, [i, tmp]);
118139

119140
chunker.add(6, (vis, index1, index2) => {
120-
// if (tmp != null) { // XXX looks dodgy using tmp here?
121-
// if (index2 != null) {
122-
// unhighlight(vis, index2);
123-
// vis.array.removeVariable('j');
124-
// }
125-
// highlight(vis, index1);
126141
vis.array.assignVariable('i', index1);
127142
}, [i, tmp]);
128143

@@ -160,15 +175,13 @@ export default {
160175
// possible last chunk in BuildHeap/DownHeapk
161176
// remove i, j if !isDownHeapkExpanded
162177
if (!isDownHeapkExpanded()) {
163-
vis.array.assignVariable('i', undefined);
164-
vis.array.assignVariable('j', undefined);
178+
vis.array.removeVariable('i');
179+
vis.array.removeVariable('j');
165180
}
166181
// remove k+highlighting if !isBuildHeapExpanded
167182
if (!isBuildHeapExpanded()) {
168-
vis.array.assignVariable('k', undefined);
169-
if (lastiHighlight !== null) {
170-
unhighlight(vis, lastH);
171-
}
183+
vis.array.removeVariable('k');
184+
unhighlight(vis, lastH);
172185
}
173186
}, [j, lastiHighlight]);
174187
} else {
@@ -182,15 +195,13 @@ export default {
182195
vis.array.assignVariable('i', c);
183196
// remove i, j if !isDownHeapkExpanded
184197
if (!isDownHeapkExpanded()) {
185-
vis.array.assignVariable('i', undefined);
186-
vis.array.assignVariable('j', undefined);
198+
vis.array.removeVariable('i');
199+
vis.array.removeVariable('j');
187200
}
188201
// remove k+highlighting if !isDownHeapkExpanded
189202
if (!isBuildHeapExpanded()) {
190-
vis.array.assignVariable('k', undefined);
191-
if (lastiHighlight !== null) {
192-
unhighlight(vis, lastH);
193-
}
203+
vis.array.removeVariable('k');
204+
unhighlight(vis, lastH);
194205
}
195206
}, [i, j, lastiHighlight]);
196207
i = j;
@@ -202,18 +213,10 @@ export default {
202213

203214
while (n > 1) {
204215
chunker.add(20, (vis, nVal, index) => {
205-
// if first iteration of while loop - clear variables & show 'n'
206-
if (nVal === nodes.length) {
207-
vis.array.clearVariables();
208-
vis.array.assignVariable('n', nVal - 1);
209-
} else {
210-
vis.array.removeVariable('i');
211-
vis.array.removeVariable('j');
212-
}
213-
214-
// else only clear 'j'
215-
216-
unhighlight(vis, index);
216+
// clear variables & show 'n'
217+
vis.array.clearVariables();
218+
vis.array.assignVariable('n', nVal - 1);
219+
unhighlight(vis, index); // XXX skip for first loop iteration?
217220
}, [n, i]);
218221

219222
let j;
@@ -228,7 +231,6 @@ export default {
228231
swapAction(21, 0, n - 1);
229232

230233
chunker.add(22, (vis, index) => {
231-
// unhighlight(vis, index);
232234
unhighlight(vis, index, false);
233235
vis.array.sorted(index);
234236
vis.heap.sorted(index + 1);
@@ -239,17 +241,14 @@ export default {
239241

240242
i = 0;
241243
chunker.add(24, (vis, index1, nVal) => {
242-
if (nVal > 0) {
243-
// highlight(vis, index1);
244-
}
245244
vis.array.assignVariable('i', index1);
246245
}, [i, n]);
247246

248247
chunker.add(25);
249248
heap = false;
250249

251250
chunker.add(26, (vis, nVal) => {
252-
if (nVal === 0) vis.array.clearVariables();
251+
// if (nVal === 0) vis.array.clearVariables();
253252
}, [n]);
254253
// need to maintain the heap after swap
255254
while (!(2 * i + 1 >= n || heap)) {
@@ -274,12 +273,6 @@ export default {
274273
heap = true;
275274
chunker.add(33, (vis, index) => {
276275
unhighlight(vis, index, false);
277-
// possible last chunk in SortHeap/DownHeap1
278-
// remove i, j if !isDownHeap1Expanded
279-
if (!isDownHeap1Expanded()) {
280-
vis.array.assignVariable('i', undefined);
281-
vis.array.assignVariable('j', undefined);
282-
}
283276
}, [j]);
284277
} else {
285278
swap = A[i];
@@ -289,12 +282,6 @@ export default {
289282
chunker.add(36, (vis, p, c) => {
290283
unhighlight(vis, p, false);
291284
vis.array.assignVariable('i', c);
292-
// possible last chunk in SortHeap/DownHeap1
293-
// remove i, j if !isDownHeap1Expanded
294-
if (!isDownHeap1Expanded()) {
295-
vis.array.assignVariable('i', undefined);
296-
vis.array.assignVariable('j', undefined);
297-
}
298285
}, [i, j]);
299286
i = j;
300287
}

src/algorithms/controllers/kruskal.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ export default {
132132
}
133133
}
134134
edges.sort((a, b) => a.weight - b.weight);
135-
// let tmp = edges.shift();
136-
// console.log([tmp.weight, tmp.node1-1, tmp.node2-1]);
137-
// console.log(edges);
138-
console.log(nodeSets);
139-
console.log(ufRank);
140135

141136
nodes.push('i'); // initialize the display
142137
findD.push('find(i)');
@@ -300,7 +295,6 @@ export default {
300295

301296

302297
if (find(nodeSets, n1) != find(nodeSets, n2)) {
303-
console.log(['selecting', e.weight, e.node1+1, e.node2+1]);
304298
nselected++;
305299
selectedD[nselected] = (e.node1+1)+'-'+(e.node2+1)
306300
costD[nselected] = e.weight;

0 commit comments

Comments
 (0)