Skip to content

Commit

Permalink
Fix #559: Add Trie [c#] (#592)
Browse files Browse the repository at this point in the history
* Implemented Trie C#

* Added check mark under Trie c#

* Fixed syntax
  • Loading branch information
DimaMukhin authored and singhpratyush committed Oct 28, 2017
1 parent dca5013 commit a30d9fa
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | | [:white_check_mark:](binary_search_tree/BinarySearchTree.java) | | [:white_check_mark:](binary_search_tree/binary_search_tree.go) | | |
| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | [:white_check_mark:](linked_list/LinkedList.cs) |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | [:white_check_mark:](stack/Stack.cs) |
| [Trie](https://en.wikipedia.org/wiki/Trie) | | [:white_check_mark:](trie/trie.cpp) | [:white_check_mark:](trie/Trie.java) | | [:white_check_mark:](trie/trie.go) | | | |
| [Trie](https://en.wikipedia.org/wiki/Trie) | | [:white_check_mark:](trie/trie.cpp) | [:white_check_mark:](trie/Trie.java) | | [:white_check_mark:](trie/trie.go) | | [:white_check_mark:](trie/Trie.cs) | |



Expand Down
90 changes: 90 additions & 0 deletions trie/Trie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;

class TrieNode
{
public char c;
public Dictionary<char, TrieNode> children;
public bool isWord;

public TrieNode()
{
children = new Dictionary<char, TrieNode>();
}

public TrieNode(char c)
{
this.c = c;
children = new Dictionary<char, TrieNode>();
}
}

public class Trie
{
private TrieNode root;

public Trie()
{
root = new TrieNode();
}

/// <summary>
/// Inserts a word into the Trie
/// Time Complexity: O(len(word))
/// </summary>
/// <param name="word">The word to insert</param>
public void Insert(String word)
{
TrieNode curr = root;

foreach (char c in word)
{
if (!curr.children.ContainsKey(c))
curr.children[c] = new TrieNode(c);

curr = curr.children[c];
}

curr.isWord = true;
}

/// <summary>
/// Searches the trie for a word
/// Time Complexity: O(len(word))
/// </summary>
/// <param name="word">The word to search for</param>
/// <returns>True if the word was found, false otherwise</returns>
public bool Search(String word)
{
TrieNode curr = root;

foreach (char c in word)
{
if (!curr.children.ContainsKey(c))
return false;

curr = curr.children[c];
}

return curr.isWord;
}

public static void Main()
{
Trie dictionary = new Trie();
// Input keys words
dictionary.Insert("the");
dictionary.Insert("a");
dictionary.Insert("there");
dictionary.Insert("answer");
dictionary.Insert("any");
dictionary.Insert("by");
dictionary.Insert("bye");
dictionary.Insert("their");
// Search for different keys words
Console.WriteLine(dictionary.Search("the"));
Console.WriteLine(dictionary.Search("these"));
Console.WriteLine(dictionary.Search("thaw"));
Console.WriteLine(dictionary.Search("their"));
}
}

0 comments on commit a30d9fa

Please sign in to comment.