Skip to content
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

Update traversal to allow post-order traversal #301

Open
grapoza opened this issue Feb 10, 2023 · 0 comments
Open

Update traversal to allow post-order traversal #301

grapoza opened this issue Feb 10, 2023 · 0 comments
Labels
enhancement New feature or request
Projects

Comments

@grapoza
Copy link
Owner

grapoza commented Feb 10, 2023

Currently only pre-order traversal is supported by the depthFirstSearch and breadthFirstSearch methods. Traversal should be updated to allow both pre and post order callbacks. Here's a proof-of-concept that got worked out during the initial filtering work. It would need to be updated to include filtering, which would be trivial.

  // TODO incorporate this into regular traversal method
  function depthFirstTraverseWithPostorder(nodeActionPreorderCallback, nodeActionPostorderCallback) {

    if (treeModel.value.length === 0) {
      return;
    }

    let nodeQueue = treeModel.value.slice();
    let visited = [];  // OPTIMIZATION - Don't track this if no postorder callback is given (and also don't push current after preorder processing)

    while (nodeQueue.length > 0) {
      let current = nodeQueue.shift();

      if (!visited.includes(current)) {
        nodeActionPreorderCallback(current);
        visited.push(current);

        let childrenPropName = current.treeNodeSpec.childrenProperty;
        nodeQueue = [...current[childrenPropName], current].concat(nodeQueue);
      }
      else {
        // this is either a leaf or a parent whose children have all been processed
        nodeActionPostorderCallback(current);
      }
    }
  }
@grapoza grapoza added the enhancement New feature or request label Feb 10, 2023
@grapoza grapoza added this to To do in 5.3.0 via automation Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
5.3.0
To do
Development

No branches or pull requests

1 participant