Skip to content

Commit

Permalink
Complete treeDFSelect.js using preorder
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent 900194b commit 017f8b1
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions treeDFselect/treeDFSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,39 @@
* Basic tree that stores a value.
*/

var Tree = function(value){
var Tree = function (value) {
this.value = value;
this.children = [];
};

Tree.prototype.DFSelect = function(filter) {
// TODO: return an array of values for which the function filter(value, depth)
// returns true
Tree.prototype.DFSelect = function (fn) {
var temp = [];
var result = [];

var inner = function (tree, depth) {
var valueAndDepth = [ tree.value, depth ];
temp.push( valueAndDepth );

tree.children.forEach(function (childTree) {
inner( childTree, depth + 1 );
});
};

inner(this, 0);

// At this point, temp is an array of [value of treeNode, depth of treeNode]
// and the elements are in pre-order depth first search

temp.forEach(function (array) {
var value = array[0];
var depth = array[1];

if ( fn.call(null, value, depth) ) {
result.push( value );
}
});

return result;
};

/**
Expand Down

0 comments on commit 017f8b1

Please sign in to comment.