0.2.0 • Published 4 years ago

tree-data-structure v0.2.0

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

TREE DATA STRUCTURE

javascript library for build tree data structure

Install

npm i tree-data-structure

Import

Browser

import Tree from "tree-data-structure";

const tree = new Tree('root')

API

tree.add() - add item in tree

tree.add(data, parent, addAllByOne)
ArgumentTypeDescriptionRequiredDefault
dataanyData that the tree node will storetrue-
parentNodeParent node that will store the new nodetrue-
addAllByOneBooleanIf the array is passed and it is true, all items of the array are added as separate nodesfalsefalse

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: []
		}
	]
 }
*/

tree.remove() - remove item from tree

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() - search in tree

tree.search(data, options)
ArgumentTypeDescriptionRequiredDefault
dataanyData to be foundtrue-
optionsObjectOptions for searchfalse-
Options
ArgumentTypeDescriptionRequiredDefault
keyStringProperty key that stores the data to be searchedfalseid
isDeepSearchBooleanuse deep search, if true, or breadth search algorithmfalsetrue
onlyFirstBooleansearch only first matchfalsefalse
0.2.0

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago