DreamsCoder.com

We Code your Dreams


home > Simple code in C for Binary tree traversal: Preorder, Inorder and Postorder




Simple code in C for Binary tree traversal: Preorder, Inorder and Postorder







Click here for Live demo

Source code

Binar Tree Traversal

  • Preorder
  • Inorder
  • Postorder


  • #include 
    #include 
    
    typedef struct BST
    {
        int data;
        struct BST *left;
        struct BST *right;
    } node;
    
    node *create();
    void insert(node *, node *);
    void preorder(node *);
    void postorder(node *);
    void inorder(node *);
    
    int main()
    {
        int ch;
        node *root = NULL, *temp, *current;
        printf("
    Enter the number of Nodes you want :	");
        scanf("%d", &ch);
        printf("
    Enter %d Nodes data :
    ", ch);
        do
        {
            temp = create();
            if (root == NULL)
                root = temp;
            else
                insert(root, temp);
            ch--;
    
        } while (ch != 0);
    
        printf("
    Preorder Traversal
    ");
        preorder(root);
    
        printf("
    Inorder Traversal
    ");
        inorder(root);
    
        printf("
    Postorder Traversal
    ");
        postorder(root);
    
        printf("
    ");
    
        return 0;
    }
    
    node *create()
    {
        node *temp;
    
        temp = (node *)malloc(sizeof(node));
        scanf("%d", &temp->data);
        temp->left = temp->right = NULL;
        return temp;
    }
    
    void insert(node *root, node *temp)
    {
        if (root == NULL)
        {
            root = temp;
        }
        else
        {
    
            if (temp->data < root->data)
            {
                if (root->left != NULL)
                    insert(root->left, temp);
                else
                    root->left = temp;
            }
    
            if (temp->data > root->data)
            {
                if (root->right != NULL)
                    insert(root->right, temp);
                else
                    root->right = temp;
            }
        }
    }
    
    void preorder(node *root)
    {
        if (root != NULL)
        {
            printf("%d 	", root->data);
            preorder(root->left);
            preorder(root->right);
        }
    }
    
    void postorder(node *root)
    {
        if (root != NULL)
        {
            postorder(root->left);
            postorder(root->right);
            printf("%d 	", root->data);
        }
    }
    
    void inorder(node *root)
    {
        if (root != NULL)
        {
            inorder(root->left);
            printf("%d 	", root->data);
            inorder(root->right);
        }
    }

    Label - data-structure





    by - Chinmay Mahajan





    Are you looking for something else?


    Suggested Programs




    Printing the singly linked list in reverse order using recursion in C


    Singly Link List program in C along with code - [Explained in Video]


    Breadth first search in C Language


    Program To show whether expression have matching parenthesis or not using C++


    Dynamic Queue program using C++