Table of contents
A tree node-based implementation in JavaScript.
Set an initial structure.
╭─────╮
│ 0 │
╰─────╯
↓
╭─────╮ ╭─────╮
│ 1 │ → │ 2 │
╰─────╯ ╰─────╯
↓
╭─────╮ ╭─────╮
│ 3 │ → │ 4 │
╰─────╯ ╰─────╯
Create and connect nodes.
const nodes = [
new Node({ id: 0, content: 'zero' }),
new Node({ id: 1, content: 'one' }),
new Node({ id: 2, content: 'two' }),
new Node({ id: 3, content: 'three' }),
new Node({ id: 4, content: 'four' })
]
nodes[0].push(
nodes[1],
nodes[2].push(
nodes[3],
nodes[4]
)
)
const node = nodes[0]Some actions.
nodes[4].root() // Node(id: 0)nodes[0].leaves() // [Node(id: 1), Node(id: 3), Node(id: 4)]nodes[1].siblings() // [Node(id: 2)]nodes[1].index() // 0nodes[1].next() // Node(id: 2)nodes[4].lineage() // [Node(id: 4), Node(id: 2), Node(id: 0)]nodes[4].depth() // 2nodes[0].height() // 2nodes[1].breadth() // 2Array.from(node) // [Node(id: 0), Node(id: 1), Node(id: 2), Node(id: 3), Node(id: 4)]See Iterators and generators for more details.
Array.from(node).sort(Node.compare())
// ⇒ [
// Node(id: 4, content: "four"),
// Node(id: 1, content: "one"),
// Node(id: 3, content: "three"),
// Node(id: 2, content: "two"),
// Node(id: 0, content: "zero")
// ]Using a custom compareFunction and mapNode function:
const compareFunction = (value, otherValue) => {
return value > otherValue ? -1 : 1
}
const mapNode = (node) => {
return node.content
}
Array.from(node).sort(Node.compare(compareFunction, mapNode))
// ⇒ [
// Node(id: 0, content: "zero"),
// Node(id: 2, content: "two"),
// Node(id: 3, content: "three"),
// Node(id: 1, content: "one"),
// Node(id: 4, content: "four")
// ]See Comparison operators for more details.
{
id,
content,
nodes
}Each node has an ID, a content and a list of nodes.
Converts Node to Object.
Builds an Object from a tree representation.
const object = node.encode()Output:
{
id: 0,
content: "zero",
nodes: [
{
id: 1,
content: "one",
nodes: []
},
{
id: 2,
content: "two",
nodes: [
{
id: 3,
content: "three",
nodes: []
},
{
id: 4,
content: "four",
nodes: []
}
]
}
]
}The method has an optional constructor parameter to build an Object with a different pattern than the internal representation.
const customObject = node.encode((node, encode) => ({
customId: node.id,
customContent: node.content,
customNodes: encode()
}))Output:
{
customId: 0,
customContent: "zero",
customNodes: [
{
customId: 1,
customContent: "one",
customNodes: []
},
{
customId: 2,
customContent: "two",
customNodes: [
{
customId: 3,
customContent: "three",
customNodes: []
},
{
customId: 4,
customContent: "four",
customNodes: []
}
]
}
]
}Converts Object to Node.
Builds a tree from an Object representation.
const nodeFromObject = Node.parse(object)The method has an optional constructor parameter to build a tree with a different pattern than the internal representation.
const nodeFromCustomObject = Node.parse(customObject, (object) => ({
id: object.customId,
content: object.customContent,
nodes: object.customNodes
}))We’re back to the initial node representation.
You can build and parse a JSON tree with #toJSON(constructor) and Node#fromJSON(json, constructor).
The methods are small wrappers around #encode() and Node#parse().