(本博客旨在个人总结回顾)
直接上代码:
// OfferMemo6.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <stack>
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
BinaryTreeNode(int value = -1)
: m_nValue(value)
, m_pLeft(NULL)
, m_pRight(NULL)
{
}
};
//递归遍历
//先序遍历(先访问父节点,再访问左节点,再访问右节点)
void PreorderRecursion(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
cout << pRoot->m_nValue << endl;
PreorderRecursion(pRoot->m_pLeft);
PreorderRecursion(pRoot->m_pRight);
}
}
//中序遍历(先访问左节点,再访问父节点,再访问右节点)
void InorderRecursion(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
InorderRecursion(pRoot->m_pLeft);
cout << pRoot->m_nValue << endl;
InorderRecursion(pRoot->m_pRight);
}
}
//后序遍历(先访问左节点,再访问右节点,再访问父节点)
void PostorderRecursion(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
PostorderRecursion(pRoot->m_pLeft);
PostorderRecursion(pRoot->m_pRight);
cout << pRoot->m_nValue << endl;
}
}
//非递归
//先序遍历(先访问父节点,再访问左节点,再访问右节点)
void PreorderNonRecursion(BinaryTreeNode* pRoot)
{
if (NULL != pRoot)
{
std::stack<BinaryTreeNode*> stackTree;
stackTree.push(pRoot);
BinaryTreeNode* pNode;
while (!stackTree.empty())
{
pNode = stackTree.top();
cout << pNode->m_nValue << endl;
stackTree.pop();
if (NULL != pNode->m_pRight)
{
stackTree.push(pNode->m_pRight);
}
if (NULL != pNode->m_pLeft)
{
stackTree.push(pNode->m_pLeft);
}
}
}
}
//中序遍历(先访问左节点,再访问父节点,再访问右节点)
void InorderNonRecursion(BinaryTreeNode* pRoot)
{
if (NULL != pRoot)
{
std::stack<BinaryTreeNode*> stackTree;
while (!stackTree.empty() || pRoot != NULL)
{
if (pRoot != NULL)
{
stackTree.push(pRoot);
pRoot = pRoot->m_pLeft;
}
else
{
pRoot = stackTree.top();
cout << pRoot->m_nValue << endl;
stackTree.pop();
pRoot = pRoot->m_pRight;
}
}
}
}
//后序遍历(先访问左节点,再访问右节点,再访问父节点)
void PostorderNonRecursion(BinaryTreeNode* pRoot)
{
if (NULL != pRoot)
{
std::stack<BinaryTreeNode*> stackTree;
std::stack<BinaryTreeNode*> stackTmpTree;//辅助栈
stackTmpTree.push(pRoot);
while (!stackTmpTree.empty())
{
pRoot = stackTmpTree.top();
stackTree.push(pRoot);
stackTmpTree.pop();
if (pRoot->m_pLeft != NULL)
{
stackTmpTree.push(pRoot->m_pLeft);
}
if (pRoot->m_pRight != NULL)
{
stackTmpTree.push(pRoot->m_pRight);
}
}
while (!stackTree.empty())
{
pRoot = stackTree.top();
cout << pRoot->m_nValue << endl;
stackTree.pop();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//创建二叉树
/*
0
^
1 4
^ ^
2 3 5 6
*/
BinaryTreeNode* pRoot = new BinaryTreeNode(0);
pRoot->m_pLeft = new BinaryTreeNode(1);
pRoot->m_pRight = new BinaryTreeNode(4);
pRoot->m_pLeft->m_pLeft = new BinaryTreeNode(2);
pRoot->m_pLeft->m_pRight = new BinaryTreeNode(3);
pRoot->m_pRight->m_pLeft = new BinaryTreeNode(5);
pRoot->m_pRight->m_pRight = new BinaryTreeNode(6);
cout << "先序遍历<递归>:" << endl;
PreorderRecursion(pRoot);
cout << "先序遍历<非递归>:" << endl;
PreorderNonRecursion(pRoot);
cout << endl << "中序遍历<递归>:" << endl;
InorderRecursion(pRoot);
cout << "中序遍历<非递归>:" << endl;
InorderNonRecursion(pRoot);
cout << endl <<"后序遍历<递归>:" << endl;
PostorderRecursion(pRoot);
cout << "后序遍历<非递归>:" << endl;
PostorderNonRecursion(pRoot);
system("pause");
return 0;
}