home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!yale.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
- From: ktf@bc3.GUN.de (Klaus ter Fehn)
- Subject: Re: What's wrong with the following code...?
- Organization: private
- Date: Mon, 21 Dec 1992 11:42:59 GMT
- Message-ID: <BzLxvp.1LM@bc3.GUN.de>
- References: <BzKnzo.Gt@cs.uiuc.edu>
- Lines: 181
-
- In article <BzKnzo.Gt@cs.uiuc.edu> ctaylor@cs.uiuc.edu (Conrad Taylor) writes:
-
- > Could someone please assist me with the following code which it
- >to build a binary tree and print out the tree nodes? Thanks in advance to
- >all that reply.
-
- I think it's a common beginner's-problem, so I post it.
-
- >#include <stdio.h>
- >#include <stdlib.h>
-
- >struct node {
- > int Info;
- > struct node *LeftTree;
- > struct node *RightTree;
- >};
-
- >typedef struct node *NODEPTR;
-
- >NODEPTR Root;
-
- >void TreeInsert(int Number, NODEPTR Root);
- >void TreeBuild(NODEPTR Root);
- >void TreePrint(NODEPTR Root);
-
-
-
-
- >void TreeBuild(NODEPTR Root)
- >{
- > Root = NULL;
-
- > TreeInsert(6, Root);
- > TreeInsert(4, Root);
- > TreeInsert(8, Root);
- > TreeInsert(7, Root);
- > TreeInsert(9, Root);
- > TreeInsert(5, Root);
- > TreeInsert(3, Root);
- >} /* end TreeBuild */
-
-
- And now here's the problem: In Funktion TreeInsert You want to change the
- content of 'Root'. But You define here a local-var named 'Root'. You see,
- You're changing the COPY of this Pointer, not the Pointer itself.
- You got to give the Funktion the adress of this Pointer, if You want it
- to change the Pointer.
- I've added a working version at the end.
-
-
- >void TreeInsert(int Number, NODEPTR Root)
- >{
- > if (Root == NULL)
- > {
- > Root = (NODEPTR)malloc(sizeof(struct node));
- > Root->Info = Number;
- > Root->LeftTree = NULL;
- > Root->RightTree = NULL;
- > }
- > else if (Number < Root->Info)
- > TreeInsert(Number, Root->LeftTree);
- > else if (Number > Root->Info)
- > TreeInsert(Number, Root->RightTree);
- > /* else if (Number = Root->Info) */
- > /* do nothing since Number is already in the tree. */
- >} /* end TreeInsert */
-
-
-
-
- >void TreePrint(NODEPTR Root)
- >{
- > if (Root != NULL)
- > {
- > TreePrint(Root->LeftTree);
- > printf("%2d\n", Root->Info);
- > TreePrint(Root->RightTree);
- > }
- >}
-
-
-
-
- >int main(void)
- >{
- > printf("Start the program.\n");
- > TreeBuild(Root);
- > TreePrint(Root);
- > printf("Finish the program.\n");
-
- > return 0;
- >}
-
- And now here is a working Version:
-
- #include <stdio.h>
- #include <stdlib.h>
-
- struct node {
- int Info;
- struct node *LeftTree;
- struct node *RightTree;
- };
-
- typedef struct node *NODEPTR;
-
- NODEPTR Root;
-
- void TreeInsert(int Number, NODEPTR *Root);
- void TreeBuild();
- void TreePrint(NODEPTR Root);
-
-
-
-
- /* void TreeBuild(NODEPTR Root) */
- void TreeBuild()
- {
- Root = NULL;
-
- TreeInsert(6, &Root);
- TreeInsert(4, &Root);
- TreeInsert(8, &Root);
- TreeInsert(7, &Root);
- TreeInsert(9, &Root);
- TreeInsert(5, &Root);
- TreeInsert(3, &Root);
- } /* end TreeBuild */
-
-
-
-
- void TreeInsert(int Number, NODEPTR *Root)
- {
- if (*Root == NULL)
- {
- *Root = (NODEPTR)malloc(sizeof(struct node));
- (*Root)->Info = Number;
- (*Root)->LeftTree = NULL;
- (*Root)->RightTree = NULL;
- }
- else if (Number < (*Root)->Info)
- TreeInsert(Number, &(*Root)->LeftTree);
- else if (Number > (*Root)->Info)
- TreeInsert(Number, &(*Root)->RightTree);
- /* else if (Number = (*Root)->Info) */
- /* do nothing since Number is already in the tree. */
- } /* end TreeInsert */
-
-
-
-
- void TreePrint(NODEPTR Root)
- {
- if (Root != NULL)
- {
- TreePrint(Root->LeftTree);
- printf("%2d\n", Root->Info);
- TreePrint(Root->RightTree);
- }
- }
-
-
-
-
- int main(void)
- {
- printf("Start the program.\n");
- TreeBuild(Root);
- TreePrint(Root);
- printf("Finish the program.\n");
-
- return 0;
- }
-
- Greetings...
- --
- Klaus ter Fehn <ktf@bc3.GUN.de>
- Neanderstr. 4 {mcshh,smurf,unido}!easix!bc3!ktf
- 4000 Duesseldorf 1
- FRG / Germany Tel.: +49-211-676331
-