GithubHelp home page GithubHelp logo

Comments (3)

Fil avatar Fil commented on April 28, 2024

We should probably cut short when we're at the desired depth, and avoid a traversal of the branches that are deeper.

from d3-hierarchy.

benjaminpreiss avatar benjaminpreiss commented on April 28, 2024

@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)

from d3-hierarchy.

benjaminpreiss avatar benjaminpreiss commented on April 28, 2024

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
}

from d3-hierarchy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.