public void TraverseTest()
{
DecisionTree tree;
int[][] inputs;
int[] outputs;
ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs);
{
DecisionNode[] expected =
{
tree.Root,
tree.Root.Branches[0], // Outlook = 0
tree.Root.Branches[1], // Outlook = 1
tree.Root.Branches[2], // Outlook = 2
tree.Root.Branches[0].Branches[0], // Humidity = 0
tree.Root.Branches[0].Branches[1], // Humidity = 1
tree.Root.Branches[2].Branches[0], // Wind = 0
tree.Root.Branches[2].Branches[1], // Wind = 1
};
int i = 0;
foreach (var node in tree.Traverse(DecisionTreeTraversal.BreadthFirst))
{
Assert.AreEqual(expected[i++], node);
}
Assert.AreEqual(expected.Length, i);
}
{
DecisionNode[] expected =
{
tree.Root,
tree.Root.Branches[0], // Outlook = 0
tree.Root.Branches[0].Branches[0], // Humidity = 0
tree.Root.Branches[0].Branches[1], // Humidity = 1
tree.Root.Branches[1], // Outlook = 1
tree.Root.Branches[2], // Outlook = 2
tree.Root.Branches[2].Branches[0], // Wind = 0
tree.Root.Branches[2].Branches[1], // Wind = 1
};
int i = 0;
foreach (var node in tree.Traverse(DecisionTreeTraversal.DepthFirst))
{
Assert.AreEqual(expected[i++], node);
}
Assert.AreEqual(expected.Length, i);
}
{
DecisionNode[] expected =
{
tree.Root.Branches[0].Branches[0], // Humidity = 0
tree.Root.Branches[0].Branches[1], // Humidity = 1
tree.Root.Branches[0], // Outlook = 0
tree.Root.Branches[1], // Outlook = 1
tree.Root.Branches[2].Branches[0], // Wind = 0
tree.Root.Branches[2].Branches[1], // Wind = 1
tree.Root.Branches[2], // Outlook = 2
tree.Root,
};
int i = 0;
foreach (var node in tree.Traverse(DecisionTreeTraversal.PostOrder))
{
Assert.AreEqual(expected[i++], node);
}
Assert.AreEqual(expected.Length, i);
}
}