Skip to content

Commit 4c0efba

Browse files
committed
Heapsort: color items in heap(s)
1 parent 7257983 commit 4c0efba

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/algorithms/controllers/heapSort.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
2626
import {areExpanded} from './collapseChunkPlugin';
2727
import {colors} from '../../components/DataStructures/colors';
2828

29+
// currently colors for graphs (including trees) are still a mess; this
30+
// is kind of a stub for when they are fixed up. The code involving
31+
// (un)highlight() and .sorted() should be fixed at this point also.
32+
// We define colors for the array (_A) and tree (_T) views; note current and
33+
// child nodes are swapped at times in downheap. At the start, nothing
34+
// is selected but it turns out that whenever anything is de-selected it
35+
// is part of a heap.
36+
const HSColors = {
37+
CURRENT_A: colors.apple,
38+
CHILD_A: colors.sky,
39+
HEAP_A: colors.leaf,
40+
CURRENT_T: 3, // Red (globalColors.apple)
41+
CHILD_T: 4, // Blue (globalColors.sky)
42+
HEAP_T: 1, // Green (globalColors.leaf)
43+
}
44+
45+
2946
// k displayed only if first BuildHeap is expanded
3047
// Note: This is only needed in the last chunk of BuildHeap. The code
3148
// looks like it displays k throughout BuildHeap but when BuildHeap is
@@ -91,22 +108,21 @@ export default {
91108

92109
const highlight = (vis, index, primaryColor = true) => {
93110
if (primaryColor) {
94-
vis.heap.visit(index + 1);
111+
vis.heap.colorNode(index + 1, HSColors.CURRENT_T);
95112
vis.array.selectColor(index, colors.apple);
96113
} else {
97-
vis.heap.select(index + 1);
114+
vis.heap.colorNode(index + 1, HSColors.CHILD_T);
98115
vis.array.selectColor(index, colors.sky);
99-
// vis.array.patch(index);
100116
}
101117
};
102118

103119
const unhighlight = (vis, index, primaryColor = true) => {
104120
if (primaryColor) {
105-
vis.heap.leave(index + 1);
121+
vis.heap.colorNode(index + 1, HSColors.HEAP_T);
106122
} else {
107-
vis.heap.deselect(index + 1);
123+
vis.heap.colorNode(index + 1, HSColors.HEAP_T);
108124
}
109-
vis.array.deselect(index);
125+
vis.array.selectColor(index, HSColors.HEAP_T);
110126
};
111127

112128
const swapAction = (b, n1, n2) => {
@@ -130,20 +146,27 @@ export default {
130146

131147
// build heap
132148
// start from the last non-leaf node, work backwards to maintain the heap
133-
for (let k = Math.floor(n / 2) - 1; k >= 0; k -= 1) {
149+
let lastNonLeaf = Math.floor(n / 2) - 1;
150+
for (let k = lastNonLeaf; k >= 0; k -= 1) {
134151

135152
let j;
136153
const tmp = i;
137154
i = k;
138155

139-
chunker.add(4, (vis, index1, index2) => {
156+
chunker.add(4, (vis, index1, index2, first, max) => {
140157
vis.array.assignVariable('k', index1);
158+
if (index1 === first) { // done the first time we reach here
159+
for (let l = index1 + 1; l <= max; l++) { // color leaves
160+
vis.heap.colorNode(l + 1, HSColors.HEAP_T);
161+
vis.array.selectColor(l, HSColors.HEAP_T);
162+
}
163+
}
141164
if (index2 != null) {
142165
unhighlight(vis, index2);
143166
vis.array.removeVariable('j');
144167
}
145168
highlight(vis, index1);
146-
}, [i, tmp]);
169+
}, [i, tmp, lastNonLeaf, n - 1]);
147170

148171
chunker.add(6, (vis, index1, index2) => {
149172
vis.array.assignVariable('i', index1);
@@ -245,6 +268,7 @@ export default {
245268
chunker.add(22, (vis, index) => {
246269
unhighlight(vis, index, false);
247270
vis.array.sorted(index);
271+
vis.heap.removeNodeColor(index + 1);
248272
vis.heap.sorted(index + 1);
249273

250274
vis.array.assignVariable('n', index - 1);
@@ -310,6 +334,7 @@ export default {
310334
// vis.array.deselect(0);
311335
unhighlight(vis, 0, true);
312336
vis.array.sorted(0);
337+
vis.heap.removeNodeColor(1);
313338
vis.heap.sorted(1);
314339
});
315340
// for test

src/components/DataStructures/Graph/GraphTracer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,29 @@ class GraphTracer extends Tracer {
267267
value: node1.value,
268268
key: node1.key,
269269
visitedCount: node1.visitedCount,
270+
visitedCount1: node1.visitedCount1, // currently need this junk for colors
271+
visitedCount2: node1.visitedCount2,
272+
visitedCount3: node1.visitedCount3,
273+
visitedCount4: node1.visitedCount4,
270274
selectedCount: node1.selectedCount,
271275
};
272276
const node2 = this.findNode(nodeId2);
273277
// Swap both the value and key (key is what animates swapping action)
274278
node1.value = node2.value;
275279
node1.key = node2.key;
276280
node1.visitedCount = node2.visitedCount;
281+
node1.visitedCount1 = node2.visitedCount1;
282+
node1.visitedCount2 = node2.visitedCount2;
283+
node1.visitedCount3 = node2.visitedCount3;
284+
node1.visitedCount4 = node2.visitedCount4;
277285
node1.selectedCount = node2.selectedCount;
278286
node2.value = temp.value;
279287
node2.key = temp.key;
280288
node2.visitedCount = temp.visitedCount;
289+
node2.visitedCount1 = temp.visitedCount1;
290+
node2.visitedCount2 = temp.visitedCount2;
291+
node2.visitedCount3 = temp.visitedCount3;
292+
node2.visitedCount4 = temp.visitedCount4;
281293
node2.selectedCount = temp.selectedCount;
282294
this.layoutTree(this.root);
283295
}
@@ -856,6 +868,7 @@ class GraphTracer extends Tracer {
856868
colorNode(node, colorIndex) {
857869
const _node = this.findNode(node);
858870
if (!_node) return; // Exit if node is not found
871+
this.removeNodeColor(node); // could avoid extra this.findNode(node)
859872

860873
if (colorIndex === 1) {
861874
_node.visitedCount1 = 1;

0 commit comments

Comments
 (0)