•
•
•
•
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
![]() |
This codeblock comes from Mr Weiss's book "Data Sturucture and Algorithm Analysis in C++"(Third Edition). I have some questions about it.
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 ^_^
c Syntax (Toggle Plain Text)
/** *This is a member function from the BinarySearchTree class. *And It try to find whether there's a element equal to x in *the binary search tree. */ bool BinarySearchTree::contains(const int & x, BinaryNode *t){ if(t==NULL) return false; /** *Weiss put the following two sentences to the end of this *codeblock with an "else".But I think put it here can make *the code more readable.Is it an error? */ if(x == t->element) return true; /** *When I implemented these, I haven't added *"return" before recursive functions. *Why must add "return" in the following sentences? *How many times will the "return" really *return when the promgram run? *What will be happened,if I delete these "return"? */ if(x < t->element) return contains(x,t->left); else if(t->element < x ) return contains(x,t->right); }
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.
•
•
•
•
This codeblock comes from Mr Weiss's book "Data Sturucture and Algorithm Analysis in C++"(Third Edition). I have some questions about it.
c Syntax (Toggle Plain Text)
/** * This is a member function from the BinarySearchTree class. * And It try to find whether there's a element equal to x in * the binary search tree. */ bool BinarySearchTree::contains(const int & x, BinaryNode *t) { if (t == NULL) { return false; } /** * Weiss put the following two sentences to the end of this * codeblock with an "else".But I think put it here can make * the code more readable.Is it an error? */ if (x == t->element) { return true; } //// The way the function is written, it doesn't seem to matter //// where this IF goes since each recursive call is part of a //// RETURN statement. /** *When I implemented these, I haven't added *"return" before recursive functions. *Why must add "return" in the following sentences? //// Because when the function finally gets to the end of //// the search, the value is passed back via the RETURN //// statements. So the function returns the value, and //// upon returning returns the value again. It just keeps //// passing the value back to the previous recursion *How many times will the "return" really *return when the promgram run? //// Exactly the same number of time the function is called... *What will be happened,if I delete these "return"? //// The function won't work. Each return below actually //// calls the function again with the next node value to //// be tested. */ if (x < t->element) { return contains(x,t->left); } else if (t->element < x ) { return contains(x,t->right); } }
So here's a simplified version of the function.
c Syntax (Toggle Plain Text)
bool BinarySearchTree::contains(const int & x, BinaryNode *t) { if (t == NULL) { return false; } // if this element is before the one we're looking for, // call the function again with the next value (left) if (x < t->element) { return contains(x,t->left); } // This element is not before the one we're looking for... // if this element is after the one we're looking for, // call the function again with the previous value (right) if (t->element < x ) { return contains(x,t->right); } // This element is not before nor after. Therefore it IS // the one we're looking for. We found it so return TRUE return true; }
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
•
•
•
•
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.
•
•
•
•
So here's a simplified version of the function.
c Syntax (Toggle Plain Text)
bool BinarySearchTree::contains(const int & x, BinaryNode *t) { if (t == NULL) { return false; } // if this element is before the one we're looking for, // call the function again with the next value (left) if (x < t->element) { return contains(x,t->left); } // This element is not before the one we're looking for... // if this element is after the one we're looking for, // call the function again with the previous value (right) if (t->element < x ) { return contains(x,t->right); } // This element is not before nor after. Therefore it IS // the one we're looking for. We found it so return TRUE return true; }
[/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.
•
•
•
•
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!
•
•
Join Date: Jun 2007
Location: yet to confirm
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 1
•
•
•
•
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.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
•
•
•
•
! 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.
バルサミコ酢やっぱいらへんで
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Reading Keypresses without a "return" (C)
- document.write to new window and into a 3 column table! Help Please! (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Square root program without sqrt or pwr
- Next Thread: purpose of the _DEBUG flag



Linear Mode