Skip to main content

Tree

 




#include <stdio.h>
#include <stdlib.h>
struct treeN
{
    int data;
    struct treeN *left;
    struct treeN *right;
};
struct treeN *create(int data)
{
    struct treeN *a = malloc(sizeof(struct treeN));
    a->data = data;
    a->left = NULL;
    a->right = NULL;
    return a;
}
void inorder(struct treeN *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf(" %d ", root->data);
        inorder(root->right);
    }
}
void preorder(struct treeN *root)
{
    if (root != NULL)
    {
        printf(" %d ", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}
void postorder(struct treeN *root)
{
    if (root != NULL)
    {
        postorder(root->left);
        postorder(root->right);
        printf(" %d ", root->data);
    }
}
int main()
{
    struct treeN *root = create(1);
    struct treeN *a = create(4);
    struct treeN *b = create(3);
    struct treeN *c = create(9);
    struct treeN *d = create(5);

    root->left = a;
    root->right = b;
    a->left = c;
    a->right= d;
   
    preorder(root);
    printf("\n");
    postorder(root);
    printf("\n");
    inorder(root);
    return 0;
}


Comments