NTree is a JavaScript library for creating and traversing n-ary tree structures.
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')
By importing ntree, you get access to the following classes:
Node
- For creating and manipulating node objects to be added to a treeTreeUtil
- 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.