stack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeep rajpoot
    New Member
    • Sep 2006
    • 5

    stack

    I have to make the program of inorder ,preorder,posto rder traversal of the binary search tree using recursion in stacks.I have made it by recursion only.How can I make this .
    I have written the code for the recursive method.

    void gettree(struct node **, int);
    void inorder(struct node *);
    void postorder(struc t node *);
    void preorder(struct node *);
    #include<stdio. h>
    #include<conio. h>
    #include<alloc. h>
    struct node
    {
    struct node *left;
    struct node *right;
    int data;

    };
    void main()
    {
    struct node *bt;
    int a,n,i;
    clrscr();
    bt=NULL;
    printf("\n\nEnt er the no of the nodes you want to enter :");
    scanf("%d",&n);
    for(i=0;i<n;i++ )
    {
    printf("\n\nEnt er the elments of the tree :");
    scanf("%d",&a);
    gettree (&bt,a);
    }
    printf("\n\nEle ments in INORDER are : \n\n");
    inorder(bt);
    printf("\n\nEle ments in PREORDER are : \n\n");
    preorder(bt);
    printf("\n\nEle ments in POSTORDER are : \n\n");
    postorder(bt);
    getch();
    }

    void gettree(struct node **root,int a)
    {
    struct node *temp;
    if(*root==NULL)
    {
    temp=(struct node*)malloc(si zeof(struct node));
    temp->left=NULL;
    temp->right=NULL;
    temp->data=a;
    *root=temp;
    return;
    }
    if((*root)->data>a)
    gettree(&((*roo t)->left),a);
    else
    gettree(&((*roo t)->right),a);

    }
    void inorder(struct node *bt)
    {
    if(bt!=NULL)
    {
    inorder(bt->left);
    printf("[ %d ]\t",bt->data);
    inorder(bt->right);

    }
    else
    return ;
    }

    void postorder(struc t node *bt)
    {
    if(bt!=NULL)
    {
    postorder(bt->left);
    postorder(bt->right);
    printf("[ %d ]\t",bt->data);
    }
    else
    return ;
    }

    void preorder(struct node *bt)
    {
    if(bt!=NULL)
    {
    printf("[ %d ]\t",bt->data);
    preorder(bt->left);
    preorder(bt->right);

    }
    else
    return ;
    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Originally posted by pradeep rajpoot
    I have made it by recursion only. How can I make this .
    Is that the question? If so, I hope you clarify.

    Comment

    Working...