@@ -72,11 +72,18 @@ class DirectedGraphConstructTracer extends Tracer {
7272 } ) ;
7373 return this ;
7474 }
75+
76+ _clearTraversal ( ) {
77+ this . manager . pushStep ( this . capsule , {
78+ type : 'clear'
79+ } ) ;
80+ return this ;
81+ }
7582
7683 processStep ( step , options ) {
7784 switch ( step . type ) {
78- case 'setTreeData ' :
79- this . setTreeData . apply ( this , step . arguments ) ;
85+ case 'clear ' :
86+ this . clear . apply ( this ) ;
8087 break ;
8188 case 'setNodePositions' :
8289 $ . each ( this . graph . nodes ( ) , ( i , node ) => {
@@ -101,13 +108,13 @@ class DirectedGraphConstructTracer extends Tracer {
101108 var targetNode = this . graph . nodes ( this . n ( step . target ) ) ;
102109 var color = visit ? this . color . visited : this . color . left ;
103110 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+ }
111118 }
112119 if ( this . logTracer ) {
113120 var source = step . source ;
@@ -159,7 +166,6 @@ class DirectedGraphConstructTracer extends Tracer {
159166 }
160167 }
161168 }
162- nodeObject . updateBreadth ( ) ;
163169 this . nodeCollection . push ( nodeObject ) ;
164170 return nodeObject ;
165171 }
@@ -169,23 +175,10 @@ class DirectedGraphConstructTracer extends Tracer {
169175 id : this . n ( val ) ,
170176 originalVal : val ,
171177 isNew : true ,
178+ visited : false ,
172179 children : [ ] ,
173- breadth : 0 ,
174180 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
189182 }
190183 return nodeObject ;
191184 }
@@ -194,43 +187,60 @@ class DirectedGraphConstructTracer extends Tracer {
194187 const nodes = [ ] ;
195188 const edges = [ ] ;
196189 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 ) ;
203210 }
204-
211+ return offsetWidth ;
212+ } ;
213+
214+ var addGraphNode = function ( node , calculatedX ) {
215+ var color = getColor ( node . isNew , node . visited , tracer . color ) ;
205216 nodes . push ( {
206217 id : node . id ,
207218 label : '' + node . originalVal ,
208219 x : calculatedX ,
209220 y : node . level - 1 ,
210221 size : 1 ,
211- color : node . isNew ? tracer . color . selected : ( node . visited ? tracer . color . visited : tracer . color . default ) ,
222+ color : color ,
212223 weight : 0
213224 } ) ;
225+ } ;
214226
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+ } ) ;
232237 } ;
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 ) ;
234244
235245 this . graph . clear ( ) ;
236246 this . graph . read ( {
@@ -269,8 +279,9 @@ class DirectedGraphConstructTracer extends Tracer {
269279 }
270280
271281 clearGraphColor ( ) {
282+ var tracer = this ;
272283 this . nodeCollection . forEach ( function ( node ) {
273- node . isNew = false ;
284+ node . visited = node . isNew = false ;
274285 } ) ;
275286
276287 this . graph . nodes ( ) . forEach ( function ( node ) {
0 commit comments