New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tree-data-structure

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-data-structure

build tree data structure for javascript

latest
Source
npmnpm
Version
0.2.0
Version published
Maintainers
1
Created
Source

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

Keywords

tree

FAQs

Package last updated on 18 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts