Last active
August 29, 2015 14:07
-
-
Save bruslim/b52355a7955d23c6ac55 to your computer and use it in GitHub Desktop.
BFS with closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint node: true*/ | |
// TreeNode Class | |
function TreeNode(value, children) { | |
var _children = children || []; | |
Object.defineProperties(this, { | |
value: { | |
value: value, | |
enumerable: true, | |
writable: true, | |
}, | |
children: { | |
enumerable: true, | |
get: function() { | |
// return undefined for JSON.stringify to ignore no children | |
return _children.length ? _children : undefined; | |
} | |
} | |
}); | |
} | |
// Tree module | |
var Tree = (function() { | |
function bfs(start, initial, command) { | |
var q = [ | |
// first call | |
function() { | |
return visit(start, initial, command); | |
} | |
]; | |
do { | |
q = q.concat(q.shift().apply()); | |
} while (q.length); | |
} | |
function visit(node, previous, command) { | |
var current; | |
if (command && typeof(command) === 'function') { | |
current = command.call(null, node, previous); | |
} | |
var nexts = []; | |
(node.children || []).forEach(function(child) { | |
nexts.push(function() { | |
return visit(child, current, command); | |
}); | |
}); | |
return nexts; | |
} | |
// Tree Class | |
function Tree(root) { | |
Object.defineProperty(this,'root',{ | |
enumerable: true, | |
value: root | |
}); | |
} | |
Tree.prototype.toLevels = function() { | |
var levels = {}; | |
bfs(this.root, 0, function(node, currentLevel) { | |
// ensure that there is an array at currentLevel | |
levels[currentLevel] = levels[currentLevel] || []; | |
// add this node to the level | |
levels[currentLevel].push(node); | |
// increment the level | |
return currentLevel + 1; | |
}); | |
return levels; | |
}; | |
Tree.prototype.getPaths = function() { | |
var paths = []; | |
bfs(this.root, [], function(node, previousPath) { | |
// shallow-copy | |
var currentPath = previousPath.slice(); | |
// add this node to shallow-copy | |
currentPath.push(node.value); | |
// add to collection of paths | |
paths.push(currentPath); | |
return currentPath; | |
}); | |
// apply the join('.') function to denote paths | |
// in '.' notation | |
return paths.map(function(path) { | |
return path.join('.'); | |
}); | |
}; | |
return Tree; | |
})(); | |
var root = new TreeNode('root',[ | |
new TreeNode('A',[ | |
new TreeNode('A1'), | |
new TreeNode('A2') | |
]), | |
new TreeNode('B',[ | |
new TreeNode('B1',[ | |
new TreeNode('B1a'), | |
new TreeNode('B1b') | |
]), | |
new TreeNode('B2') | |
]), | |
new TreeNode('C',[ | |
new TreeNode('C1'), | |
new TreeNode('C2') | |
]) | |
]); | |
var tree = new Tree(root); | |
console.log(tree.toLevels()); | |
console.log(tree.getPaths()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment