一.前序遍历
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
struct BinaryTreeNode {
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//递归实现
void PreOrder1(BinaryTreeNode* pRoot) {
if (pRoot != nullptr) {
cout << pRoot->m_nValue << " ";
if (pRoot->m_pLeft != nullptr) {
PrintTree(pRoot->m_pLeft);
}
if (pRoot->m_pRight != nullptr) {
PrintTree(pRoot->m_pRight);
}
}
}
//非递归实现
void PreOrder2(BinaryTreeNode* pRoot) {
BinaryTreeNode* p = pRoot;
stack<BinaryTreeNode*>s;
s.push(p);
//先压栈右子树再压栈左子树(这样访问的时候才是先访问左再访问右)
while (!s.empty()) {
p = s.top();
s.pop();
cout << p->m_nValue << " ";
if (p->m_pRight != nullptr) {
s.push(p->m_pRight);
}
if (p->m_pLeft != nullptr) {
s.push(p->m_pLeft);
}
}
}
二.中序遍历
//递归实现
void InOrder1(BinaryTreeNode* pRoot) {
if (pRoot != nullptr) {
if (pRoot->m_pLeft != nullptr) {
PrintTree(pRoot->m_pLeft);
}
cout << pRoot->m_nValue << " ";
if (pRoot->m_pRight != nullptr) {
PrintTree(pRoot->m_pRight);
}
}
}
//非递归实现
void InOrder2(BinaryTreeNode* pRoot) {
BinaryTreeNode* p = pRoot;
stack<BinaryTreeNode*>s;
while (p != nullptr || !s.empty()) {
if (p) {//1.向左压栈到最左边
s.push(p);
p = p->m_pLeft;
}
else {//2.回退出栈访问+向右压栈
p = s.top();
s.pop();
cout << p->m_nValue << " ";
p = p->m_pRight;
}
}
}
三.后序遍历
//递归实现
void PostOrder1(BinaryTreeNode* pRoot) {
if (pRoot != nullptr) {
if (pRoot->m_pLeft != nullptr) {
PrintTree(pRoot->m_pLeft);
}
if (pRoot->m_pRight != nullptr) {
PrintTree(pRoot->m_pRight);
}
cout << pRoot->m_nValue << " ";
}
}
//非递归实现
void PostOrder2(BinaryTreeNode* pRoot) {
BinaryTreeNode* p = pRoot;
BinaryTreeNode* r = nullptr;//辅助指针
stack<BinaryTreeNode*>s;
while (p != nullptr || !s.empty()) {
if (p) {//1.向左压栈到最左
s.push(p);
p = p->m_pLeft;
}
else {//2.回退分两种情况
p = s.top();
//第一种情况:右子树存在且未被访问=>继续压栈
if (p->m_pRight != nullptr && p->m_pRight != r) {
p = p->m_pRight;
s.push(p);
//※p已被压栈 故需向左 否则重复压栈
p = p->m_pLeft;
}
//第二种情况:右子树不存在或者右子树访问过=>访问该节点
else {
s.pop();
cout << p->m_nValue << " ";
r = p;
//※该节点左右子树均访问过 故设为NULL 继续出栈
p = nullptr;
}
}
}
}
四.层次遍历
void LevelOrder(BinaryTreeNode* pRoot) {
BinaryTreeNode* p = pRoot;
queue<BinaryTreeNode*>q;
q.push(p);
while (!q.empty()) {
p = q.front();
cout << p->m_nValue << " ";
q.pop();
if (p->m_pLeft != nullptr) {
q.push(p->m_pLeft);
}
if (p->m_pRight != nullptr) {
q.push(p->m_pRight);
}
}
}
测试补充代码
BinaryTreeNode* CreateBinaryTreeNode(double nValue)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = nValue;
pNode->m_pLeft = nullptr;
pNode->m_pRight = nullptr;
return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if (pParent != nullptr)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
void DestroyTree(BinaryTreeNode* pRoot)
{
if (pRoot != nullptr)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = nullptr;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
int main() {
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(11);
ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode3, pNode6, pNode7);
//PostOrder2(pNode1);
//InOrder2(pNode1);
//PreOrder2(pNode1);
LevelOrder(pNode1);
DestroyTree(pNode1);
return 0;
}