二进制搜索Tree-breadthFirst函数调用

时间:2010-10-22 05:00:47

标签: c++ binary-tree tree-traversal

我有void leveltraversal(ostream& out);的算法 但我不知道如何在main()中调用它。在我的作业中,我们不允许更改头文件。有没有办法在没有超载的情况下调用它?

更新

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();
 }
}

3 个答案:

答案 0 :(得分:1)

参数ostream&采用任何output stream,例如输出文件。以下示例将标准输出用作ostream

BST myBst;
// insert elements into myBst
myBst.leveltraversal( std::cout );

答案 1 :(得分:0)

如果您无法更改函数标题,则可以定义全局变量并在两个函数(mainleveltraversal)中引用它们。

答案 2 :(得分:0)

这就是我所拥有的

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();
 }
}
相关问题