C++ 使用函数指针的递归遍历
template <class T>
void BT<T>::inOrder(void (*inOrderPtr)(T&))
{
inOrderPtr(inOrder(this->root));
}
template <class T>
void BT<T>::inOrder(Node<T>* root) const
{
if (root->left != NULL)
inOrder(root->left);
//something here
if (root->right != NULL)
inOrder(root->right);
}
好吧,我正在尝试通过递归创建此遍历。 我实际上之前发布过这个问题,但由于我必须使用函数指针,所以我的处理方式是错误的。 我不明白我应该做什么。 我有一个调用私有包装器的公共包装器...但是公共包装器是传递函数的包装器,所以我该怎么办? 我觉得自己很迟钝,所以即使有人给我一个小小的提示,我也确信我会明白。 我只是不知道从这里该去哪里。
调用它的代码示例如下:
first.inOrder(print_val)
template <class T>
void BT<T>::inOrder(void (*inOrderPtr)(T&))
{
inOrderPtr(inOrder(this->root));
}
template <class T>
void BT<T>::inOrder(Node<T>* root) const
{
if (root->left != NULL)
inOrder(root->left);
//something here
if (root->right != NULL)
inOrder(root->right);
}
Ok I am trying to create this Traversal via recursion. I actually posted this problem before but I was going about it wrong due to me having to use a function pointer. I don't understand what I'm suppose to do. I've got the public wrapper that calls on the private one... but the public one is the one with the function being passed in so what do I even do with it?? I feel retarded so even if someone were to give me a small hint I'm sure I'd get it. I just don't know where to go from here.
an example of a code that calls on it is this:
first.inOrder(print_val)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的方法,但是 Node::GetItem 需要实现才能 100% 正确:
This is how to do it properly, but Node::GetItem needs implementing in order for this to be 100% correct: