views:

42

answers:

3

I have the algorithm for void leveltraversal(ostream& out); but i am not sure how to call it in main () . In my Assignment we are not allowed to change the header file. Is there a way to call it without overloading it?

Update:

void BST::levelTraversal(ostream& out){
 queue<BST::BinNode*> q;
 BinNode* cur = myRoot;
 BinNode* top = NULL;
 q.push(cur);
 while(q.empty() != false){
  top = q.front();
  if(top->left != NULL){
   q.push(top->left);
  }
  if(top->right !=NULL){
   q.push(top->right);
  }
  out<<top->data;
  q.pop();
 }
}
+1  A: 

The parameter, ostream&, takes any output stream, e.g. output file. The following example uses the standard output as an ostream:

BST myBst;
// insert elements into myBst
myBst.leveltraversal( std::cout );
ArunSaha
@Steven: I updated the answer and provided a link for `ostream`.
ArunSaha
A: 

If you can't change the function header, you can define global variables and reference them in both functions (main and leveltraversal).

Khaled
I dont understand how to print it out,
Steven
-1, since you should never use any form of global state as an hidden function input parameter. This kind of parameter is *very* likely to create pain in future code changes.
Rudi
A: 

this is what i have

void BST::levelTraversal(ostream& out){
 queue<BST::BinNode*> q;
 BinNode* cur = myRoot;
 BinNode* top = NULL;
 q.push(cur);
 while(q.empty() != false){
  top = q.front();
  if(top->left != NULL){
   q.push(top->left);
  }
  if(top->right !=NULL){
   q.push(top->right);
  }
  out<<top->data;
  q.pop();
 }
}
Steven
This is not an answer -- it might be more suitable to "edit" your question, and add this stuff over there. Then, you may delete this answer including this comment.
ArunSaha
how do i format the code correctly in the comment?
Steven
Okay - I edited the question for you and added the method definition. You can delete this answer now.
ArunSaha
doesnt give me an option to delete
Steven
Hmm, Okay, Never mind. Lets get back to business. Look at the answers and see if they help you; if not, mention why not.
ArunSaha