# tree-data-structure

> build tree data structure for javascript

Latest version **0.2.0** (published 2020-02-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install tree-data-structure
pnpm add tree-data-structure
yarn add tree-data-structure
bun add tree-data-structure
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-02-18 |
| First published | 2019-12-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 52.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | IERomanov |
| Maintainers | ieromanov |
| Keywords | tree, data-structure, node |

## Links

- npm: https://www.npmjs.com/package/tree-data-structure
- Repository: https://github.com/ieromanov/tree
- Issues: https://github.com/ieromanov/tree/issues
- npm.io page: https://npm.io/package/tree-data-structure

## Dependencies (1)

- [core-js](https://npm.io/package/core-js.md) ^3.6.4

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2020-02-18
- 0.1.0 — 2020-01-31
- 0.0.3 — 2019-12-02
- 0.0.2 — 2019-12-02
- 0.0.1 — 2019-12-01

## README

# TREE DATA STRUCTURE

javascript library for build tree data structure

## Install

```bash
npm i tree-data-structure
```

## Import

Browser

```js
import Tree from "tree-data-structure";

const tree = new Tree('root')
```

## API

### `tree.add()` - add item in tree

```js
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

```js
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

```js
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

```js
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

```js
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

```js
tree.search(data, options)
```

Argument | Type | Description | Required | Default
-------- | ---- | ----------- | -------- | -------
data | any | Data to be found | `true` | -
options | Object | Options for search | `false` | -

###### Options

Argument | Type | Description | Required | Default
-------- | ---- | ----------- | -------- | -------
key | String | Property key that stores the data to be searched | `false` | id
isDeepSearch | Boolean | use [deep search](https://en.wikipedia.org/wiki/Depth-first_search), if `true`, or [breadth search](https://en.wikipedia.org/wiki/Breadth-first_search) algorithm | `false` | `true`
onlyFirst | Boolean | search only first match | `false` | `false`

---
_Source: https://npm.io/package/tree-data-structure · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
