kuychaco / algoClass

Common data structures and algorithms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in depth first search: Incorrect order

FrankDupree opened this issue · 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]