/**
Serialize Deserialize BST:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
*/
import java.util.*;
public class SerializeDeserializeBinarySearchTree{
// Encodes a tree to a single string.
//use a recursive approach to do the pre-order traversal
public static String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
serializeHelper(root, sb);
//System.out.println("serialized is:"+sb.toString());
return sb.toString();
}
static void serializeHelper(TreeNode root, StringBuilder sb){
if(root==null){
return;
}
sb.append(root.val).append(" ");
serializeHelper(root.left, sb);
serializeHelper(root.right, sb);
}
// Decodes your encoded data to tree.
//create a dummy node, inserter the first node as its child node
//recursively fetch the values from the encoded string and populate the nodes checking if they satisfy the min max property of BST
// Decodes your encoded data to tree.
public static TreeNode deserialize(String data) {
if(data.isEmpty()) return null;
String[] vals = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
for(int i = 1; i < vals.length; i++)
insert(root, Integer.parseInt(vals[i]));
return root;
}
private static void insert(TreeNode root, int val) {
TreeNode curr = root, parent = null;
while( curr != null) {
parent = curr;
curr = curr.val < val ? curr.right : curr.left;
}
if(parent.val < val)
parent.right = new TreeNode(val);
else
parent.left = new TreeNode(val);
}
public static void main(String args[]){
TreeNode root = new TreeNode(5);
root.left = new TreeNode(2);
root.right = new TreeNode(9);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(15);
String ser = serialize(root);
System.out.println(ser);
TreeNode n = deserialize(ser);
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(n);
while(!q.isEmpty()){
TreeNode cur = q.poll();
if(cur!=null){
System.out.println(cur.val);
if(cur.left!=null);
q.add(cur.left);
if(cur.right!=null)
q.add(cur.right);
}
}
}
}
Serialize Deserialize BST:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
*/
import java.util.*;
public class SerializeDeserializeBinarySearchTree{
// Encodes a tree to a single string.
//use a recursive approach to do the pre-order traversal
public static String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
serializeHelper(root, sb);
//System.out.println("serialized is:"+sb.toString());
return sb.toString();
}
static void serializeHelper(TreeNode root, StringBuilder sb){
if(root==null){
return;
}
sb.append(root.val).append(" ");
serializeHelper(root.left, sb);
serializeHelper(root.right, sb);
}
// Decodes your encoded data to tree.
//create a dummy node, inserter the first node as its child node
//recursively fetch the values from the encoded string and populate the nodes checking if they satisfy the min max property of BST
// Decodes your encoded data to tree.
public static TreeNode deserialize(String data) {
if(data.isEmpty()) return null;
String[] vals = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
for(int i = 1; i < vals.length; i++)
insert(root, Integer.parseInt(vals[i]));
return root;
}
private static void insert(TreeNode root, int val) {
TreeNode curr = root, parent = null;
while( curr != null) {
parent = curr;
curr = curr.val < val ? curr.right : curr.left;
}
if(parent.val < val)
parent.right = new TreeNode(val);
else
parent.left = new TreeNode(val);
}
public static void main(String args[]){
TreeNode root = new TreeNode(5);
root.left = new TreeNode(2);
root.right = new TreeNode(9);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(15);
String ser = serialize(root);
System.out.println(ser);
TreeNode n = deserialize(ser);
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(n);
while(!q.isEmpty()){
TreeNode cur = q.poll();
if(cur!=null){
System.out.println(cur.val);
if(cur.left!=null);
q.add(cur.left);
if(cur.right!=null)
q.add(cur.right);
}
}
}
}
No comments:
Post a Comment