#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1

    Tree program help


    my following inorder traversal program is:

    #include<stdio.h>
    struct node
    {
    int info;
    struct node *left;
    struct node *right;
    };

    typedef struct node *nodeptr;

    nodeptr getnode()
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node *));
    return p;
    }
    nodeptr maketree(int x)
    {
    nodeptr p;
    p=getnode();
    p->info=x;
    p->left=NULL;
    p->right=NULL;
    return p;
    }
    void setleft(nodeptr p,int x)
    {
    if(p==NULL)
    printf("void insertion");
    else if(p->left!=NULL)
    printf("invalid insertion");
    else
    p->left=maketree(x);
    }
    void setright(nodeptr p,int x)
    {
    if(p==NULL)
    printf("void insertion");
    else if(p->right!=NULL)
    printf("invalid insertion");
    else
    p->right=maketree(x);
    }
    void intrav(nodeptr ptree)
    {
    if(ptree!=NULL)
    {
    intrav(ptree->left);
    printf("%d\n",ptree->info);
    intrav(ptree->right);
    }
    }
    int main()
    {
    nodeptr tree;
    nodeptr p,q;
    int n;
    scanf("%d",&n);
    tree=maketree(n);
    while(scanf("%d",&n)!=EOF)
    {
    p=q=tree;
    while(n!=p->info&&q!=NULL)
    {
    p=q;
    if(n<p->info)
    q=p->left;
    else
    q=p->right;
    }
    if(n<p->info)
    setleft(p,n);
    else
    setright(p,n);
    }

    intrav(tree);
    return 0;
    }

    the output is not coming correct
    can yu suggest me the errors
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Unreadable. No time to bother to try to make any sense out of it. Format it with proper indentation and with code tags.

    You cannot be bothered to describe the symptoms to us that we need to be able to solve your problem. So I cannot be bothered to try to read your mind.

    You obviously don't care, so why should we?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    Originally Posted by dwise1_aol
    Unreadable. No time to bother to try to make any sense out of it. Format it with proper indentation and with code tags.

    You cannot be bothered to describe the symptoms to us that we need to be able to solve your problem. So I cannot be bothered to try to read your mind.

    You obviously don't care, so why should we?
    #include<stdio.h>
    struct node {
    int info;
    struct node *left;
    struct node *right;
    };

    typedef struct node *nodeptr;

    nodeptr getnode()
    {
    nodeptr p;
    p = (nodeptr) malloc(sizeof(struct node *));
    return p;
    }

    nodeptr maketree(int x)
    {
    nodeptr p;
    p = getnode();
    p->info = x;
    p->left = NULL;
    p->right = NULL;
    return p;
    }

    void setleft(nodeptr p, int x)
    {
    if (p == NULL)
    printf("void insertion");
    else if (p->left != NULL)
    printf("invalid insertion");
    else
    p->left = maketree(x);
    }

    void setright(nodeptr p, int x)
    {
    if (p == NULL)
    printf("void insertion");
    else if (p->right != NULL)
    printf("invalid insertion");
    else
    p->right = maketree(x);
    }

    void intrav(nodeptr ptree)
    {
    if (ptree != NULL) {
    intrav(ptree->left);
    printf("%dn", ptree->info);
    intrav(ptree->right);
    }
    }

    int main()
    {
    nodeptr tree;
    nodeptr p, q;
    int n;
    scanf("%d", &n);
    tree = maketree(n);
    while (scanf("%d", &n) != EOF) {
    p = q = tree;
    while (n != p->info && q != NULL) {
    p = q;
    if (n < p->info)
    q = p->left;
    else
    q = p->right;
    }
    if (n < p->info)
    setleft(p, n);
    else
    setright(p, n);
    }

    intrav(tree);
    return 0;
    }
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    What, now you're trying to prove what a stupid jerk you are by posting unreadable code again? Unbelieveable!

    You know what you need to do to make your code readable. Why do you refuse to do it? You know what code tags are and how to use them!

    And you still refuse to tell us why you think there's anything wrong. What are the symptoms? Since you expect us to read your mind, then obviously you are able to read our minds, so you can just read our minds for the solution.

    You're also ignoring warnings:
    C:TEST>gcc -Wall tendo3.c
    tendo3.c: In function `getnode':
    tendo3.c:13: warning: implicit declaration of function `malloc'

    C:TEST>
    Last edited by dwise1_aol; November 14th, 2013 at 10:25 AM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I trust that you realize that you cannot exit that input loop in main. And that even if you could call intrav the output would be a big run-on mess.

    Since you are obviously able to read my mind, you also know how to fix that now.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,714
    Rep Power
    480
    Within this function you've allocated space for a mere pointer rather than space for the entire structure.


    nodeptr getnode()
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node *));
    return p;
    }

    Should be
    Code:
    #define NODE struct node
    nodeptr getnode(void) {
      nodeptr pn;
      pn = (nodeptr)malloc(sizeof(NODE));
      if (NULL == pn) {
        fputs("\ncannot give you that much//memory much too small//much much you ask much\n", stderr);
        exit(1);
      }
      return pn;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1
    Originally Posted by b49P23TIvg
    Within this function you've allocated space for a mere pointer rather than space for the entire structure.


    nodeptr getnode()
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node *));
    return p;
    }

    Should be
    Code:
    #define NODE struct node
    nodeptr getnode(void) {
      nodeptr pn;
      pn = (nodeptr)malloc(sizeof(NODE));
      if (NULL == pn) {
        fputs("\ncannot give you that much//memory much too small//much much you ask much\n", stderr);
        exit(1);
      }
      return pn;
    }
    thanks for all yur suggestions i got it

IMN logo majestic logo threadwatch logo seochat tools logo