I have a kdtree whose nodes consist of the following fields: public static class Node implements Serializable {
public int discriminator;
public double value;
public Node leftChild;
public Node rightChild;
public DataPoint object;
}
where DataPoint definition:
public static class DataPoint implements Serializable { public Comparable X; public Comparable Y; public Comparable Z;
public double latitude;
public double longitude;
public ArrayList<String> text = new ArrayList<String>();
}
I want to serialize the tree, store in a file and deserialize while answering range queries. My understanding of this concept od serialization is not fine. From whatever I gathered,I have written the following functions, which is not working. Can anybody help?
public static void serialize(final OutputStream out, kdtrees.Node node) throws IOException
{
if( node == null )
{
//out.write( "()".getBytes() );
//write to an output leaf file
out.write("#".getBytes());
}
else
{
//out.write( "(".getBytes() );
((ObjectOutputStream) out).writeObject(node);
serialize( out, node.leftChild );
serialize( out, node.rightChild );
//out.write( ")".getBytes( ) );
}
}
public static kdtrees.Node deserialize(final ObjectInputStream reader) throws IOException, ClassNotFoundException
{
StringTokenizer st = new StringTokenizer(reader.readObject().toString());
String curToken = st.nextToken().toString();
while(st.hasMoreTokens())
{
if(curToken.equals("#".trim()))
return null;
kdtrees.Node e = (kdtrees.Node)reader.readObject();
e.leftChild = deserialize(reader);
e.rightChild = deserialize(reader);
return e;
}
return null;
}