#include"stdio.h"
#include"stdlib.h"
#define N 3
struct node 
{
 int info;
 struct node *child[N];
};
struct node *Root=NULL,*dRoot=NULL;
int dIndex=0,sIndex=0;
struct node* createNode(int data)              //Function to create a new node
{
 struct node * root;
 root=(struct node*)malloc(sizeof(struct node));
 root->info=data;
 for(int k=0;k<N;k++)
 root->child[k]=NULL;
 return root;
}
struct node * createTree()                    //Function to create Tree 
{
 struct node * root;
 root=createNode(28);
 root->child[0]=createNode(22);
 root->child[1]=createNode(8);
 root->child[2]=createNode(12);
 root->child[0]->child[0]=createNode(2);
 root->child[0]->child[1]=createNode(7);
 root->child[1]->child[0]=createNode(13);
 root->child[2]->child[0]=createNode(19);
 root->child[2]->child[1]=createNode(25);
 root->child[2]->child[2]=createNode(24);
return root;
}
void serialize(struct node* root,int arr[])      //Function to serialize Tree 
{
 if(root==NULL)
 {
 arr[sIndex]=-1;
 sIndex++;
 return;
 }
 arr[sIndex]=root->info;
 sIndex++;
 for(int k=0;k<N;k++)
 serialize(root->child[k],arr);
}
struct node* deserialize(struct node * root,int arr[]) //Function for deserialization 
{
 if(arr[dIndex]==(-1) || dIndex==sIndex-1)
 {
 dIndex++;
 return NULL;
 }
 root=createNode(arr[dIndex]);
 dIndex++;
 for(int k=0;k<N;k++)
 {
 root->child[k]=deserialize(root->child[k],arr);
 }
return root;
}
void Travers(struct node* root)         //Function to traverse Tree 
{
 if(root==NULL)
 return;
 printf(" %d",root->info);
 for(int k=0;k<N;k++)
 Travers(root->child[k]);
}
void main(){
 int array[50];
 Root=createTree();                   //Creating Tree and address of root node returns to Root pointer 
 printf("The elements of created tree : ");
 Travers(Root);                      //Displaying the tree by traversing
 serialize(Root,array);              //Serialize the tree 
 printf("\nThe elements of tree in serialized form : ");
 for(int j=0;j<sIndex;j++)
 printf("%d ",array[j]);            //Displaying the Tree in serialized form
 printf(" \nThe elements of tree in deserialized form : ");
 dRoot=deserialize(dRoot,array);    //Deserialing 
 Travers(dRoot);                    //Displaying the Tree after retrieving after deserialization 
}