user1920811
user1920811

Reputation:

Serialize binary tree in a stream and reconstruct the tree

You have a binary tree (not BST), serialize it in a stream and reconstruct the tree maintaining the format of the tree.

Sending 2 streams InOrder + PreOrder or InOrder + PostOrder is not an option.

Can anyone suggest some solution, using JAVA?

Upvotes: 2

Views: 630

Answers (2)

Lars
Lars

Reputation: 510

In addition to Petr's answer you might want to have a look here.

Be sure that the object that should be streamed implements the java.io.Serializable interface.

Upvotes: 0

Petr
Petr

Reputation: 63359

If your data structure allows it, you could use Java Serialization API. If your tree objects (and all objects referenced from it) implement java.io.Serializable, you could use the API serialize the whole structure into a stream and then deserialize it at some different place. (The linked page contains an example.) The serialization library handles dependencies between serialized objects, so that they are properly restored when deserailizing.

Upvotes: 4

Related Questions