@@ -26,6 +26,23 @@ import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
2626import { areExpanded } from './collapseChunkPlugin' ;
2727import { 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
0 commit comments