From 9be67ff52ce4db3164849e9f4e52f50871320345 Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 12:46:09 +0530 Subject: [PATCH 1/7] Added Trie Data Structures --- .../java/com/github/pedrovgs/trie/trie.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/github/pedrovgs/trie/trie.java diff --git a/src/main/java/com/github/pedrovgs/trie/trie.java b/src/main/java/com/github/pedrovgs/trie/trie.java new file mode 100644 index 00000000..40e05a57 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/trie/trie.java @@ -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 m; + + public trie() { + m = new HashMap(); // 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; + } + +} From 3925cb277f8f1b22b895c6d1c6503b28e87da400 Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 12:47:21 +0530 Subject: [PATCH 2/7] Added Trie Data Structure --- .../com/github/pedrovgs/trie/TrieTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/test/java/com/github/pedrovgs/trie/TrieTest.java diff --git a/src/test/java/com/github/pedrovgs/trie/TrieTest.java b/src/test/java/com/github/pedrovgs/trie/TrieTest.java new file mode 100644 index 00000000..4783f11b --- /dev/null +++ b/src/test/java/com/github/pedrovgs/trie/TrieTest.java @@ -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 s = new ArrayList(); + 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"); + assertTrue(root.searchInTrie(root, "Kashish")); + assertTrue(root.searchInTrie(root, "Kashisk")); + assertFalse(root.searchInTrie(root, "Kashis")); + assertTrue(root.searchInTrie(root, "dolo")); + } +} \ No newline at end of file From 8bd486e3e9ac43c45c53cb419b600c53d48196ed Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 12:48:55 +0530 Subject: [PATCH 3/7] Update .gitignore --- .gitignore | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/.gitignore b/.gitignore index 9e939a95..8b137891 100644 --- a/.gitignore +++ b/.gitignore @@ -1,35 +1 @@ -# Built application files -*.jar -# Java class files -*.class - -# Generated files -bin/ -gen/ -classes/ - -# Maven output folder -target - -# Eclipse project files -.classpath -.project -.metadata -.settings - -# IntelliJ files -.idea -*.iml - -# OSX files -.DS_Store - -# Windows files -Thumbs.db - -# vi swap files -*.swap - -# backup files -*.bak From a39e32dc2e6d5dc594a55e6ef4a44f95e2d108ee Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 12:53:05 +0530 Subject: [PATCH 4/7] Update .gitignore --- .gitignore | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.gitignore b/.gitignore index 8b137891..9e939a95 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,35 @@ +# Built application files +*.jar +# Java class files +*.class + +# Generated files +bin/ +gen/ +classes/ + +# Maven output folder +target + +# Eclipse project files +.classpath +.project +.metadata +.settings + +# IntelliJ files +.idea +*.iml + +# OSX files +.DS_Store + +# Windows files +Thumbs.db + +# vi swap files +*.swap + +# backup files +*.bak From 509be5139a3bc035de35188fa02314cf59b7d326 Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 13:01:18 +0530 Subject: [PATCH 5/7] Fixed CheckStyle Errors in Trie.java --- .../java/com/github/pedrovgs/trie/Trie.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/github/pedrovgs/trie/Trie.java diff --git a/src/main/java/com/github/pedrovgs/trie/Trie.java b/src/main/java/com/github/pedrovgs/trie/Trie.java new file mode 100644 index 00000000..203ed326 --- /dev/null +++ b/src/main/java/com/github/pedrovgs/trie/Trie.java @@ -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 m; + + public Trie() { + m = new HashMap(); // 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; + } + +} From 75bee805b48e67e89f5c00df065729e83d266cae Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 13:04:33 +0530 Subject: [PATCH 6/7] Parallel fix to fix in trie.java Fixing of changes cused by CheckStyle fix in trie.java --- src/test/java/com/github/pedrovgs/trie/TrieTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/github/pedrovgs/trie/TrieTest.java b/src/test/java/com/github/pedrovgs/trie/TrieTest.java index 4783f11b..f8320f9d 100644 --- a/src/test/java/com/github/pedrovgs/trie/TrieTest.java +++ b/src/test/java/com/github/pedrovgs/trie/TrieTest.java @@ -25,7 +25,7 @@ public void setUp() { s.add("dolor"); s.add("sit"); s.add("amet"); - trie root = new trie(); + Trie root = new Trie(); for (int i = 0; i < s.size(); i++) { root.insertIntoTrie(root, s.get(i)); } @@ -34,7 +34,7 @@ public void setUp() { @Test public void shouldReturnTrueIfStringIsFound1() { String s = "Kashish Kashisk Lorem ipsum dolor dolo sit si amrt amet"; String[] arr = s.split(" "); - trie root = new trie(); + Trie root = new Trie(); for (int i = 0; i < arr.length; i++) { root.insertIntoTrie(root, arr[i]); } From b7078ba2632bd68dc1b265e5e4ac6ee104a5e4c7 Mon Sep 17 00:00:00 2001 From: ICEBERG98 <41355011+ICEBERG98@users.noreply.github.com> Date: Sun, 21 Oct 2018 13:24:50 +0530 Subject: [PATCH 7/7] Delete trie.java --- .../java/com/github/pedrovgs/trie/trie.java | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 src/main/java/com/github/pedrovgs/trie/trie.java diff --git a/src/main/java/com/github/pedrovgs/trie/trie.java b/src/main/java/com/github/pedrovgs/trie/trie.java deleted file mode 100644 index 40e05a57..00000000 --- a/src/main/java/com/github/pedrovgs/trie/trie.java +++ /dev/null @@ -1,56 +0,0 @@ -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 m; - - public trie() { - m = new HashMap(); // 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; - } - -}