Skip to content
Permalink
Browse files

Fix bug in StrTrie - replace node on Add if not already terminal

  • Loading branch information...
bobbymcr committed May 26, 2019
1 parent e4ed5b3 commit b84a0fd37b2c534304cf2a27c5d8a3127063ec4d
Showing with 30 additions and 1 deletion.
  1. +1 −1 src/Words.Core/StrTrie.cs
  2. +16 −0 test/Words.Test/StrTrieTest.cs
  3. +13 −0 test/Words.Test/StringTrieTest.cs
@@ -83,7 +83,7 @@ public void Add(Str value)
return;
}

if (this.nodes.ContainsKey(value))
if (this.nodes.TryGetValue(value, out bool isTerminal) && isTerminal)
{
return;
}
@@ -86,6 +86,22 @@ public void AddNodesMultipleTimes()
trie.Find(Str.Parse("ABC")).Should().Be(StrTrie.NodeKind.Terminal);
}

[Fact]
public void AddNodesMultipleTimesLongerFirst()
{
StrTrie trie = new StrTrie();

trie.Add(Str.Parse("ABC"));
trie.Add(Str.Parse("ABC"));
trie.Add(Str.Parse("AB"));
trie.Add(Str.Parse("AB"));

trie.Count.Should().Be(2);
trie.Find(Str.Parse("A")).Should().Be(StrTrie.NodeKind.Prefix);
trie.Find(Str.Parse("AB")).Should().Be(StrTrie.NodeKind.Terminal);
trie.Find(Str.Parse("ABC")).Should().Be(StrTrie.NodeKind.Terminal);
}

[Fact]
public void AddEmptyNode()
{
@@ -91,6 +91,19 @@ public void AddNodesMultipleTimes()
trie.Count.Should().Be(2);
}

[Fact]
public void AddNodesMultipleTimesLongerFirst()
{
StringTrie trie = new StringTrie();

trie.Add("ABC");
trie.Add("ABC");
trie.Add("AB");
trie.Add("AB");

trie.Count.Should().Be(2);
}

[Fact]
public void AddEmptyNode()
{

0 comments on commit b84a0fd

Please sign in to comment.
You can’t perform that action at this time.