Javascript Depth-first Search
I am trying to implement DFS in JavaScript but I am having a little problem. Here is my Algorithm class: 'use strict'; define([], function () { return function () {
Solution 1:
The problem is your addChild()
method only expects one parameter, but you are passing in multiple nodes to it.
Change your calling code to:
node1.addChild(node2);
node1.addChild(node7);
node1.addChild(node8);
node2.addChild(node3);
node2.addChild(node6);
node3.addChild(node4);
node3.addChild(node5);
node8.addChild(node9);
node8.addChild(node12);
node9.addChild(node10);
node9.addChild(node11);
Or you can change addChild to accept multiple children (probably want to change the name too):
this.addChildren = function () {
for (var i = 0; i < arguments.length; i++) {
children.push(arguments[i]);
}
};
Post a Comment for "Javascript Depth-first Search"