您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐