ok....

finally have something workable....
public void readExternal(ObjectInput in)
{
try
{
System.out.println("Coming into the readExternal of data:");
readAgain(in,this);
System.out.println("Finishing readExternal of data:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void readAgain(ObjectInput in,data current)
{
System.out.println("Coming into the readAgain of data:");
LinkedList traversal=new LinkedList();
int nodeCount=0;
int childValue=0;
try
{
nodeCount=(int)in.readInt();
System.out.println("The node count is :"+nodeCount);
traversal.add(new String("START"));
while(nodeCount>0)
{
nodeCount--;
childValue=(int)in.readInt();
System.out.println("The child value is :"+childValue+" for the node "+nodeCount);
if(childValue==3)
{
System.out.println("Coming into the loop for childValue "+childValue);
current.age=(int)in.readInt();
current.name=(String)in.readObject();
current.childL=new data();
current.childR=new data();
traversal.add(current.childR);
current=current.childL;
}
else if(childValue==1)
{
System.out.println("Coming into the loop for childValue "+childValue);
current.age=(int)in.readInt();
current.name=(String)in.readObject();
current.childL=new data();
current=current.childL;
}
else if (childValue==2)
{
System.out.println("Coming into the loop for childValue "+childValue);
current.age=(int)in.readInt();
current.name=(String)in.readObject();
current.childR=new data();
current=current.childR;
}
else
{
System.out.println("Coming into the loop for childValue "+childValue);
current.age=(int)in.readInt();
current.name=(String)in.readObject();
if(traversal.size()>1)
{
current=(data)traversal.removeLast();
}
else
{
traversal.clear();
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Coming out of readAgain of data:");
}
public void writeExternal(ObjectOutput out)
{
try
{
System.out.println("Coming into the writeExternal of data:");
writeAgain(out,this);
System.out.println("finishing the writeExternal of data:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void writeAgain(ObjectOutput out,data current_node)
{
data current=current_node;
int nodeCount=0;
LinkedList traversal=new LinkedList();
System.out.println("Loop to get the count of nodes.....");
try
{
traversal.add(new String("START"));
while(traversal.size()>0)
{
if ( current.childL!=null && current.childR!=null )
{
System.out.println("both children");
nodeCount++;
traversal.add(current.childR);
current=current.childL;
}
else
{
if (current.childL!=null)
{
System.out.println("left child");
nodeCount++;
current=current.childL;
}
else if (current.childR!=null)
{
System.out.println("right child");
nodeCount++;
current=current.childR;
}
else
{
System.out.println("no child");
nodeCount++;
if(traversal.size()>1)
{
current=(data)traversal.removeLast();
}
else
{
traversal.clear();
}
}
}
}
System.out.println("The no of nodes is : "+nodeCount);
traversal=new LinkedList();;
current=current_node;
traversal.add(new String("START"));
out.writeInt(nodeCount);
while(traversal.size()>0)
{
if ( current.childL!=null && current.childR!=null )
{
System.out.println("The childValue written is : "+3);
out.writeInt(3);
out.writeInt(current.age);
out.writeObject(current.name);
traversal.add(current.childR);
current=current.childL;
}
else
{
if (current.childL!=null)
{
System.out.println("The childValue written is : "+1);
out.writeInt(1);
out.writeInt(current.age);
out.writeObject(current.name);
current=current.childL;
}
else if (current.childR!=null)
{
System.out.println("The childValue written is : "+2);
out.writeInt(2);
out.writeInt(current.age);
out.writeObject(current.name);
current=current.childR;
}
else
{
System.out.println("The childValue written is : "+0);
out.writeInt(0);
out.writeInt(current.age);
out.writeObject(current.name);
if(traversal.size()>1)
{
current=(data)traversal.removeLast();
}
else
{
traversal.clear();
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
// this works...
By the way the real code has about 6 similar classes having some primitive variables and string objects.
Three of the these classes have 2 references to themselves
.Two of them generate about 24,000 and 33,000 nodes respectively.
The others generate about 1500 nodes between them
Keeping this in mind could you suggest any improvements in the code ...