https://github.com/itpropro/tree-structure-ts
Library for creating and interacting with tree structures, written in TypeScript.
https://github.com/itpropro/tree-structure-ts
Last synced: 4 months ago
JSON representation
Library for creating and interacting with tree structures, written in TypeScript.
- Host: GitHub
- URL: https://github.com/itpropro/tree-structure-ts
- Owner: itpropro
- License: mit
- Created: 2022-12-07T11:46:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-15T18:52:09.000Z (over 1 year ago)
- Last Synced: 2025-04-11T00:06:36.545Z (about 1 year ago)
- Language: TypeScript
- Size: 617 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# @itpropro/tree-structure-ts
[](https://www.npmjs.com/package/@itpropro/tree-structure-ts)

[](https://github.com/itpropro/tree-structure-ts/actions/workflows/ci.yml)

## Introduction
This module helps interacting with `Tree` structures in TypeScript. It is optimized to work with big trees without causing overflows. Therefore it doesn't use recursion and the implementations for `preOrder` and `postOrder` traversals use `Promise.all` for concurrency to traverse multiple nodes at once.
It is fully typed and has over 95% test coverage.
- Zero dependency
- Fully typed
- Optimized for big trees
- No recursion -> no memory overflows
- Small bundle size
## Quick Start
### Installation
To install the module, run the following command:
```bash
# bun
bun add @itpropro/tree-structure-ts
# npm
npm install @itpropro/tree-structure-ts
# yarn
yarn add @itpropro/tree-structure-ts
```
### Import
```typescript
// ESM / TypeScript
import { Tree } from '@itpropro/tree-structure-ts'
import type { TreeNode } from '@itpropro/tree-structure-ts'
```
## Usage
To create a new `Tree` instance, use the `Tree` constructor:
```typescript
const tree = new Tree('root')
const root = tree.root
```
To add a child node to a TreeNode, use the `addChild` method:
```typescript
const child1 = root.addChild('child1')
const child2 = root.addChild('child2')
```
To get all nodes in the tree below a TreeNode, use the `all` method:
```typescript
const nodes = root.all()
```
To traverse a tree, use the `traverse` method:
```typescript
root.traverse((node) => {
// This function is called for each node in the tree
})
```
You can specify the traversal order by passing one of the following values to the `traverse` method:
- breadthFirst (the default): visits nodes in breadth-first order
- depthFirst: visits nodes in depth-first order
- preOrder: visits the current node, then traverses the left subtree, then traverses the right subtree
- postOrder: traverses the left subtree, then traverses the right subtree, then visits the current node
For all available methods and fields, please read the detailed documentation of the `Tree` and `TreeNode` classes: [Class docs](https://github.com/itpropro/tree-structure-ts/blob/main/docs/modules.md).
## Contribution
See [Contributing Guide](https://github.com/itpropro/tree-structure-ts/blob/main/CONTRIBUTING.md).
## License
Made with :heart:
Published under [MIT License](./LICENSE).