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 The Trie Data Structure #46

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions src/main/java/com/github/pedrovgs/trie/Trie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.pedrovgs.trie;

import java.util.HashMap;
import java.util.Map;
/**
* In computer science, a trie, also called digital tree, radix tree or prefix tree
* is a kind of search tree—an ordered tree data structure used to store a dynamic
* set or associative array where the keys are usually strings. Unlike
* a binary search tree, no node in the tree stores the key associated
* with that node; instead, its position in the tree defines the key with which it is associated.
* All the descendants of a node have a common prefix of the string associated with that node,
* and the root is associated with the empty string. Keys tend to be associated with leaves, though
* some inner nodes may correspond to keys of interest.
**/
/**
* @author Kashish Soni.
*/

public class Trie {
public boolean isEndOfWord; // used to checkif a string ends here
public Map<Character, Trie> m;

public Trie() {
m = new HashMap<Character, Trie>(); // Hashmap used to link char values to Trie Nodes
}

public Trie trieCreateNode() {
Trie t = new Trie();
t.isEndOfWord = false; // End of Word false by default
return t;
}

public void insertIntoTrie(Trie root, String s) { // Function to insert a String into the Trie
for (int i = 0; i < s.length(); i++) {
if (!root.m.containsKey(s.charAt(i))) { // Checks if The Trie already contains that character or not
root.m.put(s.charAt(i), new Trie()); // if not then create a new node for that character
}
root = root.m.get(s.charAt(i)); // root iterates through the trie
}
root.isEndOfWord = true; // denotes end of a string/pattern
}

public boolean searchInTrie(Trie root, String s) {
for (int i = 0; i < s.length(); i++) {
if (!(root.m.containsKey(s.charAt(i)))) { // if a node does not exist in the trie
return false; // then return not found
}
root = root.m.get(s.charAt(i));
}
if (root.isEndOfWord) { // The pattern may exist partially in // <- |
return true; // another pattern but this does not // <- | Can be removed to
} // mean that the search is complete as// <- | implement pattern in pattern search
return false; // word endings may not match // <- | change to return true;
}

}
47 changes: 47 additions & 0 deletions src/test/java/com/github/pedrovgs/trie/TrieTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.pedrovgs.trie;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Kashish Soni.
*/

public class TrieTest {
@Before
public void setUp() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is work without this method, is not it?

new TrieTest();
}
@Test public void shouldReturnTrueIfStringIsFound() {
ArrayList<String> s = new ArrayList<String>();
s.add("Kashish");
s.add("Pedro");
s.add("Lorem");
s.add("ipsum");
s.add("dolor");
s.add("sit");
s.add("amet");
Trie root = new Trie();
for (int i = 0; i < s.size(); i++) {
root.insertIntoTrie(root, s.get(i));
}
assertTrue(root.searchInTrie(root, "Kashish"));
}
@Test public void shouldReturnTrueIfStringIsFound1() {
String s = "Kashish Kashisk Lorem ipsum dolor dolo sit si amrt amet";
String[] arr = s.split(" ");
Trie root = new Trie();
for (int i = 0; i < arr.length; i++) {
root.insertIntoTrie(root, arr[i]);
}
// System.out.println("Code checking");
Copy link

@juncevich juncevich Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you do not delete this commented code?

assertTrue(root.searchInTrie(root, "Kashish"));
assertTrue(root.searchInTrie(root, "Kashisk"));
assertFalse(root.searchInTrie(root, "Kashis"));
assertTrue(root.searchInTrie(root, "dolo"));
}
}