I am trying to serialize a Trie in Java (prefix tree), so I wrote this code
public void serialize(DictNode node, ObjectOutputStream outPut){
if (node == null) {
return;
}
int i;
if(node==root){
try{
System.out.println(node+" root");
outPut.writeObject(node);
}catch(IOException ex) {
ex.printStackTrace();
}
}
for(i=0;i<26;i++){
if(node.array[i]!=null){
System.out.println(node.array[i]+" ser "+(char)(i+'a'));
try{
outPut.writeObject(node.array[i]);
}catch(IOException ex) {
ex.printStackTrace();
}
serialize(node.array[i],outPut/*,temp*/);
}
}
}
But it doesn't seem to work properly. So here is the thing. When I wrote a System.out.println in order to print the adresses of the nodes that I pass througth in the code that I wrote to priint the leafs. But when I do the same thing in the method of serialization the adresses are completely different even thought the code is the same. Why does this happen????
public void printLeafNodes(DictNode node,String preflix) {
if (node == null) {
return;
}
int i;
if(node.isTerminalNode){
System.out.println(preflix);
}
for(i=0;i<26;i++){
if(node.array[i]!=null){
System.out.println(node.array[i]+" "+(char)(i+'a'));
Character.toString((char)(i+'a'));
preflix=preflix+(char)(i+'a');
printLeafNodes(node.array[i],preflix);
preflix=preflix.substring(0,preflix.length()-1);
}
}
}