algoClass
algoClass copied to clipboard
Error in depth first search: Incorrect order
Tree.js traverseDepthFirst doesn't return the correct order.
Tree.prototype.traverseDepthFirst= function(fn){
this.children.forEach(function(child){
child.traverseDepthFirst(fn);
});
fn(this)
}
Based on the example dataset:
var tree = new Tree(1);
var branch1 = tree.addChild(2);
var branch2 = tree.addChild(3);
var branch3 = tree.addChild(4);
branch1.addChild(5);
branch1.addChild(6);
branch3.addChild(7).addChild(8);
The correct order should be: [1, 2, 5, 6, 3, 4, 7, 8]
Instead it returns: [5, 6, 2, 3, 8, 7, 4, 1]