Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package dsa;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9.  
  10. public class Trie {
  11.  
  12. private TrieNode root;
  13.  
  14. public Trie() {
  15. root = new TrieNode(' ', null);
  16. }
  17.  
  18.  
  19. /**
  20. * Inserts a word into the Trie.
  21. *
  22. * @param word
  23. */
  24. public void insert(String word) {
  25. Map<Character, TrieNode> children = root.childern;
  26.  
  27. // Keep track of the parent node while we traverse through the trie.
  28. // Parent node info is required while creating a new TrieNode.
  29. TrieNode parentNode = root;
  30. for(int i=0; i< word.length(); i++) {
  31. char currentChar = word.charAt(i);
  32.  
  33. // Traverse through the Trie to add missing nodes for the word
  34. // that is getting inserted.
  35. TrieNode currentNode;
  36. if (children.containsKey(currentChar)) {
  37. currentNode = children.get(currentChar);
  38. } else {
  39. currentNode = new TrieNode(currentChar, parentNode);
  40. children.put(currentChar, currentNode);
  41. }
  42.  
  43. // Make the current node as parent node and process it's children.
  44. parentNode = currentNode;
  45. children = currentNode.childern;
  46.  
  47. // If the current character is the last character,
  48. // mark the current node as a valid Word in the Trie
  49. if (i == word.length()-1) {
  50. currentNode.isEndOfWord = true;
  51. }
  52. }
  53. }
  54.  
  55. /**
  56. * Displays all words present in a Trie.
  57. */
  58. public void displayWords(TrieNode currentNode, String str) {
  59. if (currentNode.isEndOfWord) {
  60. System.out.println(str);
  61. }
  62.  
  63. Map<Character, TrieNode> children = currentNode.childern;
  64.  
  65. for(Entry<Character, TrieNode> entry : children.entrySet()) {
  66. displayWords(entry.getValue(), str + entry.getKey());
  67. }
  68. }
  69.  
  70. /**
  71. * Returns a List of suggestions based on the prefix.
  72. *
  73. * @param prefix
  74. * @return List containing words that starts with prefix.
  75. */
  76. public List<String> suggest(String prefix) {
  77. List<String> words = new ArrayList<String>();
  78.  
  79. // Locate the prefix in the Trie
  80. TrieNode t = root;
  81. boolean prefixFound = true;
  82. for(int i=0; i< prefix.length(); i++) {
  83. char c = prefix.charAt(i);
  84.  
  85. t = t.childern.get(c);
  86. if (t == null) {
  87. prefixFound = false;
  88. break;
  89. }
  90. }
  91.  
  92. // Collect all child words for the prefix.
  93. if (prefixFound) {
  94. collectChildWords(t, words);
  95. Collections.sort(words);
  96. }
  97.  
  98. return words;
  99. }
  100.  
  101. private void collectChildWords(TrieNode currentNode, List<String> words) {
  102. if (currentNode.isEndOfWord) {
  103. words.add(currentNode.toString());
  104. }
  105.  
  106. Map<Character, TrieNode> children = currentNode.childern;
  107. for(Entry<Character, TrieNode> entry : children.entrySet()) {
  108. collectChildWords(entry.getValue(), words);
  109. }
  110. }
  111.  
  112.  
  113. public static void main(String[] args) {
  114. Trie trie = new Trie();
  115. trie.insert("dog");
  116. trie.insert("damp");
  117. trie.insert("dance");
  118. trie.insert("damage");
  119.  
  120. trie.insert("cat");
  121. trie.insert("camera");
  122. trie.insert("catch");
  123. trie.insert("camp");
  124.  
  125. trie.displayWords(trie.root, "");
  126.  
  127. List<String> suggestions = trie.suggest("ca");
  128.  
  129. System.out.println(suggestions);
  130. }
  131.  
  132.  
  133. static class TrieNode {
  134. char c;
  135. Map<Character, TrieNode> childern = new HashMap<>();
  136. boolean isEndOfWord;
  137. // Having a parent pointer helps us to implement toString()
  138. TrieNode parent;
  139.  
  140. TrieNode(char c, TrieNode parent) {
  141. this.c = c;
  142. this.parent = parent;
  143. }
  144.  
  145. public String toString() {
  146. if (parent == null) {
  147. return "";
  148. }
  149.  
  150. return parent.toString() + c;
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement