Click here to Skip to main content
15,881,898 members
Home / Discussions / Java
   

Java

 
GeneralRe: Global variables are not supported in C# Pin
Dr.Walt Fair, PE5-May-19 7:54
professionalDr.Walt Fair, PE5-May-19 7:54 
AnswerRe: Global variables are not supported in C# Pin
Maciej Los27-Mar-19 23:00
mveMaciej Los27-Mar-19 23:00 
GeneralRe: Global variables are not supported in C# Pin
Brian_TheLion27-Mar-19 23:35
Brian_TheLion27-Mar-19 23:35 
AnswerRe: Global variables are not supported in C# Pin
Dr.Walt Fair, PE5-May-19 7:50
professionalDr.Walt Fair, PE5-May-19 7:50 
QuestionHome automation using iot Pin
Member 1417643119-Mar-19 1:49
Member 1417643119-Mar-19 1:49 
AnswerRe: Home automation using iot Pin
OriginalGriff19-Mar-19 1:51
mveOriginalGriff19-Mar-19 1:51 
AnswerRe: Home automation using iot Pin
jschell23-Mar-19 5:48
jschell23-Mar-19 5:48 
QuestionStore all words in a Trie into a list Pin
Member 1418712518-Mar-19 13:55
Member 1418712518-Mar-19 13:55 
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.
GeneralRe: Store all words in a Trie into a list Pin
Richard MacCutchan18-Mar-19 22:55
mveRichard MacCutchan18-Mar-19 22:55 
QuestionGraph theory algortithm Pin
Member 141708224-Mar-19 8:33
Member 141708224-Mar-19 8:33 
QuestionJava 8 or 10 for Windows 8.1 Pin
Brian_TheLion2-Mar-19 11:45
Brian_TheLion2-Mar-19 11:45 
AnswerRe: Java 8 or 10 for Windows 8.1 Pin
Richard MacCutchan3-Mar-19 2:36
mveRichard MacCutchan3-Mar-19 2:36 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Brian_TheLion3-Mar-19 10:53
Brian_TheLion3-Mar-19 10:53 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Richard MacCutchan3-Mar-19 21:42
mveRichard MacCutchan3-Mar-19 21:42 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Brian_TheLion4-Mar-19 0:03
Brian_TheLion4-Mar-19 0:03 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Kaila Chester9-Dec-19 15:37
Kaila Chester9-Dec-19 15:37 
QuestionWhat attracts people to choose Java as a programming language Pin
Brian_TheLion22-Feb-19 19:13
Brian_TheLion22-Feb-19 19:13 
AnswerRe: What attracts people to choose Java as a programming language Pin
OriginalGriff22-Feb-19 19:15
mveOriginalGriff22-Feb-19 19:15 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion23-Feb-19 13:17
Brian_TheLion23-Feb-19 13:17 
GeneralRe: What attracts people to choose Java as a programming language Pin
OriginalGriff23-Feb-19 20:46
mveOriginalGriff23-Feb-19 20:46 
AnswerRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan22-Feb-19 21:56
mveRichard MacCutchan22-Feb-19 21:56 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion23-Feb-19 13:15
Brian_TheLion23-Feb-19 13:15 
GeneralRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan26-Feb-19 3:59
mveRichard MacCutchan26-Feb-19 3:59 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion26-Feb-19 19:33
Brian_TheLion26-Feb-19 19:33 
GeneralRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan26-Feb-19 21:15
mveRichard MacCutchan26-Feb-19 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.