C++实现二叉树的递归遍历与非递归遍历(先序、中序、后序、层序)

来源:互联网 发布:自行车风火轮编辑软件 编辑:程序博客网 时间:2024/04/29 16:56

递归实现很简单,非递归实现需要借用栈模拟递归实现,搞清楚过程后代码实现不难,直接上代码。

#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;}


0 0
原创粉丝点击