二叉搜索树-宽度优先函数调用

发布于 2024-09-28 06:19:49 字数 503 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-10-05 06:19:49

参数ostream&采用任何输出流,例如输出文件。以下示例使用标准输出作为 ostream

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

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 );
梓梦 2024-10-05 06:19:49

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

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

何其悲哀 2024-10-05 06:19:49

这就是我所拥有的

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

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();
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文