Search & Find out

Thursday, December 3, 2009

Tree search ( inorder, preorder & postorder )

 The tree is pre-determined , it shows In order , Pre order & Post order  tree search..........

 

#include
#include
struct node
{
    struct node *left;
    struct node *right;
    char data;
};
struct node *buildtree(int);
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);
char arr[]={'A', 'B', 'C', 'D', 'E', 'F', 'G','/0', '/0', 'H'  };
int lc[]={1, 3, 5, -1, 9, -1, -1, -1, -1, -1};
int rc[]={2, 4, 6, -1, -1, -1, -1, -1, -1, -1, -1};
void main()
{
    struct node *root;
    clrscr();
    root=buildtree(0);
    printf("Inorder Travesal\n");
    inorder(root);
    printf("\nPostorder Traversal\n");
    postorder(root);
    printf("\nPreorder Traversal\n");
    preorder(root);
    getch();
}
struct node *buildtree(int index)
{
    struct node *temp=NULL;
    if(index!=-1)
    {
        temp=(struct node *)malloc(sizeof(struct node));
        temp->left=buildtree(lc[index]);
        temp->data=arr[index];
        temp->right=buildtree(rc[index]);
    }
    return(temp);
}
void inorder(struct node *root)
{
    if(root!=NULL)
    {
        inorder(root->left);
        printf("%c\t", root->data);
        inorder(root->right);
    }
}
void postorder(struct node *root)
{
    if(root!=NULL)
    {
        postorder(root->left);
        postorder(root->right);
        printf("%c\t", root->data);
    }
}
void preorder(struct node *root)
{
    if(root!=NULL)
    {
        printf("%c\t", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}



Download the executable (.exe)  file for this program..
download

0 comments:

Post a Comment

 

My Blog List

Term of Use

Copyright © 2009 Black Nero is Designed by Ipietoon Sponsored by Online Business Journal