home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!cs.uiuc.edu!sparc0b!ctaylor
- From: ctaylor@cs.uiuc.edu (Conrad Taylor)
- Subject: What's wrong with the following code...?
- Message-ID: <BzKnzo.Gt@cs.uiuc.edu>
- Sender: news@cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- Date: Sun, 20 Dec 1992 19:11:48 GMT
- Lines: 90
-
- 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.
-
- -Con
-
- ps: My e-mail is ctaylor@silver.lcs.mit.edu or
- taylor1@wse.eecs.uic.edu.
-
-
- #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 */
-
-
-
-
- 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;
- }
-
-
-