Content-Length: 666566 | pFad | http://github.com/alunix/algorithm-visualizer/commit/274ec63129117b8bd25481f3653650fcd171db9b

5D Removed node collection dependecy from consumed app · alunix/algorithm-visualizer@274ec63 · GitHub
Skip to content

Commit 274ec63

Browse files
committed
Removed node collection dependecy from consumed app
udpated code to store nodeCollection and root with in the construct tracer
1 parent 7cf642a commit 274ec63

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
function bst_insert ( root, element, parent ) { // node = current node , parent = previous node
1+
function bst_insert ( root, element, parent ) { // root = current node , parent = previous node
22
tracer._visit ( root, parent )._wait ();
3+
var treeNode = T[root];
4+
var propName = '';
35
if ( element < root ) {
4-
if ( T[root][0] === -1 ) { // insert as left child of root
5-
T[root][0] = element;
6-
G[root][element] = 1; // update in graph
7-
tracer._visit ( element, root )._wait ();
6+
propName = 'left';
7+
} else if ( element > root) {
8+
propName = 'right';
9+
}
10+
if(propName !== '') {
11+
if ( !treeNode.hasOwnProperty(propName) ) { // insert as left child of root
12+
treeNode[propName] = element;
13+
T[element] = {};
14+
tracer._addNode(element, root);
815
logger._print( element + ' Inserted ');
916
} else {
10-
//tracer._clear ();
11-
bst_insert ( T[root][0], element, root );
12-
}
13-
} else if ( element > root ) { // insert as right child of root
14-
if( T[root][1] === -1 ) {
15-
T[root][1] = element;
16-
G[root][element] = 1; // update in graph
17-
tracer._visit ( element, root )._wait ();
18-
logger._print ( element + ' Inserted ');
19-
} else {
20-
//tracer._clear ();
21-
bst_insert ( T[root][1], element, root );
17+
bst_insert ( treeNode[propName], element, root );
2218
}
2319
}
2420
}
2521

2622
var Root = elements[0]; // take first element as root
23+
T[Root] = {};
24+
tracer._addRoot(Root);
2725
logger._print ( Root + ' Inserted as root of tree ');
28-
tracer._setTreeData ( G, Root );
2926

3027
for (var i = 1; i < elements.length; i++) {
3128
tracer2._select ( i )._wait();
3229
bst_insert ( Root, elements[i] ); // insert ith element
3330
tracer2._deselect( i );
31+
tracer.clear();
3432
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
var G = [];
2-
3-
41
var T = {};
52

63
var elements = [5,8,10,3,1,6,9,7,2,0,4]; // item to be searched
7-
var tracer = new DirectedGraphTracer( " BST - Elements marked red indicates the current status of tree ");
4+
var tracer = new DirectedGraphConstructTracer( " BST - Elements marked red indicates the current status of tree ", 0);
85
var tracer2 = new Array1DTracer ( " Elements ")._setData ( elements );
96
var logger = new LogTracer ( " Log ");
107
tracer.attach ( logger );

js/module/tracer/directed_graph_construct.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,29 @@ class DirectedGraphConstructTracer extends Tracer {
1414
constructor(name, nodePlacement = null) {
1515
super(name);
1616
this.nodePlacement = nodePlacement;
17+
this.nodeCollection = [];
1718
if (this.isNew) initView(this);
1819
}
1920

20-
_addNode(G, root, element, parentElement = null) {
21+
_addRoot(root) {
22+
this.manager.pushStep(this.capsule, {
23+
type: 'addRoot',
24+
arguments: arguments
25+
});
26+
return this;
27+
}
28+
29+
_addNode(element, parentElement = null) {
2130
this.manager.pushStep(this.capsule, {
2231
type: 'addNode',
2332
arguments: arguments
2433
});
2534
return this;
2635
}
2736

28-
_findNode(G, val) {
37+
_findNode(val) {
2938
var idToFind = this.n(val);
39+
var G = this.nodeCollection;
3040
var result = null;
3141
for (let i = 0; i < G.length; i++) {
3242
if(G[i].id === idToFind) {
@@ -37,10 +47,9 @@ class DirectedGraphConstructTracer extends Tracer {
3747
return result;
3848
}
3949

40-
_visit(G, target, source) {
50+
_visit(target, source) {
4151
this.manager.pushStep(this.capsule, {
4252
type: 'visit',
43-
G: G,
4453
target: target,
4554
source: source
4655
});
@@ -77,13 +86,16 @@ class DirectedGraphConstructTracer extends Tracer {
7786
node.y = position.y;
7887
});
7988
break;
89+
case 'addRoot':
90+
this.addRoot.apply(this, step.arguments);
91+
break;
8092
case 'addNode':
8193
this.addNode.apply(this, step.arguments);
8294
break;
8395
case 'visit':
8496
case 'leave':
8597
var visit = step.type == 'visit';
86-
var nodeObject = this._findNode(step.G, step.target);
98+
var nodeObject = this._findNode(step.target);
8799
nodeObject.visited = visit;
88100
nodeObject.isNew = false;
89101
var targetNode = this.graph.nodes(this.n(step.target));
@@ -108,9 +120,20 @@ class DirectedGraphConstructTracer extends Tracer {
108120
}
109121
}
110122

111-
addNode(G, root, node, parent) {
123+
addRoot(root) {
124+
if(this.rootObject) throw 'Root for this graph is already added';
125+
this.rootObject = this.createGraphNode(root);
126+
this.drawGraph(this.rootObject.level);
127+
}
128+
129+
addNode(node, parent) {
130+
var nodeObject = this.createGraphNode(node, parent)
131+
this.drawGraph(nodeObject.level);
132+
}
133+
134+
createGraphNode(node, parent) {
112135
var nodeObject = this.nodeConstruct(node);
113-
var parentObject = this._findNode(G, parent);
136+
var parentObject = this._findNode(parent);
114137
if (parentObject) {
115138
nodeObject.parent = parentObject;
116139
nodeObject.level = parentObject.level + 1;
@@ -131,18 +154,14 @@ class DirectedGraphConstructTracer extends Tracer {
131154
}
132155
if(isSpliced) {
133156
parentObject.children.splice(insertIndex, 0, nodeObject);
134-
console.log('exchange Happened');
135157
} else {
136158
parentObject.children.push(nodeObject);
137159
}
138160
}
139161
}
140162
nodeObject.updateBreadth();
141-
G.push(nodeObject);
142-
this.drawGraph(G, root, nodeObject.level);
143-
if(node === 4) {
144-
console.log(this.graph.nodes);
145-
}
163+
this.nodeCollection.push(nodeObject);
164+
return nodeObject;
146165
}
147166

148167
nodeConstruct(val) {
@@ -171,7 +190,7 @@ class DirectedGraphConstructTracer extends Tracer {
171190
return nodeObject;
172191
}
173192

174-
drawGraph(G, root, nodeLevel) {
193+
drawGraph(nodeLevel) {
175194
const nodes = [];
176195
const edges = [];
177196
var tracer = this;
@@ -210,9 +229,8 @@ class DirectedGraphConstructTracer extends Tracer {
210229
occupiedBreadth += node.breadth;
211230
}
212231
}
213-
}
214-
var rootObject = this._findNode(G, root);
215-
drawNode(rootObject);
232+
};
233+
drawNode(this.rootObject);
216234

217235
this.graph.clear();
218236
this.graph.read({
@@ -251,8 +269,10 @@ class DirectedGraphConstructTracer extends Tracer {
251269
}
252270

253271
clearGraphColor() {
254-
var tracer = this;
255-
272+
this.nodeCollection.forEach(function(node){
273+
node.isNew = false;
274+
});
275+
256276
this.graph.nodes().forEach(function (node) {
257277
node.color = tracer.color.default;
258278
});

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/alunix/algorithm-visualizer/commit/274ec63129117b8bd25481f3653650fcd171db9b

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy