C++实现二叉树的递归遍历与非递归遍历(先序、中序、后序、层序)
2014-06-10 10:45
901 查看
递归实现很简单,非递归实现需要借用栈模拟递归实现,搞清楚过程后代码实现不难,直接上代码。
#include <iostream> #include <stack> #include <queue> using namespace std; template<typename T> struct BiNode { T value; BiNode *pLeft; BiNode *pRight; BiNode() : pLeft(NULL), pRight(NULL) { } }; template<typename T> struct BiNodeAux { BiNode<T> *pNode; bool hasVisited; BiNodeAux(BiNode<T> *_node, bool _vis) : pNode(_node), hasVisited(_vis) { } }; template<typename T> void PreOrderRecursively(BiNode<T> *pRoot) { if (pRoot == NULL) return; cout << pRoot->value << "\t"; PreOrder(pRoot->pLeft); PreOrder(pRoot->pRight); } template<typename T> void PreOrder(BiNode<T> *pRoot) { if (pRoot == NULL) return; stack<BiNodeAux<T> > s; BiNode<T> *pCur = pRoot; while (pCur != NULL || !s.empty()) { if (pCur != NULL) { cout << pCur->value << "\t"; s.push(BiNodeAux<T>(pCur, false)); pCur = pCur->pLeft; } else { if (s.top().hasVisited == true) s.pop(); else { s.top().hasVisited = true; pCur = s.top().pNode->pRight; } } } } template<typename T> void InOrderRecursively(BiNode<T> *pRoot) { if (pRoot != NULL) { InOrderRecursively(pRoot->pLeft); cout << pRoot->value << "\t"; InOrderRecursively(pRoot->pRight); } } template<typename T> void InOrder(BiNode<T> *pRoot) { if (pRoot == NULL) return; stack<BiNode<T>*> s; BiNode<T> *pCur = pRoot; while (pCur != NULL || !s.empty()) { if (pCur != NULL) { s.push(pCur); pCur = pCur->pLeft; } else { cout << s.top()->value << "\t"; pCur = s.top()->pRight; s.pop(); } } } template<typename T> void PostOrderRecursively(BiNode<T> *pRoot) { if (pRoot != NULL) { PostOrderRecursively(pRoot->pLeft); PostOrderRecursively(pRoot->pRight); cout << pRoot->value << "\t"; } } template<typename T> void PostOrder(BiNode<T> *pRoot) { if (pRoot == NULL) return; stack<BiNodeAux<T> > s; BiNode<T> *pCur = pRoot; while (pCur != NULL || !s.empty()) { if (pCur != NULL) { s.push(BiNodeAux<T>(pCur, false)); pCur = pCur->pLeft; } else { if (s.top().hasVisited == false) { s.top().hasVisited = true; pCur = s.top().pNode->pRight; } else { cout << s.top().pNode->value << "\t"; s.pop(); } } } } template<typename T> void LevelOrder(BiNode<T> *pRoot) { if (pRoot == NULL) return; queue<BiNode<T>*> q; BiNode<T> *pCur = pRoot; q.push(pCur); while (!q.empty()) { pCur = q.front(); cout << pCur->value << "\t"; q.pop(); if (pCur->pLeft != NULL) q.push(pCur->pLeft); if (pCur->pRight != NULL) q.push(pCur->pRight); } } int main() { BiNode<int> nodes[7]; for (int i = 0; i < 7; ++i) nodes[i].value = i + 1; nodes[0].pLeft = &nodes[1]; nodes[0].pRight = &nodes[2]; nodes[1].pLeft = &nodes[3]; nodes[1].pRight = &nodes[4]; nodes[2].pLeft = &nodes[5]; nodes[2].pRight = &nodes[6]; PreOrderRecursively(&nodes[0]); cout << endl; PreOrder(&nodes[0]); cout << endl << endl; InOrderRecursively(&nodes[0]); cout << endl; InOrder(&nodes[0]); cout << endl << endl; PostOrderRecursively(&nodes[0]); cout << endl; PostOrder(&nodes[0]); cout << endl << endl; LevelOrder(&nodes[0]); cout << endl << endl; return 0; }
相关文章推荐
- Java实现二叉树的前序、中序、后序、层序遍历(递归方法)
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的创建,先序、中序、后序遍历的递归实现以及层序遍历
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- 二叉树先序,中序,后序、层序遍历递归和非递归实现
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- C语言非递归实现二叉树的先序、中序、后序、层序遍历
- 二叉树基本操作的递归实现(二叉树建立,先序,中序,后序,深度的递归遍历。广度优先,高度优先的非递归遍历)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树(前序,中序,后序,层序)遍历递归与循环的python实现
- 探讨:C++实现链式二叉树(用非递归方式先序,中序,后序遍历二叉树)
- 二叉树的遍历:前序、中序、后序、层序的非递归实现
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- Java实现二叉树的前序、中序、后序、层序遍历(递归方法)