I am trying to learn data structures well and implemented the following code for a depth-first traversal/application of a callback on a regular tree:
Tree.prototype.traverse = function (callback) {
callback(this.value);
if (!this.children) {
return;
}
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i];
child.traverse(callback);
}
};
How could I change this to make it breadth first instead? This is what the Tree Class looks like:
var Tree = function (value) {
var newTree = {};
newTree.value = value;
newTree.children = [];
extend(newTree, treeMethods);
return newTree;
};