Menu

Data Structures [ Lab Programs ]


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;
int top=-1;
struct Node *s[40];

//push into stack
int push(struct Node *x)
{
    s[++top]=x;
}

//pop from stack
struct Node* pop()
{
    struct Node *x=s[top--];
    return(x);
}

// 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)
{
    struct Node *ptr;
    ptr=root;
    if(root==NULL){
        printf("\nTree is empty");
    }
    else
    {
        push(root);
        while(top!=-1)
        {
            ptr=pop();
            if(ptr!=NULL)
            {
               printf("%d ",ptr->data);
               push(ptr->right);
               push(ptr->left);
            }
        }
    }
}


void inOrder(struct Node *root)
{
    struct Node *ptr;
    ptr=root;
    if(root==NULL)
    {
        printf("\nTree is empty");
    }
    else
    {
        while(top!=-1||ptr!=NULL)
        {
            if(ptr!=NULL)
            {
                push(ptr);
                ptr=ptr->left;
            }
            else{
                ptr=pop();
                printf("%d ",ptr->data);
                ptr=ptr->right;
            }
        }
    }
}


void postOrder(struct Node *root)
{
    struct Node *ptr,*temp;
    ptr=root;
    temp=NULL;
    if(root==NULL)
    {
        printf("\nTree is empty");
    }
    else{
        while(ptr->left!=NULL)
        {
           push(ptr);
           ptr=ptr->left;
        }
        while(ptr!=NULL){
            if(ptr->right==NULL||ptr->right==temp)
            {
                printf("%d ",ptr->data);
                temp=ptr;
                ptr=pop();
            }
            else
            {
                push(ptr);
                ptr=ptr->right;
                while(ptr->left!=NULL)
                {
                    push(ptr);
                    ptr=ptr->left;
                }
            }
        }
    }
}

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 :

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 Radix sorting methods to sort a given list of integers in ascending order    View Solution

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

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

11. Write a program that implements Tree sorting methods to sort a given list of integers in ascending order    View Solution

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

13. Write a program to implement the tree traversal methods using Non Recursive    View Solution

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

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

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

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

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

19. Write a program to implement the following Hash Functions: i) Division Method, ii) Multiplication Method, iii) Mid-square Method, iv) Folding Method    View Solution