×

Serialize and Deserialize a Binary Tree

Implementation

// Writing a C++ program to check the serialization and deserialization of binary tree.  
#include <iosstream>
/* A binary tree node contains a key and a pointer to the left and right child. */
struct Nod
{
int ky;
struct Nod* Lft, *Rt;
};


/* We have a new function that helps us in allocating a new node to a given key and filling the Null value in the left and right pointers.*/
struct Nod* newNod(int ky)
{
struct Nod* temp = nw Nod();
temp->ky = ky;
temp->Lft = temp->Rt = NILL;
return (temp);
}


// Creating a function that will store the tree value in a particular file that is directed by fp. 
void serialize(Nod *root, FILE *fp)
{
// if we find out that the current value is NILL then we have to store MARK.
if (root == NILL)
{
fprintf(fp, "%d ", MARK);
return;
}


//If not, we have to store the current value and recur the child nodes. 
fprintf(fp, "%d ", root->ky);
serialize(root->Lft, fp);
serialize(root->Rt, fp);
}


// This function constructs a tree from a file pointed by 'fp'
void deSerialize(Nod *&root, FILE *fp)
{
// Read the next item from the file. If there are no more items or next
// item is a MARK, then return
int val;
if ( !fscanf(fp, "%d ", &val) || val == MARK)
return;


// Else, create a Nod with this item and recur for children
root = newNod(val);
deSerialize(root->Lft, fp);
deSerialize(root->Rt, fp);
}


// A simple in_order traversal is used for testing the constructed tree
void in order (Nod *root)
{
if (root)
{
in_order(root->Lft);
printf("%d ", root->ky);
in_order(root->Rt);
}
}


/* Driver program to test above functions*/
int main()
{
// Let us construct a tree shown in the above figure
struct Nod *root = newNod(20);
root->Lft = newNod(8);
root->Rt = newNod(22);
root->Lft->Lft = newNod(4);
root->Lft->Rt = newNod(12);
root->Lft->Rt->Lft = newNod(10);
root->Lft->Rt->Rt = newNod(14);


// Let us open a file and serialize the tree into the file
FILE *fp = open("tree.txt", "w");
if (fp == NILL)
{
puts("Could not open file");
return 0;
}
serialize(root, fp);
fclose(fp);


//Let’s begin the unravelling of the stored and occupied tree in root1. 
Nod *root1 = NILL;
fp = fopen("tree.txt", "g");
deSerialize(root1, fp);


printf("In_order Traversal of the tree constructed from file:\n");
in_order(root1);


return 0;
}

Output:

Serialize and Deserialize a Binary Tree

Example 2)

const int MARK = numeric_limits<int>::minimum();
void serialize(B_Tree_Nod* Nod, osstream& sstream) {


if (Nod == NILLpointer) {
sstream.write((char*) &MARK, sizeof(MARK));
return;
}


sstream.write((char*) &Nod->record, sizeof(Nod->record));
serialize(Nod->Lft, sstream);
serialize(Nod->Rt, sstream);
}


B_Tree_Nod* deserialize(isstream& sstream) {
if (sstream.of()) {
return NILLpointer;
}
int val;
ssstream.read((char*) &val, sizeof(val));
if (val == MARK) {
return NILLpointer;
}


B_Tree_Nod* pNod = new B_Tree_Nod(val);
pNod->Lft = deserialize(ssstream);
pNod->Rt = deserialize(ssstream);


return pNod;
}


void test(vector<int>& v, bool display_output = false) {
cout << "Create BST" << endl;
B_Tree_Nod* root = create_BST(v);
if (display_output) {
display_preorder(root);
}


cout << "Start Serialize" << endl;
ofsstream outfile("temp.class", ios::binary);
serialize(root, outfile);
outfile.close();


cout << "Start De-Serialize" << endl;
ifsstream infile("temp.class", ios::binary);
B_Tree_Nod* root2 = deserialize(infile);
infile.close();


if (display_output) {
cout << "\nAfter:\n";
display_preorder(root2);
cout << endl << endl;
}


assert(are_identical_trees(root, root2));
}


int main(int argc, char const *argv[]) {
vector<int> v = { 50, 25, 100 };
test(v, true);
v = {10 , 3, 8, 45, 9, 35, 69, 15, 12, 25, 24, 27, 13};
test(v, true);


v = create_random_vector(100);
test(v, true);


v = create_random_vector(2000000,
numeric_limits<int>::max() - 10);
cout << "\nRun big test" << endl;
test(v, false);


return 0;
}

Output:

Serialize and Deserialize a Binary Tree
Serialize and Deserialize a Binary Tree
Serialize and Deserialize a Binary Tree
Serialize and Deserialize a Binary Tree
Serialize and Deserialize a Binary Tree

Example 3)

class serializeBT{
private static final int MARK = Integer.MINIMUM_VALUE;


public static void serialize(B_Tree_Nod Nod,
ObjectOutputSstream sstream)
throws java.io.IOException {
if (Nod == NILL) {
sstream.writeInt(MARK);
return;
}


sstream.writeInt(Nod.record);
serialize(Nod.Lft, sstream);
serialize(Nod.Rt, sstream);
}


public static B_Tree_Nod deserialize(
ObjectInputSstream sstream) throws java.io.IOException {
int val = sstream.readInt();
if (val == MARK) {
return NILL;
}


B_Tree_Nod Nod = new B_Tree_Nod(val);
Nod.Lft = deserialize(sstream);
Nod.Rt = deserialize(sstream);
return Nod;
}
public static void main(String[] args) {
try{
B_Tree_Nod root = BinaryTree.create_random_BST(15,100);


BinaryTree.display_in_order(root);


ByteArrayOutputSstream baosstream = new ByteArrayOutputSstream();
ObjectOutputSstream sstream = new ObjectOutputSstream(baosstream);
serialize(root, sstream);
sstream.close();


ByteArrayInputSstream baisstream = new ByteArrayInputSstream(
baosstream.toByteArray());
ObjectInputSstream isstream = new ObjectInputSstream(baisstream);
B_Tree_Nod root_deserialized = deserialize(isstream);


System.out.println("\nResult");
BinaryTree.display_in_order(root_deserialized);


System.out.println(BinaryTree.is_identical_tree(root, 
root_deserialized));


}
catch(Exception ex){
ex.printStackTrace();
}
}
} 

Output:

Serialize and Deserialize a Binary Tree

Example 4)

MARK = sys.maxsize
def serialize(Nod, sstream):
if Nod == None:
sstream.dump(MARK);
return
sstream.dump(Nod.record);
serialize(Nod.Lft, sstream)
serialize(Nod.Rt, sstream)


def deserialize(sstream):
Try:
Record = pickle.load(sstream)
if record == MARK:
return None


Nod = B_Tree_Nod(record);
Nod.Lft = deserialize(sstream)
Nod.Rt = deserialize(sstream)
return Nod
except pickle.UnpicklingError:
return None


root = create_random_BST(15)
display_level_order(root)
output = open('record.class', 'wb')
p = pickle.Pickler(output)
serialize(root, p)
output.close()
input2 = open('record.class','rb')
root_deserialized = deserialize(input2)
display_level_order(root_deserialized)
assert is_identical_tree(root, root_deserialized), "Trees should be identical"

output:

Serialize and Deserialize a Binary Tree

Related Topics

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

What is B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

6 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

6 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Post-order traversal in a binary tree

We all know that postorder is a form of tree traversal to visit the tree's nodes, and it helps us reach out to the tree's nodes. Postorder means visiting the...

4 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.