Binary Tree Serialization. Design an algorithm and write code to serialize and deserialize a binary tree. It's given by your own serialize method.
| // |
| // 40 |
| // 20 21 22 |
| //10 11 12 13 14 |
| // 2 |
| //serialised value is : 40 20 10 ) 11 2 ) ) ) 21 ) 22 12 ) 13 ) 14 ) ) ) |
| // A C++ program to demonstrate serialization and deserialiation of |
| // n-ary Tree |
| #include<iostream> |
| #include<vector> |
| usingnamespacestd; |
| //char MARKER=')'; |
| #defineMARKER -1 |
| /* A binary Node has key, vector of Node * for children */ |
| structNode |
| { |
| int key; |
| vector< structNode * > child; |
| }; |
| /* Helper function that allocates a new Node with the |
| given key and NULL children pointer. */ |
| Node* newNode(int key) |
| { |
| Node* temp = new Node; |
| temp->key = key; |
| return (temp); |
| } |
| // This function stores a tree in a file pointed by fp |
| voidserialize(Node *root, FILE *fp) |
| { |
| // Else, store current node and recur for its children |
| fprintf(fp, '%d ', root->key); |
| unsigned i=0; |
| while(i<root->child.size()) |
| { |
| serialize(root->child[i], fp); |
| i++; |
| } |
| fprintf(fp, '%d ', MARKER ); |
| } |
| // This function constructs a tree from a file pointed by 'fp' |
| // 40 20 10 ) 11 2 ) ) ) 21 ) 22 12 ) 13 ) 14 ) ) ) |
| voiddeSerialize(Node *&root, FILE *fp) |
| { |
| } |
| /* |
| // A simple inorder traversal used for testing the constructed tree |
| void inorder(Node *root) |
| { |
| if (root) |
| { |
| inorder(root->left); |
| printf('%d ', root->key); |
| inorder(root->right); |
| } |
| } |
| */ |
| voidprintTree(structNode * root) |
| { cout<< root->key<<''; |
| unsigned i=0;//becoz vectoe size returns unsigned int |
| while(i < (root->child).size()) |
| { |
| printTree(root->child[i]); |
| i++; |
| } |
| cout<<endl; |
| } |
| /* Driver program to test above functions*/ |
| intmain() |
| { |
| // Let us construct a tree shown in the above figure |
| structNode *root = newNode(40); |
| root->child.push_back( newNode(20) ); |
| root->child.push_back( newNode(21) ); |
| root->child.push_back( newNode(22) ); |
| root->child[0]->child.push_back(newNode(10)); |
| root->child[0]->child.push_back(newNode(11)); |
| root->child[0]->child[1]->child.push_back(newNode(2)); |
| root->child[2]->child.push_back(newNode(12)); |
| root->child[2]->child.push_back(newNode(13)); |
| root->child[2]->child.push_back(newNode(14)); |
| //level order traversal of tree |
| printTree(root); |
| // Let us open a file and serialize the tree into the file |
| FILE *fp = fopen('tree.txt', 'w'); |
| if (fp NULL) |
| { |
| puts('Could not open file'); |
| return0; |
| } |
| serialize(root, fp); |
| fclose(fp); |
| // Let us deserialize the storeed tree into root1 |
| Node *root1 = NULL; |
| fp = fopen('tree.txt', 'r'); |
| deSerialize(root1, fp); |
| /*printf('Inorder Traversal of the tree constructed from file:n'); |
| inorder(root1);*/ |
| return0; |
| } |
De serialize part is remaining to implement |
int deSerialize(Node *&root, FILE *fp) } |