Menu

[R22] Data Structures Lab Manual [ Lab Programs ]


Aim:

Write a program to implement the tree traversal methods using Recursive

Solution :

C program to implement the tree traversal methods using Recursive

Tree Traversal using Recursive
#include <stdio.h>
#include <stdlib.h>

// Definition of a node in a binary tree
 struct Node {
    int data;
    struct Node *left;
    struct Node *right;
}*root=NULL;

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

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

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

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


int main() {
    // Constructing a binary tree
    //       1
    //      / \
    //     2   3
    //    / \
    //   4   5
    root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    /* Traversals
    printf("Pre-order traversal: ");
    preOrder(root);
    printf("\n"); */

    printf("In-order traversal: ");
    inOrder(root);
    printf("\n");

   /* printf("Post-order traversal: ");
    postOrder(root);
    printf("\n"); */

    return 0;
}

OUTPUT
Pre-order traversal: 1 2 4 5 3 
In-order traversal: 4 2 5 1 3 
Post-order traversal: 4 5 2 3 1

Related Content :

Data Structures Lab Programs

1) Write a program that uses functions to perform the following operations on singly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

2) Write a program that uses functions to perform the following operations on doubly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

3) Write a program that uses functions to perform the following operations on circular linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

4) Write a program that implement Stack (its operations) using Array View Solution

5) Write a program that implement Stack (its operations) using Linked List (Pointer) View Solution

6) Write a program that implement Queue(its operations) using Array View Solution

7) Write a program that implement Queue (its operations) using Linked List (Pointer) View Solution

8) Write a program that implements Quick sort sorting methods to sort a given list of integers in ascending order View Solution

9) Write a program that implements Merge sort sorting methods to sort a given list of integers in ascending order View Solution

10) Write a program that implements Heap sort sorting methods to sort a given list of integers in ascending order View Solution

11) Write a program to implement the tree traversal methods using Recursive View Solution

12) Write a program to implement the tree traversal methods using Non Recursive View Solution

13) Write a program to implement Binary Search Tree (its operations) View Solution

14) Write a program to implement AVL Tree (its operations) View Solution

15) Write a program to implement Red - Black Tree (its operations) View Solution

16) Write a program to implement B Trees (its operations) View Solution

17) Write a program to implement B+ Trees (its operations) View Solution

18) Write a program to implement the graph traversal methods (Breadth First Search) View Solution

19) Write a program to implement the graph traversal methods (Depth First Search) View Solution

20) Write a program to Implement a Pattern matching algorithms using Boyer- Moore View Solution

21) Write a program to Implement a Pattern matching algorithms using Knuth-Morris-Pratt View Solution