From 23990b4f6eb2aafa0c0b8205aff97630e2ca411e Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Thu, 26 Oct 2017 10:15:41 -0400 Subject: [PATCH] Implemented getWords(Trie tr). --- .../src/TrieFunctions.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Java/TriesWithFrequencies/src/TrieFunctions.java b/Java/TriesWithFrequencies/src/TrieFunctions.java index 59a90df6..3c634b94 100644 --- a/Java/TriesWithFrequencies/src/TrieFunctions.java +++ b/Java/TriesWithFrequencies/src/TrieFunctions.java @@ -496,6 +496,26 @@ public static String pathsToJSON(List>> paths) { return res; } + //! @description Finds all words in the trie tr that start with the word searchWord. + //! @param tr a trie object + //! @param sword search word + public static List> getWords(Trie tr) { + + // Simple copy of the code below, too short and trivial to refactor at this point. + List>> paths = rootToLeafPaths(tr); + + List> res = new ArrayList<>(); + for (List> ps : paths) { + + List w = new ArrayList<>(); + + for (Map.Entry p : ps) { + w.add(p.getKey()); + } + res.add(w); + } + return res; + } //! @description Finds all words in the trie tr that start with the word searchWord. //! @param tr a trie object