@@ -26,6 +26,23 @@ import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
26
26
import { areExpanded } from './collapseChunkPlugin' ;
27
27
import { colors } from '../../components/DataStructures/colors' ;
28
28
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
+
29
46
// k displayed only if first BuildHeap is expanded
30
47
// Note: This is only needed in the last chunk of BuildHeap. The code
31
48
// looks like it displays k throughout BuildHeap but when BuildHeap is
@@ -91,22 +108,21 @@ export default {
91
108
92
109
const highlight = ( vis , index , primaryColor = true ) => {
93
110
if ( primaryColor ) {
94
- vis . heap . visit ( index + 1 ) ;
111
+ vis . heap . colorNode ( index + 1 , HSColors . CURRENT_T ) ;
95
112
vis . array . selectColor ( index , colors . apple ) ;
96
113
} else {
97
- vis . heap . select ( index + 1 ) ;
114
+ vis . heap . colorNode ( index + 1 , HSColors . CHILD_T ) ;
98
115
vis . array . selectColor ( index , colors . sky ) ;
99
- // vis.array.patch(index);
100
116
}
101
117
} ;
102
118
103
119
const unhighlight = ( vis , index , primaryColor = true ) => {
104
120
if ( primaryColor ) {
105
- vis . heap . leave ( index + 1 ) ;
121
+ vis . heap . colorNode ( index + 1 , HSColors . HEAP_T ) ;
106
122
} else {
107
- vis . heap . deselect ( index + 1 ) ;
123
+ vis . heap . colorNode ( index + 1 , HSColors . HEAP_T ) ;
108
124
}
109
- vis . array . deselect ( index ) ;
125
+ vis . array . selectColor ( index , HSColors . HEAP_T ) ;
110
126
} ;
111
127
112
128
const swapAction = ( b , n1 , n2 ) => {
@@ -130,20 +146,27 @@ export default {
130
146
131
147
// build heap
132
148
// 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 ) {
134
151
135
152
let j ;
136
153
const tmp = i ;
137
154
i = k ;
138
155
139
- chunker . add ( 4 , ( vis , index1 , index2 ) => {
156
+ chunker . add ( 4 , ( vis , index1 , index2 , first , max ) => {
140
157
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
+ }
141
164
if ( index2 != null ) {
142
165
unhighlight ( vis , index2 ) ;
143
166
vis . array . removeVariable ( 'j' ) ;
144
167
}
145
168
highlight ( vis , index1 ) ;
146
- } , [ i , tmp ] ) ;
169
+ } , [ i , tmp , lastNonLeaf , n - 1 ] ) ;
147
170
148
171
chunker . add ( 6 , ( vis , index1 , index2 ) => {
149
172
vis . array . assignVariable ( 'i' , index1 ) ;
@@ -245,6 +268,7 @@ export default {
245
268
chunker . add ( 22 , ( vis , index ) => {
246
269
unhighlight ( vis , index , false ) ;
247
270
vis . array . sorted ( index ) ;
271
+ vis . heap . removeNodeColor ( index + 1 ) ;
248
272
vis . heap . sorted ( index + 1 ) ;
249
273
250
274
vis . array . assignVariable ( 'n' , index - 1 ) ;
@@ -310,6 +334,7 @@ export default {
310
334
// vis.array.deselect(0);
311
335
unhighlight ( vis , 0 , true ) ;
312
336
vis . array . sorted ( 0 ) ;
337
+ vis . heap . removeNodeColor ( 1 ) ;
313
338
vis . heap . sorted ( 1 ) ;
314
339
} ) ;
315
340
// for test
0 commit comments