VisitationTree

- Working on a visitation tree implementation
This commit is contained in:
Tristan B. Velloza Kildaire 2024-04-23 15:49:49 +02:00
parent f49ecf1908
commit 167c2b7c0d
1 changed files with 52 additions and 0 deletions

View File

@ -727,4 +727,56 @@ unittest
string[] result = treeOfStrings.dfs(strat, touch);
writeln("dfs: ", result);
}
public class VisitationTree(T) : Tree!(T)
{
private bool visisted;
this(T value)
{
super(value);
}
public T[] linearize()
{
return dfs(toDelegate(&_shouldVisit), toDelegate(&_touch));
}
private static bool _shouldVisit(Tree!(T) tnode)
{
VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode;
return vnode && !vnode.isVisited();
}
private static void _touch(Tree!(T) tnode)
{
VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode;
if(vnode)
{
vnode.mark();
}
}
private void mark()
{
this.visisted = true;
}
private bool isVisited()
{
return this.visisted;
}
}
unittest
{
VisitationTree!(string) root = new VisitationTree!(string)("root");
VisitationTree!(string) thing = new VisitationTree!(string)("subtree");
root.appendNode(thing);
thing.appendNode(root);
writeln(root.linearize());
}