Friday 18 October 2024

Traversal -Create Node,Preorder,PostOrder,Inorder


  • #include <stdio.h>  

  • #include <stdlib.h>  

  •   

  • struct node {  

  •     int element;  

  •     struct node* left;  

  •     struct node* right;  

  • };  

  •   

  • /*To create a new node*/  

  • struct node* createNode(int val)  

  • {  

  •     struct node* Node = (struct node*)malloc(sizeof(struct node));  

  •     Node->element = val;  

  •     Node->left = NULL;  

  •     Node->right = NULL;  

  •   

  •     return (Node);  

  • }  

  •   

  • /*function to traverse the nodes of binary tree in preorder*/  

  • void traversePreorder(struct node* root)  

  • {  

  •     if (root == NULL)  

  •         return;  

  •     printf(" %d ", root->element);  

  •     traversePreorder(root->left);  

  •     traversePreorder(root->right);  

  • }  

  •   

  •   

  • /*function to traverse the nodes of binary tree in Inorder*/  

  • void traverseInorder(struct node* root)  

  • {  

  •     if (root == NULL)  

  •         return;  

  •     traverseInorder(root->left);  

  •     printf(" %d ", root->element);  

  •     traverseInorder(root->right);  

  • }  

  •   

  • /*function to traverse the nodes of binary tree in postorder*/  

  • void traversePostorder(struct node* root)  

  • {  

  •     if (root == NULL)  

  •         return;  

  •     traversePostorder(root->left);  

  •     traversePostorder(root->right);  

  •     printf(" %d ", root->element);  

  • }  

  •   

  •   

  • int main()  

  • {  

  •     struct node* root = createNode(36);  

  •     root->left = createNode(26);  

  •     root->right = createNode(46);  

  •     root->left->left = createNode(21);  

  •     root->left->right = createNode(31);  

  •      

  •     printf("\n The Preorder traversal of given binary tree is -\n");  

  •     traversePreorder(root);  

  •       

  •     printf("\n The Inorder traversal of given binary tree is -\n");  

  •     traverseInorder(root);  

  •       

  •     printf("\n The Postorder traversal of given binary tree is -\n");  

  •     traversePostorder(root);  

  •   

  •     return 0;  

  • }    


Without structure

#include <stdio.h>

#include <stdlib.h>


#define MAX_NODES 100  // Maximum number of nodes in the tree


// Global arrays to represent tree elements and relationships

int element[MAX_NODES];   // Stores node values

int left[MAX_NODES];      // Stores index of the left child (-1 if no child)

int right[MAX_NODES];     // Stores index of the right child (-1 if no child)


// Function to create a new node at the given index

void createNode(int index, int val, int leftChild, int rightChild) {

    element[index] = val;           // Store the element at the given index

    left[index] = leftChild;         // Store the left child index (-1 if no child)

    right[index] = rightChild;       // Store the right child index (-1 if no child)

}


// Preorder traversal (Root -> Left -> Right)

void traversePreorder(int index) {

    if (index == -1) return;  // Base case: no node to traverse


    printf(" %d ", element[index]);  // Visit root

    traversePreorder(left[index]);   // Visit left subtree

    traversePreorder(right[index]);  // Visit right subtree

}


// Inorder traversal (Left -> Root -> Right)

void traverseInorder(int index) {

    if (index == -1) return;  // Base case: no node to traverse


    traverseInorder(left[index]);    // Visit left subtree

    printf(" %d ", element[index]);  // Visit root

    traverseInorder(right[index]);   // Visit right subtree

}


// Postorder traversal (Left -> Right -> Root)

void traversePostorder(int index) {

    if (index == -1) return;  // Base case: no node to traverse


    traversePostorder(left[index]);   // Visit left subtree

    traversePostorder(right[index]);  // Visit right subtree

    printf(" %d ", element[index]);   // Visit root

}


int main() {

    // Create nodes using arrays instead of structures

   // Create binary tree nodes

    createNode(0, 36, 1, 2);  // Root node with value 36

    createNode(1, 26, 3, 4);  // Node 26 with left child 21 and right child 31

    createNode(2, 46, -1, -1);  // Node 46 with no children

    createNode(3, 21, -1, -1);  // Node 21 with no children

    createNode(4, 31, -1, -1);  // Node 31 with no children

  


    printf("\nThe Preorder traversal of the binary tree is:\n");

    traversePreorder(0);  // Start from the root (index 0)


    printf("\n\nThe Inorder traversal of the binary tree is:\n");

    traverseInorder(0);  // Start from the root (index 0)


    printf("\n\nThe Postorder traversal of the binary tree is:\n");

    traversePostorder(0);  // Start from the root (index 0)


    return 0;

}