home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18755 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.2 KB  |  91 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!sunic!dkuug!aud.dk!vaxc.aud.auc.dk!fischer
  3. From: fischer@iesd.auc.dk (Lars Peter Fischer)
  4. Subject: Re: What's wrong with the following code...?
  5. In-Reply-To: ctaylor@cs.uiuc.edu's message of 20 Dec 92 19: 11:48 GMT
  6. Message-ID: <FISCHER.92Dec21223932@orange.iesd.auc.dk>
  7. Sender: news@vaxc.aud.auc.dk (USENET News System)
  8. Organization: Mathematics and Computer Science, Aalborg University
  9. References: <BzKnzo.Gt@cs.uiuc.edu>
  10. Date: Mon, 21 Dec 1992 21:39:32 GMT
  11. Lines: 78
  12.  
  13.  
  14.  [ I tried to answer by mail, but it bounced, so... ]
  15.  
  16. >>>>> "Conrad" == Conrad Taylor (ctaylor@cs.uiuc.edu)
  17.  
  18. Conrad> void TreeBuild(NODEPTR Root)
  19. Conrad> {
  20. Conrad>     Root = NULL;
  21.  
  22. Conrad>     TreeInsert(6, Root);
  23. Conrad>     TreeInsert(4, Root);
  24. Conrad>     TreeInsert(8, Root);
  25. Conrad>     TreeInsert(7, Root);
  26. Conrad>     TreeInsert(9, Root);
  27. Conrad>     TreeInsert(5, Root);
  28. Conrad>     TreeInsert(3, Root);
  29. Conrad> } /* end TreeBuild */
  30.  
  31.  
  32. Note that in the above, Root is local to the function. The
  33. modification to Root (Root = NULL) is all fine and dandy, but in a
  34. calling function, using
  35.  
  36.   TreeBuild (r)
  37.  
  38. r is *not* modified. r is passed by *value* and hence have the same
  39. value after the call as it had before the call.
  40.  
  41. The best solution is to "return" value where possible instead of using
  42. side effetcs, i.e.:
  43.  
  44.  NODEPTR TreeBuild ()
  45.  {
  46.     NODEPTR Root = NULL;
  47.  
  48.     Root = TreeInsert (6, Root);
  49.     ....
  50.     Root = TreeInsert (3, Root);
  51.  
  52.     return Root;
  53.  }
  54.  
  55. and then use
  56.  
  57.  r = TreeBuild ();
  58.  
  59. Likewise, TreeInsert would be modified to read something like
  60.  
  61.  
  62.  NODEPTR TreeInsert(int Number, NODEPTR Root)
  63.  {
  64.     if (Root == NULL)
  65.       {
  66.      Root = (NODEPTR)malloc(sizeof(struct node));
  67.      Root->Info  = Number;
  68.      Root->LeftTree = NULL;
  69.      Root->RightTree = NULL;
  70.          return Root;
  71.       }
  72.     
  73.     if (Number < Root->Info)
  74.       return TreeInsert(Number, Root->LeftTree);
  75.  
  76.     if (Number > Root->Info)
  77.       return TreeInsert(Number, Root->RightTree);
  78.  
  79.     return Root;
  80.  } 
  81.  
  82.  
  83. Since you are returning only one value, this fits well, and in my view
  84. the recursive nature of the algorithm is much simpler to understand
  85. this way.
  86.  
  87. /Lars
  88. --
  89. Lars Fischer, fischer@iesd.auc.dk | It takes an uncommon mind to think of
  90. CS Dept., Aalborg Univ., DENMARK. | these things.  -- Calvin
  91.