10.C Program to display the traversal of a tree(Part B)

10.C Program to display the traversal of a tree


ALGORITHM :

Step 1: Start
Step 2: Define a structure to represent a node in the tree (e.g., TreeNode) with necessary variables (e.g., data to store the data, and left and right to store the addresses of the left and right children).
Step 3: Implement the create_node function to create a new node with the given data.
        a. Input the data for the new node.
        b. Allocate memory for the new node.
        c. Set the data of the new node to the input data.
        d. Set the left and right of the new node to NULL.
        e. Return the address of the new node.
Step 4: Implement the traversal function to display the elements of the tree using the following traversal techniques:
        a. Inorder Traversal:
               i. Traverse the left subtree using a recursive call to traversal.
               ii. Display the data of the current node.
               iii. Traverse the right subtree using a recursive call to traversal.
        b. Preorder Traversal:
               i. Display the data of the current node.
               ii. Traverse the left subtree using a recursive call to traversal.
               iii. Traverse the right subtree using a recursive call to traversal.
        c. Postorder Traversal:
               i. Traverse the left subtree using a recursive call to traversal.
               ii. Traverse the right subtree using a recursive call to traversal.
               iii. Display the data of the current node.
Step 5: Input the elements of the tree (in any order) and create the tree using create_node function accordingly.
Step 6: Display the elements of the tree using the traversal function for inorder, preorder, and postorder.
Step 7: End

SOURCE CODE :

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int data;
    struct TreeNode *left;
    struct TreeNode *right;
};

struct TreeNode *createNode(int data) {
    struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

void inorderTraversal(struct TreeNode *root) {
    if (root != NULL) {
        inorderTraversal(root->left);
        printf("%d ", root->data);
        inorderTraversal(root->right);
    }
}

void preorderTraversal(struct TreeNode *root) {
    if (root != NULL) {
        printf("%d ", root->data);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
    }
}

void postorderTraversal(struct TreeNode *root) {
    if (root != NULL) {
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        printf("%d ", root->data);
    }
}

void freeTree(struct TreeNode *root) {
    if (root != NULL) {
        freeTree(root->left);
        freeTree(root->right);
        free(root);
    }
}

int main() {
    struct TreeNode *root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    printf("Inorder Traversal: ");
    inorderTraversal(root);
    printf("\n");

    printf("Preorder Traversal: ");
    preorderTraversal(root);
    printf("\n");

    printf("Postorder Traversal: ");
    postorderTraversal(root);
    printf("\n");

    freeTree(root);

    return 0;
}


OUTPUT :

Inorder Traversal: 4 2 5 1 3 
Preorder Traversal: 1 2 4 5 3 
Postorder Traversal: 4 5 2 3 1 


No comments:

Post a Comment