iterate-tree

2.0.1 • Public • Published

iterate-tree

npm

Iterate tree object by given propety name and callback.

Methods implement Depth-first search(DFS) and Breadth-first search(BFS) algorithms for traversing or searching.

Install

npm install iterate-tree

Traversing

var iterator = require('iterate-tree')
 
//Breadth-first search(BFS)
iterator.bfs(treeObject, 'propName', (obj) => {
 
    // Do something with the object
 
});
 
//Depth-first search(DFS)
iterator.dfs(treeObject, 'propName', (obj) => {
 
    // Do something with the object
 
});

Searching

If method is used as search of an object in the tree structure - RETURN FALSE in the callback breaks the search when it is needed.

var iterator = require('iterate-tree')
 
//Breadth-first search(BFS)
iterator.bfs(treeObject, 'propName', (obj) => {
    if(obj == condition){
        
        // Do something with the found object
 
        return false; // finish the search
    }
});
 
//Depth-first search(DFS)
iterator.dfs(treeObject, 'propName', (obj) => {
    if(obj == condition){
        
        // Do something with the found object
 
        return false; // finish the search
    }
});

Package Sidebar

Install

npm i iterate-tree

Weekly Downloads

12

Version

2.0.1

License

MIT

Last publish

Collaborators

  • angel.s.angelov