@@ -72,11 +72,18 @@ class DirectedGraphConstructTracer extends Tracer {
72
72
} ) ;
73
73
return this ;
74
74
}
75
+
76
+ _clearTraversal ( ) {
77
+ this . manager . pushStep ( this . capsule , {
78
+ type : 'clear'
79
+ } ) ;
80
+ return this ;
81
+ }
75
82
76
83
processStep ( step , options ) {
77
84
switch ( step . type ) {
78
- case 'setTreeData ' :
79
- this . setTreeData . apply ( this , step . arguments ) ;
85
+ case 'clear ' :
86
+ this . clear . apply ( this ) ;
80
87
break ;
81
88
case 'setNodePositions' :
82
89
$ . each ( this . graph . nodes ( ) , ( i , node ) => {
@@ -101,13 +108,13 @@ class DirectedGraphConstructTracer extends Tracer {
101
108
var targetNode = this . graph . nodes ( this . n ( step . target ) ) ;
102
109
var color = visit ? this . color . visited : this . color . left ;
103
110
if ( targetNode ) {
104
- targetNode . color = color ;
105
- if ( step . source !== undefined ) {
106
- var edgeId = this . e ( step . source , step . target ) ;
107
- var edge = this . graph . edges ( edgeId ) ;
108
- edge . color = color ;
109
- this . graph . dropEdge ( edgeId ) . addEdge ( edge ) ;
110
- }
111
+ targetNode . color = color ;
112
+ if ( step . source !== undefined ) {
113
+ var edgeId = this . e ( step . source , step . target ) ;
114
+ var edge = this . graph . edges ( edgeId ) ;
115
+ edge . color = color ;
116
+ this . graph . dropEdge ( edgeId ) . addEdge ( edge ) ;
117
+ }
111
118
}
112
119
if ( this . logTracer ) {
113
120
var source = step . source ;
@@ -159,7 +166,6 @@ class DirectedGraphConstructTracer extends Tracer {
159
166
}
160
167
}
161
168
}
162
- nodeObject . updateBreadth ( ) ;
163
169
this . nodeCollection . push ( nodeObject ) ;
164
170
return nodeObject ;
165
171
}
@@ -169,23 +175,10 @@ class DirectedGraphConstructTracer extends Tracer {
169
175
id : this . n ( val ) ,
170
176
originalVal : val ,
171
177
isNew : true ,
178
+ visited : false ,
172
179
children : [ ] ,
173
- breadth : 0 ,
174
180
level : 1 ,
175
- parent : null ,
176
- visited : false ,
177
- updateBreadth : function ( ) {
178
- var oldBreadth = nodeObject . breadth ;
179
- if ( nodeObject . children . length > 0 ) {
180
- nodeObject . breadth = nodeObject . children . length % 2 ? 0 : 1 ;
181
- for ( let j = 0 ; j < nodeObject . children . length ; j ++ ) {
182
- nodeObject . breadth += nodeObject . children [ j ] . breadth ;
183
- }
184
- } else { nodeObject . breadth = 1 ; }
185
- if ( oldBreadth !== nodeObject . breadth && nodeObject . parent ) {
186
- nodeObject . parent . updateBreadth ( ) ;
187
- }
188
- }
181
+ parent : null
189
182
}
190
183
return nodeObject ;
191
184
}
@@ -194,43 +187,60 @@ class DirectedGraphConstructTracer extends Tracer {
194
187
const nodes = [ ] ;
195
188
const edges = [ ] ;
196
189
var tracer = this ;
197
- var drawNode = function ( node , parentNode , occupiedBreadth ) {
198
- var calculatedX = node . breadth ;
199
- if ( parentNode ) {
200
- calculatedX = parentNode . breadth + occupiedBreadth - node . breadth ;
201
- } else if ( node . children . length > 0 ) {
202
- calculatedX = Math . ceil ( calculatedX / node . children . length ) ;
190
+
191
+ var arrangeChildNodes = function ( node , offsetWidth ) {
192
+ if ( node . children . length > 1 ) {
193
+ var midPoint = Math . floor ( node . children . length / 2 ) ;
194
+ for ( let i = 0 ; i < node . children . length ; i ++ ) {
195
+ if ( i === midPoint ) {
196
+ offsetWidth += ( node . children . length % 2 === 0 ? 1 : 0 ) ;
197
+ addGraphNode ( node , offsetWidth ) ;
198
+ }
199
+ offsetWidth = arrangeChildNodes ( node . children [ i ] , offsetWidth ) ;
200
+ addEdge ( node , node . children [ i ] ) ;
201
+ }
202
+ } else {
203
+ if ( node . children . length === 0 ) {
204
+ offsetWidth += 1 ;
205
+ } else {
206
+ offsetWidth = arrangeChildNodes ( node . children [ 0 ] , offsetWidth ) ;
207
+ addEdge ( node , node . children [ 0 ] ) ;
208
+ }
209
+ addGraphNode ( node , offsetWidth ) ;
203
210
}
204
-
211
+ return offsetWidth ;
212
+ } ;
213
+
214
+ var addGraphNode = function ( node , calculatedX ) {
215
+ var color = getColor ( node . isNew , node . visited , tracer . color ) ;
205
216
nodes . push ( {
206
217
id : node . id ,
207
218
label : '' + node . originalVal ,
208
219
x : calculatedX ,
209
220
y : node . level - 1 ,
210
221
size : 1 ,
211
- color : node . isNew ? tracer . color . selected : ( node . visited ? tracer . color . visited : tracer . color . default ) ,
222
+ color : color ,
212
223
weight : 0
213
224
} ) ;
225
+ } ;
214
226
215
- if ( node . children . length > 0 ) {
216
- var midPoint = node . children . length / 2 ;
217
- var occupiedBreadth = 0 ;
218
- for ( let j = 0 ; j < node . children . length ; j ++ ) {
219
- var childNode = node . children [ j ] ;
220
- edges . push ( {
221
- id : tracer . e ( node . originalVal , childNode . originalVal ) ,
222
- source : node . id ,
223
- target : childNode . id ,
224
- color : node . visited && childNode . visited ? tracer . color . visited : tracer . color . default ,
225
- size : 1 ,
226
- weight : refineByType ( childNode . originalVal )
227
- } ) ;
228
- drawNode ( childNode , node , occupiedBreadth ) ;
229
- occupiedBreadth += node . breadth ;
230
- }
231
- }
227
+ var addEdge = function ( node , childNode ) {
228
+ var color = getColor ( node . visited && childNode . isNew , node . visited && childNode . visited , tracer . color ) ;
229
+ edges . push ( {
230
+ id : tracer . e ( node . originalVal , childNode . originalVal ) ,
231
+ source : node . id ,
232
+ target : childNode . id ,
233
+ color : color ,
234
+ size : 1 ,
235
+ weight : refineByType ( childNode . originalVal )
236
+ } ) ;
232
237
} ;
233
- drawNode ( this . rootObject ) ;
238
+
239
+ var getColor = function ( isNew , isVisited , colorPalete ) {
240
+ return isNew ? colorPalete . selected :
241
+ ( isVisited ? colorPalete . visited : colorPalete . default ) ;
242
+ } ;
243
+ arrangeChildNodes ( this . rootObject , 0 ) ;
234
244
235
245
this . graph . clear ( ) ;
236
246
this . graph . read ( {
@@ -269,8 +279,9 @@ class DirectedGraphConstructTracer extends Tracer {
269
279
}
270
280
271
281
clearGraphColor ( ) {
282
+ var tracer = this ;
272
283
this . nodeCollection . forEach ( function ( node ) {
273
- node . isNew = false ;
284
+ node . visited = node . isNew = false ;
274
285
} ) ;
275
286
276
287
this . graph . nodes ( ) . forEach ( function ( node ) {
0 commit comments