javascript library for build tree data structure
npm i tree-data-structure
Browser
import Tree from "tree-data-structure";
const tree = new Tree('root')
tree.add(data, parent, addAllByOne)
Argument | Type | Description | Required | Default |
---|---|---|---|---|
data | any | Data that the tree node will store | true |
- |
parent | Node | Parent node that will store the new node | true |
- |
addAllByOne | Boolean | If the array is passed and it is true , all items of the array are added as separate nodes |
false |
false |
The first argument is the data that the tree node will store The second argument is the parent node that will store the new node
import Tree from "tree-data-structure";
const tree = new Tree('root')
tree.add('child', tree.root)
/*
output tree root node
{
data: 'root',
children: [{
data: 'child',
children: []
}]
}
*/
Third argument is optional. If you pass an array of data as the first argument and pass true
as the third argument, each element of the array will be added as a separate node
import Tree from "tree-data-structure";
const tree = new Tree('root')
tree.add(['one', 'two', 'three'], tree.root, true)
/*
output tree root node
{
data: 'root',
children: [
{
data: 'one',
children: []
},
{
data: 'two',
children: []
},
{
data: 'three',
children: []
}
]
}
*/
You can pass any type of data.
If you pass Object
, his properties overwrite in node
import Tree from "tree-data-structure";
const tree = new Tree('root')
tree.add({ one: 1, two: 2, three: 3 }, tree.root)
/*
output tree root node
{
data: 'root',
children: [
{
one: 1,
two: 2,
three: 3,
children: []
}
]
}
*/
import Tree from "tree-data-structure";
const tree = new Tree('root')
const node = tree.add({ one: 1, two: 2, three: 3 }, tree.root)
tree.remove(node)
/*
output tree root node
{
data: 'root',
children: []
}
*/
tree.search(data, options)
Argument | Type | Description | Required | Default |
---|---|---|---|---|
data | any | Data to be found | true |
- |
options | Object | Options for search | false |
- |
Argument | Type | Description | Required | Default |
---|---|---|---|---|
key | String | Property key that stores the data to be searched | false |
id |
isDeepSearch | Boolean | use deep search, if true , or breadth search algorithm |
false |
true |
onlyFirst | Boolean | search only first match | false |
false |