Skip to content

Error in depth first search: Incorrect order #24

@FrankDupree

Description

@FrankDupree

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions