{"size": 1116, "ext": "js", "max_stars_repo_path": "src/test/algorithms/searching/dfs.js", "max_stars_repo_name": "Subomi/algorithms.js", "max_stars_repo_stars_event_min_datetime": "2017-04-10T08:19:05.000Z", "max_stars_repo_stars_event_max_datetime": "2017-04-10T08:19:05.000Z", "max_issues_repo_path": "src/test/algorithms/searching/dfs.js", "max_issues_repo_name": "niilante/algorithms.js-1", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/test/algorithms/searching/dfs.js", "max_forks_repo_name": "niilante/algorithms.js-1", "max_forks_repo_forks_event_min_datetime": "2019-03-02T03:23:43.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-02T03:23:43.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 25.3636363636, "max_line_length": 76, "alphanum_fraction": 0.5869175627}
'use strict';

var root = require('../../..');
var BST = root.DataStructures.BST;
var dfs = root.Search.dfs;
var assert = require('assert');

describe('Depth First Search', function() {
  var bst = new BST();
  bst.insert(4);
  bst.insert(8);
  bst.insert(10);
  bst.insert(2);
  bst.insert(1);
  bst.insert(3);
  bst.insert(0);
  bst.insert(5);
  bst.insert(100);

  var callbackGenerator = function(a) {
    return n => a.push(n);
  };

  it('should return the items sorted when retrieving in order', function() {
    var a = [];
    dfs(bst.root, callbackGenerator(a));
    assert.deepEqual(a, [0, 1, 2, 3, 4, 5, 8, 10, 100]);
  });

  it('should return parents before children when retrieving pre-order',
      function() {
        var a = [];
        dfs.preOrder(bst.root, callbackGenerator(a));
        assert.deepEqual(a, [4, 2, 1, 0, 3, 8, 5, 10, 100]);
      });

  it('should return children before parents when retrieving post-order',
      function() {
        var a = [];
        dfs.postOrder(bst.root, callbackGenerator(a));
        assert.deepEqual(a, [0, 1, 3, 2, 5, 100, 10, 8, 4]);
      });
});
