From a16eb526b3dcc64f3ee11912ee496d94ab85a53d Mon Sep 17 00:00:00 2001 From: ShivamShekhar1997 <72576812+ShivamShekhar1997@users.noreply.github.com> Date: Fri, 9 Oct 2020 01:31:30 +0530 Subject: [PATCH 1/3] Added a Trie data structure implementation in Java --- Trie.txt | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Trie.txt diff --git a/Trie.txt b/Trie.txt new file mode 100644 index 00000000..99b5a4e5 --- /dev/null +++ b/Trie.txt @@ -0,0 +1,62 @@ +private class Node { + String prefix; + HashMap children; + + // Does this node represent the last character in a word? + boolean isWord; + + private Node(String prefix) { + this.prefix = prefix; + this.children = new HashMap(); + } +} + +// The trie +private Node trie; + +// Construct the trie from the dictionary +public Autocomplete(String[] dict) { + trie = new Node(""); + for (String s : dict) insertWord(s); +} + +// Insert a word into the trie +private void insertWord(String s) { + // Iterate through each character in the string. If the character is not + // already in the trie then add it + Node curr = trie; + for (int i = 0; i < s.length(); i++) { + if (!curr.children.containsKey(s.charAt(i))) { + curr.children.put(s.charAt(i), new Node(s.substring(0, i + 1))); + } + curr = curr.children.get(s.charAt(i)); + if (i == s.length() - 1) curr.isWord = true; + } +} + +// Find all words in trie that start with prefix +public List getWordsForPrefix(String pre) { + List results = new LinkedList(); + + // Iterate to the end of the prefix + Node curr = trie; + for (char c : pre.toCharArray()) { + if (curr.children.containsKey(c)) { + curr = curr.children.get(c); + } else { + return results; + } + } + + // At the end of the prefix, find all child words + findAllChildWords(curr, results); + return results; +} + +// Recursively find every child word +private void findAllChildWords(Node n, List results) { + if (n.isWord) results.add(n.prefix); + for (Character c : n.children.keySet()) { + findAllChildWords(n.children.get(c), results); + } +} \ No newline at end of file From 8d61dd20d2c8744c87ac7f97eff0ce566bfb86d5 Mon Sep 17 00:00:00 2001 From: ShivamShekhar1997 <72576812+ShivamShekhar1997@users.noreply.github.com> Date: Thu, 22 Oct 2020 16:23:11 +0530 Subject: [PATCH 2/3] Trie Java implementation --- trie/Trie.txt | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 trie/Trie.txt diff --git a/trie/Trie.txt b/trie/Trie.txt new file mode 100644 index 00000000..99b5a4e5 --- /dev/null +++ b/trie/Trie.txt @@ -0,0 +1,62 @@ +private class Node { + String prefix; + HashMap children; + + // Does this node represent the last character in a word? + boolean isWord; + + private Node(String prefix) { + this.prefix = prefix; + this.children = new HashMap(); + } +} + +// The trie +private Node trie; + +// Construct the trie from the dictionary +public Autocomplete(String[] dict) { + trie = new Node(""); + for (String s : dict) insertWord(s); +} + +// Insert a word into the trie +private void insertWord(String s) { + // Iterate through each character in the string. If the character is not + // already in the trie then add it + Node curr = trie; + for (int i = 0; i < s.length(); i++) { + if (!curr.children.containsKey(s.charAt(i))) { + curr.children.put(s.charAt(i), new Node(s.substring(0, i + 1))); + } + curr = curr.children.get(s.charAt(i)); + if (i == s.length() - 1) curr.isWord = true; + } +} + +// Find all words in trie that start with prefix +public List getWordsForPrefix(String pre) { + List results = new LinkedList(); + + // Iterate to the end of the prefix + Node curr = trie; + for (char c : pre.toCharArray()) { + if (curr.children.containsKey(c)) { + curr = curr.children.get(c); + } else { + return results; + } + } + + // At the end of the prefix, find all child words + findAllChildWords(curr, results); + return results; +} + +// Recursively find every child word +private void findAllChildWords(Node n, List results) { + if (n.isWord) results.add(n.prefix); + for (Character c : n.children.keySet()) { + findAllChildWords(n.children.get(c), results); + } +} \ No newline at end of file From cad11c41a99a92484c6c88cfba14a0e69836c9f3 Mon Sep 17 00:00:00 2001 From: ShivamShekhar1997 <72576812+ShivamShekhar1997@users.noreply.github.com> Date: Thu, 22 Oct 2020 16:31:26 +0530 Subject: [PATCH 3/3] Delete Trie.txt --- Trie.txt | 62 -------------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 Trie.txt diff --git a/Trie.txt b/Trie.txt deleted file mode 100644 index 99b5a4e5..00000000 --- a/Trie.txt +++ /dev/null @@ -1,62 +0,0 @@ -private class Node { - String prefix; - HashMap children; - - // Does this node represent the last character in a word? - boolean isWord; - - private Node(String prefix) { - this.prefix = prefix; - this.children = new HashMap(); - } -} - -// The trie -private Node trie; - -// Construct the trie from the dictionary -public Autocomplete(String[] dict) { - trie = new Node(""); - for (String s : dict) insertWord(s); -} - -// Insert a word into the trie -private void insertWord(String s) { - // Iterate through each character in the string. If the character is not - // already in the trie then add it - Node curr = trie; - for (int i = 0; i < s.length(); i++) { - if (!curr.children.containsKey(s.charAt(i))) { - curr.children.put(s.charAt(i), new Node(s.substring(0, i + 1))); - } - curr = curr.children.get(s.charAt(i)); - if (i == s.length() - 1) curr.isWord = true; - } -} - -// Find all words in trie that start with prefix -public List getWordsForPrefix(String pre) { - List results = new LinkedList(); - - // Iterate to the end of the prefix - Node curr = trie; - for (char c : pre.toCharArray()) { - if (curr.children.containsKey(c)) { - curr = curr.children.get(c); - } else { - return results; - } - } - - // At the end of the prefix, find all child words - findAllChildWords(curr, results); - return results; -} - -// Recursively find every child word -private void findAllChildWords(Node n, List results) { - if (n.isWord) results.add(n.prefix); - for (Character c : n.children.keySet()) { - findAllChildWords(n.children.get(c), results); - } -} \ No newline at end of file