I have a dictionary project in java.it adds words to a trie tree and then I need to add a linkedlist to end of each word I entered that saves meanings. I have made two kind of nodes like bellow for the trie and linked list.
static class TrieNode{
TrieNode[]children=new TrieNode[29];
boolean IsEnd;
TrieNode(){
IsEnd=false;
for(int i=0;i<29;i++)
children[i]=null;
}
}
static class Meaningnode{
String Meaning;
Meaningnode next;
public Meaningnode(String Meaning){
this.Meaning=Meaning;
}
}
29 links for 26 letters and a link to parent and space ' ' and a one to the list;
In your explanation for 29 entries, you write:
29 links for 26 letters and a link to parent and space' ' and a one to the list;
The problem here is that the "one to the list" will violate the type that is expected. The declaration is:
TrieNode[]children=new TrieNode[29];
But you want to use an entry for a Meaningnode
instead of a TrieNode
. That is not right. Instead, just dedicate a separate property for that purpose, just like you have already a separate property for isEnd
. I would even make a separate entry for the parent too (if you really need a parent reference).
Finally, add a constructor for Meaningnode
that can take a second argument for specifying next
:
static class Meaningnode {
String Meaning;
Meaningnode next;
public Meaningnode(String Meaning) {
this.Meaning = Meaning;
}
public Meaningnode(String Meaning, Meaningnode next) {
this.Meaning = Meaning;
this.next = next;
}
}
static class TrieNode {
TrieNode[] children = new TrieNode[27];
boolean IsEnd;
TrieNode parent;
Meaningnode meaning;
TrieNode() {
IsEnd = false;
parent = null;
meaning = null;
for (int i = 0; i < 27; i++)
children[i] = null;
}
// Method to prepend a meaning to the list of meanings:
addMeaning(string meaning) {
this.meaning = new Meaningnode(meaning, this.meaning);
}
// Other methods...
// ...
}