This C program demonstrates how to serialize a binary tree into a file and deserialize it back into a tree structure using pre-order traversal.

Program Explanation

The program is structured into several functions:

  • Node Structure: Defines the structure of a binary tree node.
  • Serialize Function: Writes the tree to a file in a pre-order traversal. Null pointers are recorded as markers.
  • Deserialize Function: Reads the tree structure from a file and reconstructs the binary tree.
  • Utility Functions: Includes functions for creating new nodes and inserting nodes into the tree.
  • Main Function: Demonstrates serialization and deserialization processes.

Program Code

// Include necessary headers
#include <stdio.h>
#include <stdlib.h>

// Define the structure for tree nodes
typedef struct node {
    int data;
    struct node* left;
    struct node* right;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

// Function to serialize the binary tree
void serialize(Node* root, FILE* file) {
    if (root == NULL) {
        fprintf(file, "# ");
        return;
    }
    fprintf(file, "%d ", root->data);
    serialize(root->left, file);
    serialize(root->right, file);
}

// Function to deserialize the binary tree
Node* deserialize(FILE* file) {
    int val;
    char ch;

    if (!fscanf(file, "%d", &val)) {
        fscanf(file, "%c", &ch);
        return NULL;
    }

    Node* node = createNode(val);
    node->left = deserialize(file);
    node->right = deserialize(file);
    return node;
}

// Main function to demonstrate serialization and deserialization
int main() {
    Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    // Open file for writing
    FILE* file = fopen("tree.txt", "w");
    if (file == NULL) {
        printf("Failed to open file\\n");
        return 1;
    }
    serialize(root, file);
    fclose(file);

    // Open file for reading
    file = fopen("tree.txt", "r");
    if (file == NULL) {
        printf("Failed to open file\\n");
        return 1;
    }
    Node* deserializedRoot = deserialize(file);
    fclose(file);

    // Printing to verify, actual implementation of display function needed
    printf("Deserialized tree root is %d\\n", deserializedRoot->data);
    return 0;
}
    

 

Key Components of the Program:

  • Node Structure: Defines the structure of the nodes making up the binary tree.
  • createNode Function: Allocates and initializes a new tree node with data.
  • serialize Function: Recursively traverses the tree in a pre-order manner and writes each node’s value to a file. If a node is NULL, it writes a special marker (#).
  • deserialize Function: Reads values from the file, reconstructing the tree by creating nodes where data is available and skipping where markers indicate NULL.
  • Main Function: Sets up a sample tree, serializes it to a file, then reads the file to deserialize it back into a tree structure, and verifies the result by printing the root of the deserialized tree.

This program demonstrates a simple method to persist and recover a binary tree structure, using file operations combined with tree traversal algorithms.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)