TreeReader

To make reading a Tree (made up of TreeNode) a bit easier

and while using it, make sure you do not make a loop in TreeNodes by putting a parent or parent's parent in a node's childNodes, doing so will cause an infinite loop, TreeReader cannot currently handle this

Members

Functions

clear
void clear()

.destroy()s children of the root, including children of children and so on, the root is also .destroy-ed

count
uinteger count()

counts and returns number of nodes in the tree

count
uinteger count(bool function(TreeNode!(T)*) doCount)

counts and returns number of nodes in the tree

iterate
void iterate(bool function(TreeNode!(T)*) func)

calls a function on every node

iterate
void iterate(bool delegate(TreeNode!(T)*) func)

calls a delegate on every node

Variables

root
TreeNode!(T)* root;

the root node

Examples

TreeReader!int tree;
// testing iterate
// make a sample tree
TreeNode!int rootNode;
rootNode.data = 0;
// childNodes of root
TreeNode!int rootChild0 = TreeNode!int(1), rootChild1=TreeNode!int(2);
// childNodes of rootChild0
TreeNode!int child0child0 = TreeNode!int(3), child0child1 = TreeNode!int(4);
// childNodes of rootChild1
TreeNode!int child1child0 = TreeNode!int(5), child1child1 = TreeNode!int(6);
// arrange them in a tree
rootNode.childNodes = [&rootChild0, &rootChild1];
rootChild0.childNodes = [&child0child0, &child0child1];
rootChild1.childNodes = [&child1child0, &child1child1];
tree.root = &rootNode;
// check if iterate's working
int[] iteratedNodes;
tree.iterate((TreeNode!(int)* node){iteratedNodes ~= (*node).data; return true;});
// make sure each number was iterated
assert ([0,1,2,3,4,5,6].matchElements(iteratedNodes), "TreeReader.iterate did not iterate through all nodes");
/// now test count
assert (tree.count == 7, "TreeReader.count returned invalid count");
/// thats all unit tests for now, so destroy all nodes now
tree.clear;

Meta