|
| 1 | +# @itpropro/tree-structure-ts |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This module help interacting with `Tree` structures. 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 ones. |
| 6 | +It is fully typed and has over 95% test coverage. |
| 7 | + |
| 8 | +- Zero dependency |
| 9 | +- Fully typed |
| 10 | +- Fast |
| 11 | +- No recursion -> no memory overflows |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +To install the module, run the following command: |
| 16 | + |
| 17 | +```bash |
| 18 | +pnpm install @itpropro/tree-structure-ts |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +To create a new `Tree` instance, use the `Tree` constructor: |
| 24 | + |
| 25 | +```typescript |
| 26 | +const tree = new Tree('root') |
| 27 | +const root = tree.root |
| 28 | +``` |
| 29 | + |
| 30 | +To add a child node to a TreeNode, use the addChild method: |
| 31 | + |
| 32 | +```typescript |
| 33 | +const child1 = root.addChild('child1') |
| 34 | +const child2 = root.addChild('child2') |
| 35 | +``` |
| 36 | + |
| 37 | +To get all nodes in the tree below a TreeNode, use the all method: |
| 38 | + |
| 39 | +```typescript |
| 40 | +const nodes = root.all() |
| 41 | +``` |
| 42 | + |
| 43 | +To traverse a tree, use the traverse method: |
| 44 | + |
| 45 | +```typescript |
| 46 | +root.traverse((node) => { |
| 47 | + // This function is called for each node in the tree |
| 48 | +}) |
| 49 | +``` |
| 50 | + |
| 51 | +You can specify the traversal order by passing one of the following values to the traverse method: |
| 52 | + |
| 53 | +- breadthFirst (the default): visits nodes in breadth-first order |
| 54 | +- depthFirst: visits nodes in depth-first order |
| 55 | +- preOrder: visits the current node, then traverses the left subtree, then traverses the right subtree |
| 56 | +- postOrder: traverses the left subtree, then traverses the right subtree, then visits the current node |
| 57 | + |
| 58 | +for all avalaible methods and fields, please read the detailed documentation of the `Tree` and `TreeNode` class: [Class docs](modules.md). |
| 59 | + |
| 60 | +## Contribution |
| 61 | + |
| 62 | +See [Contributing Guide](https://github.com/itpropro/tree-structure-ts/blob/main/CONTRIBUTING.md). |
| 63 | + |
| 64 | +## License |
| 65 | + |
| 66 | +Made with :heart: |
| 67 | + |
| 68 | +Published under [MIT License](./LICENCE). |
0 commit comments