About the binary tree

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yuri CHUANG

    About the binary tree

    Hi,
    Here is my code,
    #include<stdio. h>
    #include<stdlib .h>


    typedef struct BiTNode
    {
    char data;
    struct BiTNode *lchild,*rchild ;
    }BiTNode;

    void CreateBiTree(Bi TNode*);
    void PreOrder(BiTNod e*);

    int main(void)
    {
    BiTNode *p;
    p=NULL;
    CreateBiTree(p) ;
    printf("%d\n",p );
    PreOrder(p);


    system("PAUSE") ;
    return 0;
    }

    void CreateBiTree(Bi TNode* t)
    {
    char ch;
    ch=getche();
    printf("\n");
    if(ch==' ')t=NULL;
    else
    {
    t=(BiTNode*)mal loc(sizeof(BiTN ode));
    printf("%d\n",t );
    if(t==NULL)exit (-1);
    t->data=ch;
    puts("left child");
    CreateBiTree(t->lchild);
    puts("right child");
    CreateBiTree(t->rchild);
    }
    }

    void PreOrder(BiTNod e* t)
    {
    if(t!=NULL)
    {
    printf("%c ",t->data);
    PreOrder(t->lchild);
    PreOrder(t->rchild);
    }
    }

    I create the tree recursively,but in main function,the pointer is
    NULL.Could anyone help me to build a binary tree with recursion?Thank
    you!

  • Walter Roberson

    #2
    Re: About the binary tree

    In article <1148278705.782 537.290760@38g2 000cwa.googlegr oups.com>,
    Yuri CHUANG <yuri_1985@126. com> wrote:
    [color=blue]
    >Here is my code,[/color]
    [color=blue]
    >int main(void)
    >{
    > BiTNode *p;[/color]

    Here you create a variable with name p that is local to main().[color=blue]
    > p=NULL;[/color]

    Here you set that variable to an initial value.
    [color=blue]
    > CreateBiTree(p) ;[/color]

    Here you pass the *value* of the variable to CreateBiTree()
    [color=blue]
    > printf("%d\n",p );[/color]

    And here you expect the *value* of the variable to have changed.
    [color=blue]
    > PreOrder(p);
    > system("PAUSE") ;
    > return 0;
    >}[/color]
    [color=blue]
    >void CreateBiTree(Bi TNode* t)
    >{
    > char ch;
    > ch=getche();[/color]

    Here you call upon a windows-specific function getche() without
    a particularily good reason to do so, and without having declared
    getche() in any header.

    You also assign the result into a char variable directly, before
    testing it for EOF, even though EOF is not certain to be
    representable as a char .
    [color=blue]
    > printf("\n");
    > if(ch==' ')t=NULL;
    > else
    > {
    > t=(BiTNode*)mal loc(sizeof(BiTN ode));[/color]

    Here you assign a value to the variable t, which is a *copy* of
    the parameter value that you passed in from the previous level.
    In particular, when you called CreateBiTree() from main, a -copy-
    of a NULL pointer was put into t, and you then overwrite that copy.
    But overwriting the copy does not change the parameter value in
    the calling routine, so the main()'s variable named p is not going
    to be changed by what you do to t in CreateBiTree().

    [color=blue]
    > printf("%d\n",t );[/color]

    Here you attempt to print out a pointer as if were an int. That will
    work on *some* systems, but it will not work on most systems, as
    pointers are often bigger than int. It is quite common, for example,
    for a pointer to be the size of a long instead of an int. (You
    should also consider the possibility that the pointer converted to
    an int is going to turn out negative.)
    [color=blue]
    > I create the tree recursively,but in main function,the pointer is
    > NULL.Could anyone help me to build a binary tree with recursion?[/color]

    Either return the new pointer values from the routines, or else
    pass in a pointer *to* the pointer: e.g., CreateBiTree(&p )
    and then void CreateBiTree(Bi TNode** t) { *t = malloc(....) }
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers

    Comment

    • Yuri CHUANG

      #3
      Re: About the binary tree

      You give me the correct answer,thank you very much!
      But,as I've learned before,I pass the pointer not a variable as the
      parameter to the CreateBiTree function,it should be changed when return
      to the main.

      Comment

      • CBFalconer

        #4
        Re: About the binary tree

        Yuri CHUANG wrote:[color=blue]
        >
        > You give me the correct answer,thank you very much!
        > But,as I've learned before,I pass the pointer not a variable as
        > the parameter to the CreateBiTree function,it should be changed
        > when return to the main.[/color]

        This is incomprehensibl e. In general on usenet you should realize
        that readers may very well not have convenient access to previous
        articles in a thread. That means that your reply articles should
        include adequate context, so that they stand by themselves. Google
        is NOT usenet, it is only a very poor interface to the real usenet
        system. To include proper context when using google, see my sig.
        below. Please be sure to read the referenced URLs.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>
        Also see <http://www.safalra.com/special/googlegroupsrep ly/>


        Comment

        Working...