private static bool validateSuffixStrings( SuffixTree tree, out List <int> failedLeafNodeIds) { Stack <Tuple <StEdge, string> > edgeStrings = new Stack <Tuple <StEdge, string> >(); // Step 1: Populate edgeStrings with data from child edges of the root node. // Track any leaves that are immedage children of the root node. List <Tuple <StEdge, string> > leafEdgeStrings = new List <Tuple <StEdge, string> >(); foreach (StEdge edge in tree.Root.ChildEdges()) { Tuple <StEdge, string> edgeString = new Tuple <StEdge, string>(edge, tree.EdgeSubstring(edge)); edgeStrings.Push(edgeString); if (!edge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format("SuffixTreeTest: Found a leaf: {0:s}", edgeString.Item2)); leafEdgeStrings.Add(edgeString); } } // Step 2: Walk the tree, adding the remaining edges. Keep track of leaf edges. while (edgeStrings.Count > 0) { Tuple <StEdge, string> edgeString = edgeStrings.Pop(); foreach (StEdge childEdge in edgeString.Item1.ChildNode.ChildEdges()) { Tuple <StEdge, string> newEdgeString = new Tuple <StEdge, string>( childEdge, edgeString.Item2 + tree.EdgeSubstring(childEdge)); edgeStrings.Push(newEdgeString); if (!childEdge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format("SuffixTreeTest: Found a leaf: {0:s}", newEdgeString.Item2)); leafEdgeStrings.Add(newEdgeString); } } } // Step 3: Inspect the leaf edge data. Keep track of failed leaf nodes failedLeafNodeIds = new List <int>(); foreach (var leafEdgeString in leafEdgeStrings) { // Accumulated string should equal the corresponding substring of tree.Text. int len = leafEdgeString.Item2.Length; string pathStr = leafEdgeString.Item2; string textStr = tree.RangeString(tree.Text.Length - len, tree.Text.Length - 1); string formatSpec = "{0," + tree.Text.Length.ToString() + ":s}"; string formatStr = String.Format( "SuffixTreeTest: About to compare \"{0:s}\" with \"{1:s}\"", formatSpec, formatSpec); Console.WriteLine(formatStr, pathStr, textStr); if (pathStr != textStr) { failedLeafNodeIds.Add(leafEdgeString.Item1.ChildNode.Id); } } return(failedLeafNodeIds.Count() == 0); }
private static bool validateSuffixStrings( SuffixTree tree, out List<int> failedLeafNodeIds) { Stack<Tuple<StEdge, string>> edgeStrings = new Stack<Tuple<StEdge, string>>(); // Step 1: Populate edgeStrings with data from child edges of the root node. // Track any leaves that are immedage children of the root node. List<Tuple<StEdge, string>> leafEdgeStrings = new List<Tuple<StEdge, string>>(); foreach (StEdge edge in tree.Root.ChildEdges()) { Tuple<StEdge, string> edgeString = new Tuple<StEdge, string>(edge, tree.EdgeSubstring(edge)); edgeStrings.Push(edgeString); if (!edge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format("SuffixTreeTest: Found a leaf: {0:s}", edgeString.Item2)); leafEdgeStrings.Add(edgeString); } } // Step 2: Walk the tree, adding the remaining edges. Keep track of leaf edges. while (edgeStrings.Count > 0) { Tuple<StEdge, string> edgeString = edgeStrings.Pop(); foreach (StEdge childEdge in edgeString.Item1.ChildNode.ChildEdges()) { Tuple<StEdge, string> newEdgeString = new Tuple<StEdge, string>( childEdge, edgeString.Item2 + tree.EdgeSubstring(childEdge)); edgeStrings.Push(newEdgeString); if (!childEdge.ChildNode.HasChildEdges()) { Console.WriteLine(String.Format("SuffixTreeTest: Found a leaf: {0:s}", newEdgeString.Item2)); leafEdgeStrings.Add(newEdgeString); } } } // Step 3: Inspect the leaf edge data. Keep track of failed leaf nodes failedLeafNodeIds = new List<int>(); foreach (var leafEdgeString in leafEdgeStrings) { // Accumulated string should equal the corresponding substring of tree.Text. int len = leafEdgeString.Item2.Length; string pathStr = leafEdgeString.Item2; string textStr = tree.RangeString(tree.Text.Length - len, tree.Text.Length - 1); string formatSpec = "{0," + tree.Text.Length.ToString() + ":s}"; string formatStr = String.Format( "SuffixTreeTest: About to compare \"{0:s}\" with \"{1:s}\"", formatSpec, formatSpec); Console.WriteLine(formatStr, pathStr, textStr); if (pathStr != textStr) { failedLeafNodeIds.Add(leafEdgeString.Item1.ChildNode.Id); } } return (failedLeafNodeIds.Count() == 0); }