tree-traversal.js
function traverseBreadthFirstSync(rootNode, options) {
  options = extend({
    subnodesAccessor: function(node) { return node.subnodes; },
    userdataAccessor: function(node, userdata) { return userdata; },
    onNode: function(node, userdata) {},
    userdata: null
  }, options);

  var queue = [];
  queue.push([rootNode, options.userdata]);

  while (queue.length>0) {
    var front = queue.shift();
    var node = front[0];
    var data = front[1];

    options.onNode(node, data);

    var subnodeData = options.userdataAccessor(node, data);
    var subnodes = options.subnodesAccessor(node);
    for (var i=0; i<subnodes.length; i++) {
      queue.push([subnodes[i], subnodeData]);
    }
  }
  return rootNode;
}
function traverseBreadthFirstSync(rootNode, options) {
  options = extend({
    subnodesAccessor: function(node) { return node.subnodes; },
    userdataAccessor: function(node, userdata) { return userdata; },
    onNode: function(node, userdata) {},
    userdata: null
  }, options);

  var queue = [];
  queue.push([rootNode, options.userdata]);

  while (queue.length>0) {
    var front = queue.shift();
    var node = front[0];
    var data = front[1];

    options.onNode(node, data);

    var subnodeData = options.userdataAccessor(node, data);
    var subnodes = options.subnodesAccessor(node);
    for (var i=0; i<subnodes.length; i++) {
      queue.push([subnodes[i], subnodeData]);
    }
  }
  return rootNode;
}
function traverseBreadthFirstSync ( rootNode , options ) { options = extend ( { subnodesAccessor : function ( node ) { return node . subnodes ; } , userdataAccessor : function ( node , userdata ) { return userdata ; } , onNode : function ( node , userdata ) { } , userdata : null } , options ) ; var queue = [ ] ; queue . push ( [ rootNode , options . userdata ] ) ; while ( queue . length > 0 ) { var front = queue . shift ( ) ; var node = front [ 0 ] ; var data = front [ 1 ] ; options . onNode ( node , data ) ; var subnodeData = options . userdataAccessor ( node , data ) ; var subnodes = options . subnodesAccessor ( node ) ; for ( var i = 0 ; i < subnodes . length ; i ++ ) { queue . push ( [ subnodes [ i ] , subnodeData ] ) ; } } return rootNode ; }
Synchronously traverses the tree breadth first.
Synchronously traverses the tree breadth first .