Learn-dsa..in 30 days!



























CC-18 : Serialize and Deserialize a Binary Tree.

Description:

In a mirror Binary Tree, all nodes have their left and right child nodes interchanged. Given a Binary Tree, find and return the mirror Binary Tree.

Test cases and expected outputs:

Input Parameters Expected outputs
Serialize and de-serialize above tree. Bfs traversal before Serialization:
: 4,5,7,6,78,14,33,8,16,18,9

Bfs traversal after Serialization and deserialization:
: 4,5,7,6,78,14,33,8,16,18,9

Pseudocode:

We will use StringBuilder variable named serial to hold the serialized Binary Tree.
We use character ‘*’ to separate left and right child of node in the variable serial.
We use character ‘#’ to indicate the current child of current node is null in the variable serial.

Method serializeBinTree(TreeNode root, StringBuilder serial):

If root node is null, append # and * to serial and return.
Else:
Append root node’s data and ‘*’ to serial.
Recursively call serializeBinTree() method with roots’ left child and serial as input parameters.
Recursively call serializeBinTree() method with roots’ right child and serial as input parameters.

Method deserializeBinTree(serial):

Tokenize serial on the token ‘*’ and store in variable named nodes or type StringTokenizer.
Return value returned by calling deserialize() with nodes as input parameter.

Method deserialize(nodes):

If nodes does not have any more token, return null.
Else:
Set variable data to next token of nodes.
If data is ‘#’ return null.
Else :
Create a new instance of BinTreeNode named node and set its node data member variable to data.
Set node’s left child to return value of recursive call to deserialize(nodes).
Set node’s right child to return value of recursive call to deserialize(nodes).
Return node.


Code:

public class BinTreeSerialize {	

private void serializeBinTree(BinTreeNode root, StringBuilder serial) {
	if (root==null) {serial.append("#").append("*"); return;}
	serial.append(root.getNodeData()).append("*");
	serializeBinTree(root.getLeftChild(), serial);
	serializeBinTree(root.getRightChild(), serial);	
}

private BinTreeNode deserialize(StringTokenizer nodes) {
	if (nodes.hasMoreTokens()==false) { return null;}
	String data=nodes.nextToken();
	if (data.equals("#")) {return null;}
	BinTreeNode node=new BinTreeNode(Integer.parseInt(data));
	node.setLeftChild(deserialize(nodes));
	node.setRightChild(deserialize(nodes));
	return node;
}

private BinTreeNode deserializeBinTree(StringBuilder serial) {
	StringTokenizer nodes=new StringTokenizer(serial.toString(),"*");
	return deserialize(nodes);
}

public ArrayList<Integer> bfsTraversal(BinTreeNode root) throws Exception{
	if (root==null) {return null;}
	ArrayList<Integer> bfs=new ArrayList<Integer>();
	Queue<BinTreeNode> queue=new LinkedList<BinTreeNode>();
	queue.offer(root);
	BinTreeNode node=null;
	while (queue.size()!=0) {
		node=queue.poll();
		bfs.add(node.getNodeData());
		if (node.getLeftChild()!=null) {
			queue.offer(node.getLeftChild());
		}	
		if (node.getRightChild()!=null) {
			queue.offer(node.getRightChild());
		}
			
	}
	return bfs;
}
}

Click here to download and run code and test cases !