二叉树后序遍历迭代器(仅支持++)

数据结构作业,昨天康大佬()优化了一下觉得不错,贴上来,供测试使用

#include <iostream>
#include <stack>
using namespace std;

template <class Type> struct BTNode {
    BTNode *left;
    Type data;
    BTNode *right;
    BTNode(Type x) {
        data = x;
        left = right = NULL;
    }
};

template <class Type> class PostOrder;

template <class Type> class BinaryTree {
private:
    BTNode<Type> *root;
    friend class PostOrder<Type>;
public:
    BinaryTree(BTNode<Type>*t) { root = t; }
};

//Base class for BT Iterator
template <class Type> class TreeIterator {
protected:
    const BinaryTree <Type> & T; //BT
    const BTNode<Type> *current;
public:
    TreeIterator(const BinaryTree <Type> & BT)
        : T(BT), current(NULL) { }
    virtual ~TreeIterator() { }
    virtual void First() = 0;
    virtual void operator ++ () = 0;
    operator bool() { return current != NULL; }
    const Type operator()()const {
        if (current)
            return current->data;
        return (Type)0;
    }
};

template <class Type> struct StkNode {
    //Stack node definition
    const BTNode <Type> *Node;  //Node Address
    int PopTime;                                        //Counter
    StkNode(const BTNode <Type> *N = NULL) : Node(N), PopTime(0) { }
};

template <class Type> class PostOrder : public TreeIterator <Type> {
    bool renew;
public:
    PostOrder(const BinaryTree <Type> & BT) :TreeIterator(BT) { renew = false; }
    ~PostOrder() { }
    void First();
    //Seek to the first node in postorder traversal
    void operator ++ ();
    //Seek to the successor
protected:
    stack<StkNode<Type>> st;     //Active record stack
};

template <class Type>
void PostOrder<Type>::First() {
    current = T.root;
    while (!st.empty()) {
        st.pop();
    }
    renew = true;
    operator++();
}

template <class Type>
void PostOrder<Type>::operator ++() {
    if (!renew && current == T.root && st.empty()) {
        current = NULL;
        return;
    }
    renew = false;
    const BTNode<Type> *p = current;
    StkNode<Type> w;
    do {
        // Here, you must add necessary statements to set pointer current to the right posotion
        if (st.empty() || (st.top().Node->left != p && st.top().Node->right != current))
            while (p) {
                st.push(StkNode<Type>(p));
                p = p->left;
            }
        w = st.top();
        st.pop();
        if (++w.PopTime == 2) {
            current = w.Node;
            return;
        }
        st.push(w);
        if (w.Node->right) {
            p = w.Node->right;
        }
    } while (true);
}

int main() {
    BTNode<int> *p = new BTNode<int>(6);
    p->left = new BTNode<int>(4);
    p->right = new BTNode<int>(10);
    p->left->left = new BTNode<int>(2);
    p->right->left = new BTNode<int>(8);
    p->right->right = new BTNode<int>(12);
    BinaryTree<int> T(p);
    PostOrder<int> it(T);
    for (it.First(); it; ++it) {
        std::cout << it() << std::endl;
    }

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值