Design an algorithm and write code to serialize and deserialize a binary tree/graph
Page 1 of 1
Design an algorithm and write code to serialize and deserialize a binary tree/graph
Serialization:
public static void serialize(TreeNode root, ArrayListNode arr){
if(root.value==null)
arr.add("#");
serialize(root.left,arr);
serialize(root.right,arr);
}
Derialization:
public static void derialization(TreeNode root, ArrayListNode arr){
while(arr.value=="#"){
arr=arr.next;
}
if(arr==null)
return;
TreeNode p=new TreeNode(arr.value);
derialization(root.left,arr.next);
derialization(root.right,arr.next);
}
public static void serialize(TreeNode root, ArrayListNode arr){
if(root.value==null)
arr.add("#");
serialize(root.left,arr);
serialize(root.right,arr);
}
Derialization:
public static void derialization(TreeNode root, ArrayListNode arr){
while(arr.value=="#"){
arr=arr.next;
}
if(arr==null)
return;
TreeNode p=new TreeNode(arr.value);
derialization(root.left,arr.next);
derialization(root.right,arr.next);
}
Re: Design an algorithm and write code to serialize and deserialize a binary tree/graph
If the tree is BST, it can be determined by a preorder traversal!
Similar topics
» You are given a TreeNode, and you have to write an algorithm which will return the no. of nodes in the tree.
» Write a program to determine if a binary tree is well ordered
» Describe an algorithm to save a Binary Search Tree (BST) to a file
» Given a directed graph, design an algorithm to find out whether there is a route be- tween two nodes.
» To get mirror image of a binary tree
» Write a program to determine if a binary tree is well ordered
» Describe an algorithm to save a Binary Search Tree (BST) to a file
» Given a directed graph, design an algorithm to find out whether there is a route be- tween two nodes.
» To get mirror image of a binary tree
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum