Skip to content

Instantly share code, notes, and snippets.

@weidagang
Created June 22, 2013 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weidagang/5841305 to your computer and use it in GitHub Desktop.
Save weidagang/5841305 to your computer and use it in GitHub Desktop.
Binary tree iterator
#include <stack>
#ifndef BINARY_TREE_ITERATOR
#define BINARY_TREE_ITERATOR
/*
class Node {
public:
Node* left();
Node* right();
...
};
*/
template<typename Node>
class BinaryTreeIterator {
public:
virtual Node* next() = 0;
virtual bool hasNext() = 0;
};
template<typename Node>
class PreOrderIterator : public BinaryTreeIterator<Node> {
public:
PreOrderIterator (Node* node) {
if (NULL == node) {
return;
}
mStack.push(node);
}
virtual Node* next() {
if (mStack.empty()) {
return NULL;
}
Node *top = mStack.top();
mStack.pop();
if (NULL != top->right()) {
mStack.push(top->right());
}
if (NULL != top->left()) {
mStack.push(top->left());
}
return top;
}
virtual bool hasNext() {
return !mStack.empty();
}
private:
std::stack<Node*> mStack;
};
template<typename Node>
class InOrderIterator : public BinaryTreeIterator<Node> {
public:
InOrderIterator(Node *node) {
Node *current = node;
while (NULL != current) {
mStack.push(current);
current = current->left();
}
}
virtual Node* next() {
if (mStack.empty()) {
return NULL;
}
Node *top = mStack.top();
mStack.pop();
if (NULL != top->right()) {
Node *current = top->right();
while (NULL != current) {
mStack.push(current);
current = current->left();
}
}
return top;
}
virtual bool hasNext() {
return !mStack.empty();
}
private:
std::stack<Node*> mStack;
};
template<typename Node>
class PostOrderIterator : public BinaryTreeIterator<Node> {
public:
PostOrderIterator (Node* node) {
Node *current = node;
while (NULL != current) {
mStack.push(current);
current = current->left();
}
}
virtual Node* next() {
if (mStack.empty()) {
return NULL;
}
Node *top = mStack.top();
mStack.pop();
if (!mStack.empty() && top == mStack.top()->left()) {
Node *current = mStack.top()->right();
while (NULL != current) {
mStack.push(current);
current = current->left();
}
}
return top;
}
virtual bool hasNext() {
return !mStack.empty();
}
private:
std::stack<Node*> mStack;
};
#endif
@henrywoo
Copy link

PostOrderIterator is absolutely wrong!

@lu161513
Copy link

yes,your PostOrderIterator is wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment