@@ -93,26 +93,40 @@ export default {
93
93
* parent = k , left child = 2*k, right child = 2*k + 1
94
94
*/
95
95
96
+ let lastiHighlight = null ;
97
+
96
98
// build heap
97
99
// start from the last non-leaf node, work backwards to maintain the heap
98
100
for ( let k = Math . floor ( n / 2 ) - 1 ; k >= 0 ; k -= 1 ) {
99
- chunker . add ( 4 , ( vis , index ) => {
100
- vis . array . assignVariable ( 'k' , index ) ;
101
- } , [ k ] ) ;
101
+ // chunker.add(4, (vis, index) => {
102
+ // vis.array.assignVariable('k', index);
103
+ // }, [k]);
102
104
103
105
let j ;
104
106
const tmp = i ;
105
107
i = k ;
106
108
107
- chunker . add ( 6 , ( vis , index1 , index2 ) => {
108
- if ( tmp != null ) {
109
+ chunker . add ( 4 , ( vis , index1 , index2 ) => {
110
+ vis . array . assignVariable ( 'k' , index1 ) ;
111
+ // if (tmp != null) { // XXX looks dodgy using tmp here?
112
+ if ( index2 != null ) {
109
113
unhighlight ( vis , index2 ) ;
110
114
vis . array . removeVariable ( 'j' ) ;
111
115
}
112
116
highlight ( vis , index1 ) ;
117
+ } , [ i , tmp ] ) ;
118
+
119
+ 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);
113
126
vis . array . assignVariable ( 'i' , index1 ) ;
114
127
} , [ i , tmp ] ) ;
115
128
129
+ lastiHighlight = k ;
116
130
heap = false ;
117
131
chunker . add ( 7 ) ;
118
132
@@ -141,37 +155,44 @@ export default {
141
155
// parent is greater than largest child, so it is already a valid heap
142
156
if ( A [ i ] >= A [ j ] ) {
143
157
heap = true ;
144
- chunker . add ( 15 , ( vis , index ) => {
158
+ chunker . add ( 15 , ( vis , index , lastH ) => {
145
159
unhighlight ( vis , index , false ) ;
146
160
// possible last chunk in BuildHeap/DownHeapk
147
161
// remove i, j if !isDownHeapkExpanded
148
162
if ( ! isDownHeapkExpanded ( ) ) {
149
163
vis . array . assignVariable ( 'i' , undefined ) ;
150
164
vis . array . assignVariable ( 'j' , undefined ) ;
151
165
}
152
- // remove k if !isBuildHeapExpanded
166
+ // remove k+highlighting if !isBuildHeapExpanded
153
167
if ( ! isBuildHeapExpanded ( ) ) {
154
168
vis . array . assignVariable ( 'k' , undefined ) ;
169
+ if ( lastiHighlight !== null ) {
170
+ unhighlight ( vis , lastH ) ;
171
+ }
155
172
}
156
- } , [ j ] ) ;
173
+ } , [ j , lastiHighlight ] ) ;
157
174
} else {
158
175
swap = A [ i ] ;
159
176
A [ i ] = A [ j ] ;
160
177
A [ j ] = swap ;
161
178
swapAction ( 17 , i , j ) ;
162
- chunker . add ( 18 , ( vis , p , c ) => {
179
+ lastiHighlight = j ;
180
+ chunker . add ( 18 , ( vis , p , c , lastH ) => {
163
181
unhighlight ( vis , p , false ) ;
164
182
vis . array . assignVariable ( 'i' , c ) ;
165
183
// remove i, j if !isDownHeapkExpanded
166
184
if ( ! isDownHeapkExpanded ( ) ) {
167
185
vis . array . assignVariable ( 'i' , undefined ) ;
168
186
vis . array . assignVariable ( 'j' , undefined ) ;
169
187
}
170
- // remove k if !isDownHeapkExpanded
188
+ // remove k+highlighting if !isDownHeapkExpanded
171
189
if ( ! isBuildHeapExpanded ( ) ) {
172
190
vis . array . assignVariable ( 'k' , undefined ) ;
191
+ if ( lastiHighlight !== null ) {
192
+ unhighlight ( vis , lastH ) ;
193
+ }
173
194
}
174
- } , [ i , j ] ) ;
195
+ } , [ i , j , lastiHighlight ] ) ;
175
196
i = j ;
176
197
}
177
198
}
@@ -185,7 +206,10 @@ export default {
185
206
if ( nVal === nodes . length ) {
186
207
vis . array . clearVariables ( ) ;
187
208
vis . array . assignVariable ( 'n' , nVal - 1 ) ;
188
- } else vis . array . removeVariable ( 'j' ) ;
209
+ } else {
210
+ vis . array . removeVariable ( 'i' ) ;
211
+ vis . array . removeVariable ( 'j' ) ;
212
+ }
189
213
190
214
// else only clear 'j'
191
215
@@ -198,14 +222,14 @@ export default {
198
222
A [ 0 ] = swap ;
199
223
200
224
chunker . add ( 21 , ( vis , index ) => {
201
- highlight ( vis , 0 ) ;
202
- highlight ( vis , index , false ) ;
225
+ highlight ( vis , index ) ;
226
+ highlight ( vis , 0 , false ) ;
203
227
} , [ n - 1 ] ) ;
204
228
swapAction ( 21 , 0 , n - 1 ) ;
205
229
206
230
chunker . add ( 22 , ( vis , index ) => {
207
- unhighlight ( vis , index ) ;
208
- unhighlight ( vis , 0 , false ) ;
231
+ // unhighlight(vis, index);
232
+ unhighlight ( vis , index , false ) ;
209
233
vis . array . sorted ( index ) ;
210
234
vis . heap . sorted ( index + 1 ) ;
211
235
@@ -216,7 +240,7 @@ export default {
216
240
i = 0 ;
217
241
chunker . add ( 24 , ( vis , index1 , nVal ) => {
218
242
if ( nVal > 0 ) {
219
- highlight ( vis , index1 ) ;
243
+ // highlight(vis, index1);
220
244
}
221
245
vis . array . assignVariable ( 'i' , index1 ) ;
222
246
} , [ i , n ] ) ;
0 commit comments