Permalink
Browse files

Implemented Traverse

  • Loading branch information...
1 parent b8c26a1 commit 13ab05086d2ca68e7e37e5ad63d2fd8fd4431958 @dlidstrom committed Feb 13, 2012
Showing with 37 additions and 1 deletion.
  1. +19 −0 Words.Test/TernaryTreeTest.cs
  2. +18 −1 Words/TernaryTree.cs
@@ -78,5 +78,24 @@ public void NearSearch()
// Assert
Assert.AreEqual(2, matches.Count);
}
+
+ [TestMethod]
+ public void Traverse()
+ {
+ // Arrange
+ var tree = new TernaryTree(Language.Swedish);
+ tree.Add("abe", "abc", "abd");
+ var visited = new List<string>();
+
+ // Act
+ tree.Traverse(s => visited.Add(s));
+
+ // Assert
+ var expected = new List<string> { "abc", "abd", "abe" };
+ Assert.AreEqual(expected.Count, visited.Count);
+ Assert.AreEqual(expected[0], visited[0]);
+ Assert.AreEqual(expected[1], visited[1]);
+ Assert.AreEqual(expected[2], visited[2]);
+ }
}
}
View
@@ -164,8 +164,25 @@ public List<string> Matches(string s, int limit = 100)
/// traverse(p-&gt;hikid);
/// }
/// </code>
- public void Traverse()
+ public void Traverse(Action<string> action)
{
+ if (action == null)
+ throw new ArgumentNullException("action");
+
+ Traverse(root, action, string.Empty);
+ }
+
+ private void Traverse(Node node, Action<string> action, string s)
+ {
+ if (node == null)
+ return;
+
+ Traverse(node.Left, action, s);
+ if (node.WordEnd)
+ action.Invoke(s);
+ else
+ Traverse(node.Center, action, s + node.Char);
+ Traverse(node.Right, action, s);
}
public List<string> NearSearch(string s, int d = 1, int limit = 100)

0 comments on commit 13ab050

Please sign in to comment.