[英]Create a C++ preorder iterator to supply tree nodes to a loop
I am trying to write a C++ program to supply tree nodes to this loop:我正在尝试编写一个 C++ 程序来为这个循环提供树节点:
bin_tree<int> *my_tree = ...
for (bin_tree<int>::iterator n = my_tree->begin();
n != my_tree->end(); ++n)
{
cout << *n << "\n";
}
The class I have is written in Java, and I would like to translate it into C++, however I am having trouble doing so.我的课程是用 Java 编写的,我想将它翻译成 C++,但是我在这样做时遇到了麻烦。 This is the class:
这是课程:
class BinTree<T> implements Iterable<T> {
BinTree<T> left;
BinTree<T> right;
T val;
// other methods: insert, delete, lookup, ...
public Iterator<T> iterator()
{
return new TreeIterator(this);
}
private class TreeIterator implements Iterator<T>
{
private Stack<BinTree<T>> s = new Stack<BinTree<T>>();
TreeIterator(BinTree<T> n)
{
if (n.val != null) s.push(n);
}
public boolean hasNext()
{
return !s.empty();
}
public T next()
{
if (!hasNext()) throw new NoSuchElementException();
BinTree<T> n = s.pop();
if (n.right != null) s.push(n.right);
if (n.left != null) s.push(n.left);
return n.val;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
}
Any help on how to write this program correctly in C++ would be appreciated, I am not sure how to properly implement the preorder iterator.任何有关如何在 C++ 中正确编写此程序的帮助将不胜感激,我不确定如何正确实现预序迭代器。
If you are trying to create a container that supports iterators I would suggest the following (you probably know all this but it may help you write good C++):如果您正在尝试创建一个支持迭代器的容器,我会建议以下内容(您可能知道所有这些,但它可能会帮助您编写好的 C++):
Understand iterators (in your case it may make sense to have bidirectional iterators).了解迭代器(在您的情况下,拥有双向迭代器可能是有意义的)。 Iterators are wrappers to pointers in C++.
迭代器是 C++ 中指针的包装器。
Understand the underlying data structure you want (binary tree or binary search tree?).了解你想要的底层数据结构(二叉树还是二叉搜索树?)。
How should an iterator traverse a tree?迭代器应该如何遍历树? (Preorder,inorde,postorder, or level order)
(前序、中序、后序或级序)
My approach would be to write my own node class/struct.我的方法是编写我自己的节点类/结构。 Then write an iterator class that handles the pointer manipulation.
然后编写一个迭代器类来处理指针操作。 If you need to handle allocations a certain way I would write a custom allocator to help too.
如果您需要以某种方式处理分配,我也会编写一个自定义分配器来提供帮助。 Finally write your container that uses the other classes.
最后编写使用其他类的容器。 (Modular and separates out functionality in way that is easy to understand in C++)
(模块化并以在 C++ 中易于理解的方式分离功能)
If you are just looking for the iterator logic and have everything else you need;如果您只是在寻找迭代器逻辑并拥有您需要的一切; begin is the first address and can be wrapped as &data [0] for example.
begin 是第一个地址,例如可以包装为 &data [0] 。 End is the last address in similar manner.
End 是类似方式的最后一个地址。 ++ operator is supported on pointers and will move along linearly towards end.
++ 运算符在指针上受支持,并将沿直线向终点移动。 Still will want to figure out moving the tree should work!
仍然会想弄清楚移动树应该有效! May even provide multiple ways.
甚至可以提供多种方式。
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.