-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New nodesAtDepth(depth) function #178
Comments
We should probably cut short when we're at the desired depth, and avoid a traversal of the branches that are deeper. |
@Fil Yeah, true... Thinking about this, it would be useful to extend iterator.js as well as create another function called eachToDepth: First, the changes to iterator.js: export default function*(depth = this.height) {
var node = this, current, next = [node], children, i, n;
do {
current = next.reverse(), next = [];
while (node = current.pop()) {
yield node;
if ((children = node.children) && node.depth < depth) {
for (i = 0, n = children.length; i < n; ++i) {
next.push(children[i]);
}
}
}
} while (next.length);
} Now the new eachToDepth.js file (leveraging our changed iterator): export default function(callback, depth, that) {
let index = -1;
for (const node of this[Symbol.iterator](depth)) {
callback.call(that, node, ++index, this);
}
return this;
} Then we can use the changes above to construct nodesAtDepth.js: export default function(depth) {
const nodes = []
this.eachToDepth(function(node) {
if (node.depth === depth) {
nodes.push(node);
}
}, depth);
return nodes
} The above proposals include nodes at the specified depth (traverse tree breadth first including descendants with specified depth) |
Or maybe we don't even need eachToDepth.js... In that case nodesAtDepth.js would be: export default function(depth) {
const nodes = []
for (const node of this[Symbol.iterator](depth)) {
if (node.depth === depth) {
nodes.push(node);
}
}
return nodes
} |
Hey Mike
Great work. For tree traversal it would be great to have a function that returns all nodes at a certain depth.
Example (taken from source code for leaves()):
kind regards Benjamin
The text was updated successfully, but these errors were encountered: