mirror of https://github.com/deavmi/niknaks
Compare commits
2 Commits
943a16beb6
...
a5f7e9fc95
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | a5f7e9fc95 | |
Tristan B. Velloza Kildaire | 926598086d |
|
@ -872,26 +872,76 @@ unittest
|
|||
assert(!treeOfStrings.removeNode(subtree_1));
|
||||
}
|
||||
|
||||
/**
|
||||
* A kind-of a tree which has the ability
|
||||
* to linearize all of its nodes which
|
||||
* results in performing a depth first
|
||||
* search resulting in the collection of
|
||||
* all nodes into a single array with
|
||||
* elements on the left hand side being
|
||||
* the most leafiest (and left-to-right
|
||||
* on the same depth are in said order).
|
||||
*
|
||||
* It also marks a node as visited on
|
||||
* entry to it via the dfs call to it.
|
||||
*
|
||||
* When dfs is performed, a child node
|
||||
* is only recursed upon if it has not
|
||||
* yet been visited.
|
||||
*
|
||||
* With all this, it means a graph of
|
||||
* relations can be flattened into an
|
||||
* array.
|
||||
*/
|
||||
public class VisitationTree(T) : Tree!(T)
|
||||
{
|
||||
private bool visisted;
|
||||
|
||||
/**
|
||||
* Constructs a new node
|
||||
*
|
||||
* Params:
|
||||
* value = the value
|
||||
*/
|
||||
this(T value)
|
||||
{
|
||||
super(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the linearization
|
||||
*
|
||||
* Returns: the linearized list
|
||||
*/
|
||||
public T[] linearize()
|
||||
{
|
||||
return dfs(toDelegate(&_shouldVisit), toDelegate(&_touch));
|
||||
}
|
||||
|
||||
/**
|
||||
* The inclusion startergy
|
||||
*
|
||||
* Params:
|
||||
* tnode = the tree node
|
||||
* Returns: `true` if not
|
||||
* yet visited or incompatible
|
||||
* node type
|
||||
*/
|
||||
private static bool _shouldVisit(Tree!(T) tnode)
|
||||
{
|
||||
VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode;
|
||||
return vnode && !vnode.isVisited();
|
||||
}
|
||||
|
||||
/**
|
||||
* The touching stratergy
|
||||
*
|
||||
* Only works on compatible
|
||||
* tree nodes
|
||||
*
|
||||
* Params:
|
||||
* tnode = the tree node
|
||||
*/
|
||||
private static void _touch(Tree!(T) tnode)
|
||||
{
|
||||
VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode;
|
||||
|
@ -901,17 +951,31 @@ public class VisitationTree(T) : Tree!(T)
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks this node as
|
||||
* visited
|
||||
*/
|
||||
private void mark()
|
||||
{
|
||||
this.visisted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks this node has been
|
||||
* visited
|
||||
*
|
||||
* Returns: `true` if visited,
|
||||
* otherwise `false`
|
||||
*/
|
||||
private bool isVisited()
|
||||
{
|
||||
return this.visisted;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests out using the visitation tree
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
VisitationTree!(string) root = new VisitationTree!(string)("root");
|
||||
|
@ -925,5 +989,4 @@ unittest
|
|||
|
||||
assert(linearized[0] == "subtree");
|
||||
assert(linearized[1] == "root");
|
||||
|
||||
}
|
Loading…
Reference in New Issue