Skip to content

Commit 0546562

Browse files
committed
Updated layout stratergy to draw nodes
1 parent 274ec63 commit 0546562

File tree

2 files changed

+66
-55
lines changed

2 files changed

+66
-55
lines changed

algorithm/tree/binary_search_tree/bst_insert/code.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function bst_insert ( root, element, parent ) { // root = current node , parent
1111
if ( !treeNode.hasOwnProperty(propName) ) { // insert as left child of root
1212
treeNode[propName] = element;
1313
T[element] = {};
14-
tracer._addNode(element, root);
14+
tracer._addNode(element, root)._wait();
1515
logger._print( element + ' Inserted ');
1616
} else {
1717
bst_insert ( treeNode[propName], element, root );
@@ -27,6 +27,6 @@ logger._print ( Root + ' Inserted as root of tree ');
2727
for (var i = 1; i < elements.length; i++) {
2828
tracer2._select ( i )._wait();
2929
bst_insert ( Root, elements[i] ); // insert ith element
30-
tracer2._deselect( i );
31-
tracer.clear();
30+
tracer2._deselect( i )._wait();
31+
tracer._clearTraversal();
3232
}

js/module/tracer/directed_graph_construct.js

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy