Skip to content

AngelAngelov/iterate-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
    }
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published