Reputation: 1
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();
}
}
Upvotes: 0
Views: 289
Reputation: 1
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();
}
}
Upvotes: 0
Reputation: 20403
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 );
Upvotes: 1
Reputation: 1194
If you can't change the function header, you can define global variables and reference them in both functions (main
and leveltraversal
).
Upvotes: 0