-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
ICEBERG98
wants to merge
7
commits into
pedrovgs:master
Choose a base branch
from
ICEBERG98:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9be67ff
Added Trie Data Structures
ICEBERG98 3925cb2
Added Trie Data Structure
ICEBERG98 8bd486e
Update .gitignore
ICEBERG98 a39e32d
Update .gitignore
ICEBERG98 509be51
Fixed CheckStyle Errors in Trie.java
ICEBERG98 75bee80
Parallel fix to fix in trie.java
ICEBERG98 b7078ba
Delete trie.java
ICEBERG98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?