Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Trie and it's unit test. #13

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Algorithms/Algorithms.Tests/Trie/TrieTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Xunit;
using Algorithms.Trie;

namespace Algorithms.Tests
{
public class TrieTests
{
[Fact]
public void EmptyCheck()
{
var trie = new Algorithms.Trie.Trie();
Assert.Empty(trie.FindPrefix("h"));
}

[Fact]
public void InsertAndFindCheck()
{
var trie = new Algorithms.Trie.Trie();
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Equal(2, trie.FindPrefix("H").Count);
Assert.Equal(2, trie.FindPrefix("He").Count);
Assert.Single(trie.FindPrefix("Hel"));
Assert.Single(trie.FindPrefix("Hey"));
}

[Fact]
public void DuplicateInsertAndFindCheck()
{
var trie = new Algorithms.Trie.Trie();
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Equal(2, trie.FindPrefix("H").Count);
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Equal(2, trie.FindPrefix("H").Count);
trie.Insert("hello");
trie.Insert("hey");
Assert.Equal(2, trie.FindPrefix("h").Count);
Assert.Equal(2, trie.FindPrefix("H").Count);
}

[Fact]
public void CaseSensitiveFindCheck()
{
var trie = new Algorithms.Trie.Trie();
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Equal(2, trie.FindPrefix("H").Count);
Assert.Empty(trie.FindPrefix("h"));
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Empty(trie.FindPrefix("h"));
Assert.Equal(2, trie.FindPrefix("H").Count);
trie.Insert("hello");
trie.Insert("hey");
Assert.Equal(2, trie.FindPrefix("h").Count);
Assert.Equal(2, trie.FindPrefix("H").Count);
Assert.Equal(2, trie.FindPrefix("He").Count);
Assert.Single(trie.FindPrefix("Hel"));
Assert.Single(trie.FindPrefix("Hey"));
}

[Fact]
public void ClearCheck()
{
var trie = new Algorithms.Trie.Trie();
trie.Insert("Hello");
trie.Insert("Hey");
Assert.Equal(2, trie.FindPrefix("H").Count);
Assert.Empty(trie.FindPrefix("h"));
trie.Clear();
Assert.Empty(trie.FindPrefix("h"));
Assert.Empty(trie.FindPrefix("H"));
}
}
}
122 changes: 122 additions & 0 deletions Algorithms/Algorithms/Trie/Trie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Trie
{
public class Trie
{
private Dictionary<char, Trie> _children = new Dictionary<char, Trie>();
private bool _isWordCompleted = false;
private int _size = 0;

public void Insert(string s)
{
Insert(s, 0);
}

private void Insert(string s, int index)
{
_size++;
if (index == s.Length)
{
_isWordCompleted = true;
return;
}
char c = s[index];
Trie child = GetNode(c);
if (child == null)
{
child = new Trie();
InsertNode(c, child);
}
child.Insert(s, index + 1);
}

private Trie GetNode(char c)
{
if (_children.ContainsKey(c))
{
return _children[c];
}
return null;
}

private void InsertNode(char c, Trie node)
{
_children.Add(c, node);
}

public List<string> FindPrefix(string word)
{
var list = new List<string>();
if (string.IsNullOrEmpty(word))
return list;

var iterator = this;
var fetchedWord = new StringBuilder();
for (int i = 0; i < word.Length; i++)
{
var c = word[i];
if (!iterator._children.ContainsKey(c))
break;

fetchedWord.Append(c);
iterator = iterator._children[c];
}

if (iterator._isWordCompleted)
{
list.Add(fetchedWord.ToString());
}
if (fetchedWord.Length<1) return list;
var remainingWords = GetAllWordsFromTrie(iterator);
var preWord = fetchedWord.ToString();
var words = remainingWords.Select(w => $"{preWord}{w}");
list.AddRange(words);
return list;
}

private List<string> GetAllWordsFromTrie(Trie iterator)
{
var list = new List<string>();
if (iterator == null || !iterator._children.Any())
return list;

foreach (var iteratorChild in iterator._children)
{
var fetchedWord = new StringBuilder();
fetchedWord.Append(iteratorChild.Key);
if (iteratorChild.Value._isWordCompleted)
{
list.Add(fetchedWord.ToString());
}

var remaining = GetAllWordsFromTrie(iteratorChild.Value);
var preword = fetchedWord.ToString();
var res = remaining.Select(w => $"{preword}{w}");
list.AddRange(res);
}
return list;
}

public void Clear()
{
ClearNodes(this);
}

private void ClearNodes(Trie value)
{
int removedNodes = 0;
foreach (var keyValue in value._children)
{
ClearNodes(keyValue.Value);
removedNodes++;
}
_size -= removedNodes;
value._children.Clear();
}
}
}