So the point of the other thread (java compiler) was I could not test any solutions I came up with at home for my next question.
My assignment is to create a Binary Tree (done), and write the output to a file name (out.dat). The assignment is for one of the binary trees to use serialization to show how this code will create a smaller out.dat file than using the default writeObject command which will be used in the other binary Tree file. To do this we need to write our own writeObject/readObject method.
I think I have the read object method but will include it for a complete picture.
The example we are working off of is a Linked List. Below is the write/readObject code used in class example,
private void writeObject(ObjectOutputStream s) throws IOException
{ System.out.println("call specialized writer");
s.writeInt(_v.size());
for (Iterator i = _v.iterator(); i.hasNext(); )
s.writeInt(((Integer) i.next()).intValue());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{ System.out.println("call specialized reader");
_v = new LinkedList();
int size = in.readInt();
for (int i = 0; i<size; i++)
addBack(in.readInt());
}
Then in the test file we use the following code to write out.dat, which uses the above code for write and readObject.
ObjectOutputStream os
= new ObjectOutputStream
(new FileOutputStream("out.dat"));
os.writeObject(L);
os.flush();
ObjectInputStream in
= new ObjectInputStream
(new FileInputStream("out.dat"));
IntListSerializable V = (IntListSerializable) in.readObject();
---------------------------------------------------------------------------------
My ReadObject Method is ...
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{ System.out.println("call specialized reader");
BinaryTree read = new BinaryTree();
int size = in.readInt();
for (int i = 0; i<size; i++)
read.insert(in.readInt()); //my insert method.
}
My problem comes in for the write method. First I am not sure how to return the next value in my tree with out doing the inorder recursive traversal.
In my tree I have the following methods...
Lookup() which is a boolean, Insert, size(), printTree, printPostOrder
Could I do a recursive next() function where it traverses similar to printTree, except use next and no print functions?
Prints the node values in the "inorder" order.
Uses a recursive helper to do the traversal.
*/
public void printTree() {
printTree(root);
System.out.println();
}
private void printTree(Node node) {
if (node == null)
return;
// left, node itself, right
printTree(node.left);
System.out.print(node.data + " ");
printTree(node.right);
}
This was I could do something like ...
private void writeObject(ObjectOutputStream s) throws IOException
{ System.out.println("call specialized writer");
s.writeInt( ???.size());
for (i = 0; i < size; i++)
s.writeInt(((Integer) ???.next()).intValue());
}
Not sure what I need to put in for the ??? either, since I assume I do not need to create a new tree here. But in my code I use the following code to create my tree...
public class BinaryTree implements Serializable {
private Node root;
private static class Node {
Node left;
Node right;
int data;
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
// Creates an empty binary tree -- a null root pointer.
public void BinaryTree() {
root = null;
}
Should I just create a tree in my class source code?
BinaryTree someTree = new BinaryTree();
Then on the readObject in the testing file, which reads from the writeObject I am trying to put together above...
This is the example we are given to model off of...
ObjectInputStream in = new ObjectInputStream (new FileInputStream("out.dat"));
IntListSerializable V = (IntListSerializable) in.readObject();
Do I just do something like the following?
BinaryTree X = (BinaryTree)in.readObject();
or
Node X = (Node)in.readObject();