User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,439 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,625 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1086 | Replies: 8
Reply
Join Date: May 2007
Location: China
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Question What's the meaning of the "return" here?

  #1  
Jun 5th, 2007
This codeblock comes from Mr Weiss's book "Data Sturucture and Algorithm Analysis in C++"(Third Edition). I have some questions about it.
  1. /**
  2. *This is a member function from the BinarySearchTree class.
  3. *And It try to find whether there's a element equal to x in
  4. *the binary search tree.
  5. */
  6. bool BinarySearchTree::contains(const int & x, BinaryNode *t){
  7. if(t==NULL)
  8. return false;
  9. /**
  10. *Weiss put the following two sentences to the end of this
  11. *codeblock with an "else".But I think put it here can make
  12. *the code more readable.Is it an error?
  13. */
  14. if(x == t->element)
  15. return true;
  16. /**
  17. *When I implemented these, I haven't added
  18. *"return" before recursive functions.
  19. *Why must add "return" in the following sentences?
  20. *How many times will the "return" really
  21. *return when the promgram run?
  22. *What will be happened,if I delete these "return"?
  23. */
  24. if(x < t->element)
  25. return contains(x,t->left);
  26. else if(t->element < x )
  27. return contains(x,t->right);
  28. }

My questions have put in the codeblock.I know that my English is bad,if you find some errors in my English syntax,please help me too! As you know,it's impossible to study C++ well without English.
Thanks ^_^
Last edited by WaltP : Jun 5th, 2007 at 1:20 am. Reason: Use CODE tags, not QUOTE tags for your code.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: What's the meaning of the "return" here?

  #2  
Jun 5th, 2007
Originally Posted by meiyantao View Post
This codeblock comes from Mr Weiss's book "Data Sturucture and Algorithm Analysis in C++"(Third Edition). I have some questions about it.
First of all, please format your code so it can be followed. I'll format it as I go...

  1. /**
  2. * This is a member function from the BinarySearchTree class.
  3. * And It try to find whether there's a element equal to x in
  4. * the binary search tree.
  5. */
  6. bool BinarySearchTree::contains(const int & x, BinaryNode *t)
  7. {
  8. if (t == NULL)
  9. {
  10. return false;
  11.  
  12. }
  13. /**
  14. * Weiss put the following two sentences to the end of this
  15. * codeblock with an "else".But I think put it here can make
  16. * the code more readable.Is it an error?
  17. */
  18. if (x == t->element)
  19. {
  20. return true;
  21. }
  22. //// The way the function is written, it doesn't seem to matter
  23. //// where this IF goes since each recursive call is part of a
  24. //// RETURN statement.
  25.  
  26. /**
  27. *When I implemented these, I haven't added
  28. *"return" before recursive functions.
  29. *Why must add "return" in the following sentences?
  30. //// Because when the function finally gets to the end of
  31. //// the search, the value is passed back via the RETURN
  32. //// statements. So the function returns the value, and
  33. //// upon returning returns the value again. It just keeps
  34. //// passing the value back to the previous recursion
  35.  
  36. *How many times will the "return" really
  37. *return when the promgram run?
  38. //// Exactly the same number of time the function is called...
  39.  
  40. *What will be happened,if I delete these "return"?
  41. //// The function won't work. Each return below actually
  42. //// calls the function again with the next node value to
  43. //// be tested.
  44. */
  45. if (x < t->element)
  46. {
  47. return contains(x,t->left);
  48. }
  49. else
  50. if (t->element < x )
  51. {
  52. return contains(x,t->right);
  53. }
  54. }

So here's a simplified version of the function.
  1. bool BinarySearchTree::contains(const int & x, BinaryNode *t)
  2. {
  3. if (t == NULL)
  4. {
  5. return false;
  6. }
  7.  
  8. // if this element is before the one we're looking for,
  9. // call the function again with the next value (left)
  10. if (x < t->element)
  11. {
  12. return contains(x,t->left);
  13. }
  14. // This element is not before the one we're looking for...
  15.  
  16. // if this element is after the one we're looking for,
  17. // call the function again with the previous value (right)
  18. if (t->element < x )
  19. {
  20. return contains(x,t->right);
  21. }
  22. // This element is not before nor after. Therefore it IS
  23. // the one we're looking for. We found it so return TRUE
  24. return true;
  25. }
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: May 2007
Location: China
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Re: What's the meaning of the "return" here?

  #3  
Jun 5th, 2007
Thank you very much!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: What's the meaning of the "return" here?

  #4  
Jun 5th, 2007
Originally Posted by meiyantao View Post
As you know,it's impossible to study C++ well without English.
Thanks ^_^


Probably because English has become the universal language of business. Anyone who needs to conduct business in todays global world needs to know English.

And the English you posted looks great to me.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2007
Location: China
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Re: What's the meaning of the "return" here?

  #5  
Jun 5th, 2007

So here's a simplified version of the function.
  1. bool BinarySearchTree::contains(const int & x, BinaryNode *t)
  2. {
  3. if (t == NULL)
  4. {
  5. return false;
  6. }
  7.  
  8. // if this element is before the one we're looking for,
  9. // call the function again with the next value (left)
  10. if (x < t->element)
  11. {
  12. return contains(x,t->left);
  13. }
  14. // This element is not before the one we're looking for...
  15.  
  16. // if this element is after the one we're looking for,
  17. // call the function again with the previous value (right)
  18. if (t->element < x )
  19. {
  20. return contains(x,t->right);
  21. }
  22. // This element is not before nor after. Therefore it IS
  23. // the one we're looking for. We found it so return TRUE
  24. return true;
  25. }

[/quote]

Yes,thank you.It's beautiful.Your code is most the same as Mr Weiss's. Ha,I am trying to implement all of his book,but I can't write them without looking his code.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: What's the meaning of the "return" here?

  #6  
Jun 5th, 2007
I'm most certainly not a fan of Weiss' code, but the book you have is half decent in terms of coverage. His stuff is noticeably dumbed down, but if you want to take your trees to the next level, try this.
I'm here to prove you wrong.
Reply With Quote  
Join Date: May 2007
Location: China
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Re: What's the meaning of the "return" here?

  #7  
Jun 6th, 2007
Originally Posted by Narue View Post
I'm most certainly not a fan of Weiss' code, but the book you have is half decent in terms of coverage. His stuff is noticeably dumbed down, but if you want to take your trees to the next level, try this.


I will try to do it,thank you very much!
Reply With Quote  
Join Date: Jun 2007
Location: yet to confirm
Posts: 5
Reputation: ss339 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
ss339's Avatar
ss339 ss339 is offline Offline
Newbie Poster

Re: What's the meaning of the "return" here?

  #8  
Jun 7th, 2007
Originally Posted by Ancient Dragon View Post
Probably because English has become the universal language of business. Anyone who needs to conduct business in todays global world needs to know English.


Thats the problem man....

Many people that dont have english as first language, it is much harder...

I know this first hand..
peace.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: What's the meaning of the "return" here?

  #9  
Jun 8th, 2007
Originally Posted by meiyantao View Post
! As you know,it's impossible to study C++ well without English.

If you have good C++ books written in or translated to a language you understand, English is not a necessity. Ask any Japanese C++ programmer. You will find it hard communicating in an English IT forum like this, but you can learn C++ without English.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC