Can anyone provide either code, pseudocode, or even provide good links to implementing DFS and BFS in plain JavaScript (No JQuery, or any helper libraries)? I\'ve been trying to
DFS:
function m(elem) {
elem.childNodes.forEach(function(a) {
m(a);
});
//do sth with elem
}
m(document.body);
This loops through all elements, and for each element through each child's and so on.
BFS:
var childs = [];
function m(elem) {
elem.childNodes.forEach(function(a) {
childs.push(a);
});
b = childs;
childs = [];
b.forEach(function(a) {
m(a);
});
}
m(document.body);
This loops through the elements, pushes their children onto a stack, and starts again with each of them. As you can see, this consumes much more space (the child's array) which is not the best...