Skip to content

NTree is a JavaScript library for creating and traversing n-ary tree structures

License

Notifications You must be signed in to change notification settings

nitoygo/ntree.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NTree

NTree is a JavaScript library for creating and traversing n-ary tree structures.

Installation

Node

NTree is available as an npm module so you can install it with npm install ntree.js and use it in your code:

const ntree = require('ntree.js')

Usage

By importing ntree, you get access to the following classes:

  • Node - For creating and manipulating node objects to be added to a tree
  • TreeUtil - For utility functions necessary to do something to a tree
const {Node, TreeUtil} = require('ntree.js')
:
let aNode = new Node("A")
let bNode = new Node("B")
:
aNode.addChild(bNode)
:

To traverse a tree, get an iterable property of a root node and consume the iterator:

:
let iteratable = TreeUtil.getPreOrderIterable(aNode)
let iterator = iterable[Symbol.iterator]()

console.log(iterator.next())
console.log(iterator.next())
:

Or simply use for of

:
let iterable = TreeUtil.getPreOrderIterable(aNode)
for (node of iterable) {
  console.log(node)
}

As of this version, only getPreOrderIterable() and getDepthFirstIterable() is supported.
Other API add-ons to this library are currently under development.

About

NTree is a JavaScript library for creating and traversing n-ary tree structures

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published