Can do depth-first traversals like on this picture for abstract trees.
(source: https://en.wikipedia.org/wiki/Tree_traversal)
To run examples you'll need to have node and you'll need to run npm install
.
npm run tsfile examples/tree-from-image-example.ts
const treeData =
{
$d: 'F', $c: [
{
$d: 'B', $c: [
{ $d: 'A', $c: [] },
{
$d: 'D', $c: [
{ $d: 'C', $c: [] },
{ $d: 'E', $c: [] }]
}]
},
{
$d: 'G', $c: [
null,
{
$d: 'I', $c: [
{ $d: 'H', $c: [] }]
}]
}]
};
const traversal = new DepthFirstTraversal({
traversableTree: {
makeRoot: () => treeData,
makeVertex: (childHint: any) => childHint
}
});
traversal.addVisitorFor(DepthFirstTraversalOrder.PRE_ORDER, (vertex) => reportVisit(DepthFirstTraversalOrder.PRE_ORDER, vertex.getData()));
traversal.addVisitorFor(DepthFirstTraversalOrder.IN_ORDER, (vertex) => reportVisit(DepthFirstTraversalOrder.IN_ORDER, vertex.getData()));
traversal.addVisitorFor(DepthFirstTraversalOrder.POST_ORDER, (vertex) => reportVisit(DepthFirstTraversalOrder.POST_ORDER, vertex.getData()));
traversal.makeRunner().run();
Stdout
npm run tsfile examples/traversable-object-example.ts
npm run tsfile examples/traversable-object-example-disable-subtree-traversal.ts
Same as traversable-object-example.ts, but uses DISABLE_SUBTREE_TRAVERSAL
command on vertex B
.
npm run tsfile examples/traversable-object-example-rewrite-vertex-hints.ts
Same as traversable-object-example.ts, but
uses REWRITE_VERTEX_HINTS_ON_PRE_ORDER
command on vertex E
.
npm run tsfile examples/traversable-object-iterable-example.ts
npm run tsfile examples/rewrite-object-example.ts
Not tested beyond examples given and never yet applied to real-life problems.
If for some reason you want to use this as a dependency, please use fixed version (remove ^ from version
of configurable-tree-traversal
in package.json)
- Breadth-first
- Add self-reference test
- --
- Async traversal
- Implementation for FS (with a way to keep fs and tree in sync)
- Unit tests
- Freeze data at some point? make trees/vertices immutable?
- Benchmarks
- Breadth-first traversal