@coutcout/tree-datastructure
v1.0.11
Published
This package can be used for creating a tree datastructure where a node can possess as many nodes as possible. This is a simple tree structure with only basics functions (getChilds/getParent).
Downloads
333
Maintainers
Readme
tree-datastructure
This package allow creating simple tree datastructures.
Installation
npm install @coutcout/tree-datastructure
Use
Create a tree
let tree = new Tree("This is the root");
Get the root
let root = tree.getRoot();
Add a node to the tree
let node1 = root.addChild("I'm the first childNode");
let node11 = node1.addChild("Hi");
let node2 = root.addChild("I'm the second child");
let node3 = new TreeNode("Bye bye");
root.addChildTreeNode(node3);
Result
digraph G {
root -> node1
root -> node2
root -> node3
node1 -> node11
}
Get a parent
let parent1 = node11.getParent(1);
// parent = root
let parent2 = node11.getParent();
// parent = node1
Get childs
let childs = root.getChilds();
// childs = [node1, node2, node3]