algoClass icon indicating copy to clipboard operation
algoClass copied to clipboard

Error in depth first search: Incorrect order

Open FrankDupree opened this issue 6 years ago • 0 comments

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]

FrankDupree avatar Apr 05 '18 07:04 FrankDupree