二叉树后序遍历迭代器(仅支持++)
来源:互联网 发布:恒扬数据副总经理 编辑:程序博客网 时间:2024/06/10 00:46
数据结构作业,昨天康大佬()优化了一下觉得不错,贴上来,供测试使用
#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 Iteratortemplate <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 successorprotected: 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
- 二叉树后序遍历迭代器(仅支持++)
- iOS语音合成(仅支持英文)
- java创建二叉树(仅算法)
- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历(递归)
- 二叉树后序遍历
- 数据结构 -- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历
- 二叉树后序遍历算法
- 二叉树后序遍历实例
- 二叉树后序遍历
- 二叉树后序遍历
- binTreePosterorderTraversal二叉树后序遍历
- 冒泡
- 6.使用github
- 鲜为人知的 Linux 命令(4)
- 著名开源网络取证工具Xplico远程未授权RCE漏洞
- LeetCode-Easy刷题(7) Remove Duplicates from Sorted Array
- 二叉树后序遍历迭代器(仅支持++)
- 边坡计算与模型
- 安装pcre-8.39和Lighttpd 在进行make的时候出现的问题
- java国际化
- centOS6.5 Nginx无法解析php文件
- seo是否是前途未卜?
- 对PostgreSQL数据库结构的宏观理解
- LeetCode-Easy刷题(8) Remove Element
- CentOS下安装OpenOffice4.1.3