diff --git a/Code/C++/serialize_and_deserialize_binary_tree.cpp b/Code/C++/serialize_and_deserialize_binary_tree.cpp new file mode 100644 index 000000000..04fb4ae51 --- /dev/null +++ b/Code/C++/serialize_and_deserialize_binary_tree.cpp @@ -0,0 +1,105 @@ +// 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 tree. + +#include +using namespace std; + +struct node{ + int next; + struct node* left, *right; +}; + +node* newNode(int next){ + node* temp = new node; + temp->next = next; + temp->left = temp->right = NULL; + return (temp); +} + +// This function stores a tree in a file +void serialize(node *root, FILE *f){ + if(root == NULL){ + fprintf(f, "%d ", -1); + return; + } + + // Else, store current node and call function for it's children + fprintf(f, "%d ", root->next); + serialize(root->left, f); + serialize(root->right, f); +} + +// This function constructs a tree from a file +void deserialize(node *&root, FILE *f){ + // Read next item from file. If theere are no more items + // item is -1, then return + int val; + if(!fscanf(f, "%d ", &val) || val == -1) + return; + + // Else create node with this item and call function for it's children + root = newNode(val); + deserialize(root->left, f); + deserialize(root->right, f); +} + +// A simple inorder traversal used for testing the constructed tree +void inorder(node *root){ + if(root){ + inorder(root->left); + cout<next<<" "; + inorder(root->right); + } +} +int main(){ + struct node *root = newNode(14); + root->left = newNode(7); + root->right = newNode(21); + + //serialize the tree into the file + FILE *f = fopen("tree.txt", "w"); + if(f == NULL){ + cout<<"Could not open file"; + return 0; + } + serialize(root, f); + fclose(f); + + // deserialize the storeed tree + node *root1 = NULL; + f = fopen("tree.txt", "r"); + deserialize(root1, f); + inorder(root1); + + return 0; +} + +// output : 7 14 21 + +// Test Cases : + +// Input: root = [1,2,3,null,null,4,5] +// Output: [1,2,3,null,null,4,5] + +// Input: root = [] +// Output: [] + +// Input: root = [1] +// Output: [1] + +// Input: root = [1,2] +// Output: [1,2] + + + +// Time complexity : In both serialization and deserialization functions, +// we visit each node exactly once, thus the time complexity is O(N), +// where N is the number of nodes, i.e. the size of tree. + +// Space complexity : In both serialization and deserialization functions, +// we keep the entire tree, either at the beginning or at the end, +// therefore, the space complexity is O(N). + diff --git a/Tree/readme.md b/Tree/readme.md index 6b9e980fd..447f7ed4f 100644 --- a/Tree/readme.md +++ b/Tree/readme.md @@ -78,6 +78,7 @@ that, this property should be satisfied at every node in the tree. * Spiral Traversal of Binary Tree ----> [C++](/Code/C++/spiral_traversal_of_binary_tree.cpp) * Distance between two nodes of a binary tree ----> [C++](/Code/C++/distance_between_two_nodes_of_BT.cpp) * Searching in BST ----> [C++](/Code/C++/searching_in_bst.cpp) | [Java](Code\Java\Searching_in_BST.Java) +* Serialize and Deserialize Binary Tree ----> [C++](/Code/C++/serialize_and_deserialize_binary_tree.cpp) * Threaded Tree ----> [C++](/Code/C++/threaded_binary_tree.cpp) * Top-View of a Binary tree ----> [C++](/Code/C++/Top-View.cpp)