Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Collect all words in a Trie with a prefix

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Collect all words in a Trie with a prefix

    I'm trying to implement a method that will store all words in a Trie data structure in a List.
    This is my Node class:

    class Node {
        char c;
        HashMap<Character, Node> children = new HashMap<Character, Node>();
        boolean isCompleteWord;
     
        public Node(char c) {
            this.c = c;
            isCompleteWord = false;
        }
        public Node() {
        }
    }

    I am using a HashMap to store the child characters as keys and the child nodes as values. A node that completes a word will have the field isCompleteWord set to true.

    Methods to add all words to a List:

    List<String> collectWords(char c) {
        List<String> words = new ArrayList<>();
        String word = Character.toString(c);
        Node node = root.children.get(c);
        collectWordsHelper(words, node, word);
        return words;
    }
     
    void collectWordsHelper(List<String> words, Node node, String word) {
        for (char i = 'a'; i <= 'z'; i++) {
            if (node.children.containsKey(i)) {
                System.out.println(i); // prints the right letters
                Node child = node.children.get(i);
                word += i;
                if (child.isCompleteWord && !words.contains(word)) {
                    words.add(word);
                    collectWordsHelper(words, child, "");
                }
                collectWordsHelper(words, child, word);
            }
        }
    }

    Currently if I have stored in the Trie the words "beard", "bold", "brew", when I print the list of words starting with the prefix "b", I get:

    [beard, beold, beorew]

    What I was expecting

    [beard, bold, brew]

    I think I need a way for the String word to be reset whenever I have found a word, instead of the next characters being appended.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Collect all words in a Trie with a prefix

    Also posted at https://www.dreamincode.net/forums/t...with-a-prefix/

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. problem in Trie data structure in java
    By Rajchandra in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2014, 05:17 AM
  2. New - Need Advice about Java Based site to collect data for dissertation
    By mm2025 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2014, 05:20 AM
  3. Replies: 2
    Last Post: February 10th, 2014, 06:24 AM
  4. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  5. Implementation of search function for Patricia Trie
    By javaguy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2008, 01:54 PM

Tags for this Thread