id-tree
is a lightweight TypeScript library for managing tree data structures where nodes contain only a unique numerical identifier (ID). The library provides an easy-to-use API to create, modify, and traverse tree structures, as well as methods for serializing and deserializing trees in compact formats.
- Create and manage tree structures with ease
- Add and remove nodes using unique IDs
- Find and move nodes within the tree
- Serialize and deserialize tree structures in compact array and object formats
- Set and manage leaf-only nodes
- Compatible with TypeScript and JavaScript projects
To install the id-tree
library, run the following command:
npm install id-tree
Import the Tree class from the id-tree package:
import { Tree } from 'id-tree';
Create a new instance of the Tree class:
const tree = new Tree();
To set the root node of the tree, use the setRoot method:
const rootNode = tree.setRoot(1);
To add a new node to the tree, use the addNode method:
const newNode = tree.addNode(parentId, nodeId);
To find a node with a specific ID, use the findNode method:
const node = tree.findNode(nodeId);
To move a node (and its subtree) to a new parent at a specified index, use the move method:
tree.move(nodeId, newParentId, index);
To set leaf-only nodes that cannot have children, use the setLeafs method:
tree.setLeafs([leafId1, leafId2, ...]);
To serialize the tree in a compact array format, use the toArray method:
const compactArray = tree.toArray();
To serialize the tree in a compact object format, use the toObj method:
const compactObj = tree.toObj();
To deserialize a tree from a compact array format, use the fromArray method:
const restoredTree = Tree.fromArray(compactArray);
To deserialize a tree from a compact object format, use the fromObj method:
const restoredTree = Tree.fromObj(compactObj);
To deserialize a tree from a map of nodes, use the fromMap method:
let rootId = 100;
let pairs = [[100, 1], [1, 2]]
const tree = Tree.fromMap([rootId, pairs]);
Contributions are welcome! Please submit a pull request or create an issue on the project's GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.
If you need assistance or have any questions, please create an issue on the project's GitHub repository.
This library - the code, test cases, and this documentation - were all written by ChatGPT. Atleast the v1.
I used the following prompts to build this library (the main ones) -
can you write an ordered tree implementation in typescript where the only data contained in each node is the "id" of type number?
For the above implementation, how to restore a tree from a JSON?
Now in the above implementation, include the following methods -
- setLeafs - here you pass and array of id's that cannot have children, this is leaf only nodes
- fixTree - if the tree has any node with id in the leaf only array, it should move all children to the parent Add validation in addNode to throw error if it is being added to a lead only node
I need couple of methods to store this tree and restore this tree -
- toCompact - this provides a compact representation of this tree to store in database
- fromCompact - this recreates the tree from a compact data representation
include following method - move(node_id, parent_id, index) - this method moves the entire sub tree of node with id node_id to the node with id=parent_id at the index position of its children. If index is undefined, add as last child.
Combine all the functionality mentioned above and write out the complete code
Following is the complete code of what you implemented above, please write a comprehensive set of test cases using the jest library
continue writing the missing test cases
The test case "Compact array representation with leaf-only nodes" is failing because the leafs are not being stored when compacted. Can you find the problem in my code?
Can you fix the same issue with this test case - 'Compact object representation with leaf-only nodes'
seems I don't need to store the leafs when i compact. can you write the toArray, fromArray, toObj and fromObj so it does not store that
if i want to publish this tree implementation as an npm package, list few good names for it
I am going to call this
id-tree
. Write me a README.md documentation for this library