mirror of https://github.com/deavmi/niknaks
VisitationTree
- Working on a visitation tree implementation
This commit is contained in:
parent
f49ecf1908
commit
167c2b7c0d
|
@ -727,4 +727,56 @@ unittest
|
||||||
|
|
||||||
string[] result = treeOfStrings.dfs(strat, touch);
|
string[] result = treeOfStrings.dfs(strat, touch);
|
||||||
writeln("dfs: ", result);
|
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());
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue