1

I am implementing B Plus Tree in java . I have a node class in which I am maintaining references to child object Nodes . Now When I serialize any node , it also serialize all the child nodes also. What I want is to serialize only that node and the references to the child nodes.I tried writing the node object as a byte stream but on de-serializing it is not working.

public class BNode implements Serializable
{
    LinkedList<Float> keys;
    LinkedList<BNode> childPointers;
    BNode parent;
 ...
}

In B+ tree , the nodes are saved in disk and I have to simulate that action. Now each page is of 2 KB(say) so in my each node I am saving data of around 2044 bytes( 255 float values, and 256 node references - total 255*4 + 255*4 + some other data of 10 bytes)in a single file simulating a single node. Now if I serialize the parent node, it is serializing whole tree into single file thus defeating the whole purpose

user207421
  • 305,947
  • 44
  • 307
  • 483
justpraveen
  • 163
  • 2
  • 5

2 Answers2

0

You have to use transient. This excludes variables from serializing.

public class BNode implements Serializable
{
    LinkedList<Float> keys;
    LinkedList<BNode> childPointers;
    transient BNode parent;
 ...
}

If you need more sophisticated rules you can overwrite de default behaviour for (de)serialization by implementing this mehtods:

private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

Check http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html for an indepth description.

wdf
  • 171
  • 5
BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • 1
    what about the childPointer list. Serializing the object also serializing all the nodes which are in childPointer list. – justpraveen Feb 09 '15 at 10:32
0

In any practical disk-based B-tree, each index block will need to contain disk offsets of the blocks corresponding to the keys in this block. So you will need to make the reference array transient, and add a non-transient array of long for the offsets.

I would also suggest that you don't keep a parent reference or offset if you can possibly avoid it. If you memorise each search path you can avoid it. In large trees when you split an interior node, you don't want to have to update the parent links in N/2 child nodes as well as all the other things you have to do. It's a very significant performance hit.

user207421
  • 305,947
  • 44
  • 307
  • 483