@marianmeres/tree
v2.2.5
Published
[](https://www.npmjs.com/package/@marianmeres/tree) [](https://jsr.io/@marianmeres/tree) [;
const root = tree.appendChild('root');
const child1 = root.appendChild('child1');
const child2 = root.appendChild('child2');
child1.appendChild('grandchild1');
child1.appendChild('grandchild2');
// Tree structure visualization
console.log(tree.toString());
// root
// child1
// grandchild1
// grandchild2
// child2
// Traversal (pre-order, post-order, level-order available)
for (const node of tree.preOrderTraversal()) {
console.log(node?.value);
}
// Search
const found = tree.find(child1.id); // by ID
const matches = tree.findAllBy('child1'); // by value
// Manipulation
tree.insert(child2.id, 'newChild'); // insert under child2
tree.move(child1.id, child2.id); // move child1 under child2
tree.remove(child2.id); // remove subtree
// Serialization
const json = tree.dump();
const restored = Tree.factory<string>(json);
// Readonly mode (all mutations throw)
const readonlyTree = Tree.factory<string>(json, true);API
See API.md for complete API documentation.
