title: breadth-first traversal of a tree in javascript
body: <p>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:</p>
  
  <pre><code>Tree.prototype.traverse = function (callback) {
    callback(this.value);
  
    if (!this.children) {
      return;
    }
    for (var i = 0; i &lt; this.children.length; i++) {
      var child = this.children[i];
      child.traverse(callback);
    }
  };
  </code></pre>
  
  <p>How could I change this to make it breadth first instead?
  This is what the Tree Class looks like:</p>
  
  <pre><code>var Tree = function (value) {
    var newTree = {};
  
    newTree.value = value;
    newTree.children = [];
    extend(newTree, treeMethods);
  
    return newTree;
  };
  </code></pre>