1.前序遍历

前序遍历:先遍历根节点,再遍历左子树,最后遍历右子树(根-左-右)

二叉树的四种遍历方法(C++)

测试代码:

// 二叉树的三种遍历
// 1.前序遍历
// 2.中序遍历
// 3.后序遍历

#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

void visitAlongLeftBranch(BinaryTreeNode* , vector<int>& , stack<BinaryTreeNode*>&);

void TraverseBinaryTree(BinaryTreeNode* pRoot, vector<int>& treeNodes)  //二叉树前序遍历算法(迭代版)
{
    stack<BinaryTreeNode*> S; //辅助栈

    while (true)
    {
        visitAlongLeftBranch(pRoot, treeNodes, S);  //从当前节点逐批访问
        if (S.empty()) break; //栈为空, 弹出
        pRoot = S.top();  //弹出下一批的起点
        S.pop();
    }
}

//从当前节点出发, 沿左分支不断深入, 直至没有左分支的节点; 沿途节点遇到后立即访问
void visitAlongLeftBranch(BinaryTreeNode* pRoot, vector<int>& treeNodes, stack<BinaryTreeNode*>& S)
{
    while (pRoot)
    {
        treeNodes.push_back(pRoot->m_nValue); //访问当前节点
        S.push(pRoot->m_pRight); //右子节点入栈暂存
        pRoot = pRoot->m_pLeft; //沿左分支深入一层
    }
}

// ====================测试代码====================
void Test(const char* testName, BinaryTreeNode* pRoot, 
    vector<int> expectedPre, vector<int> expectedIn, vector<int> expectedPost, int expectedLength)
{
    if (testName != nullptr)
        printf("%s begins: ", testName);

    vector<int> treeNodes;  //保存遍历值
    TraverseBinaryTree(pRoot, treeNodes);
    int length = treeNodes.size();

    bool flag = true;  //标志位
    if (length != expectedLength) flag = false;  //长度不一致, 则遍历算法错误

    for (int i = 0; i < length; ++i)  //顺序不一致, 则遍历算法错误
    {
        if (treeNodes[i] != expectedPre[i])  //注意:遍历期望值在这里改
            flag = false;
    }
    
    if (flag)
        printf("Passed.");
    else
        printf("Failed.");

    //打印遍历值
    printf("\nExpected: ");
    for (int i = 0; i < expectedLength; ++i)
        printf("%d ", expectedPre[i]);  //注意:遍历期望值在这里改

    printf("\nActual:   ");
    for (int i = 0; i < length; ++i)
        printf("%d ", treeNodes[i]);
    printf("\n\n");
}

// 1.满二叉树
//            10
//          /    \
//         6      14
//        /\      /\
//       4  8   12  16
void Test1()
{
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
    BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);

    ConnectTreeNodes(pNode10, pNode6, pNode14);
    ConnectTreeNodes(pNode6, pNode4, pNode8);
    ConnectTreeNodes(pNode14, pNode12, pNode16);

    vector<int> expectedPre = {10, 6, 4, 8, 14, 12, 16};
    vector<int> expectedIn = {4, 6, 8, 10, 12, 14, 16};
    vector<int> expectedPost = {4, 8, 6, 12, 16, 14, 10};

    Test("Test1", pNode10, expectedPre, expectedIn, expectedPost, 7);

    DestroyTree(pNode10);
}

// 2.完全二叉树
//            10
//          /    \
//         6      14
//        /\      /
//       4  8   12
void Test2()
{
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);

    ConnectTreeNodes(pNode10, pNode6, pNode14);
    ConnectTreeNodes(pNode6, pNode4, pNode8);
    ConnectTreeNodes(pNode14, pNode12, nullptr);

    vector<int> expectedPre = { 10, 6, 4, 8, 14, 12};
    vector<int> expectedIn = { 4, 6, 8, 10, 12, 14};
    vector<int> expectedPost = { 4, 8, 6, 12, 14, 10 };

    Test("Test2", pNode10, expectedPre, expectedIn, expectedPost, 6);

    DestroyTree(pNode10);
}

// 3.二叉树
//            10
//          /    \
//         6      14
//         \      /
//          8   12  
//         /
//        4
void Test3()
{
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);

    ConnectTreeNodes(pNode10, pNode6, pNode14);
    ConnectTreeNodes(pNode6, nullptr , pNode8);
    ConnectTreeNodes(pNode8, pNode4, nullptr);
    ConnectTreeNodes(pNode14, pNode12, nullptr);

    vector<int> expectedPre = { 10, 6, 8, 4, 14, 12 };
    vector<int> expectedIn = { 6, 4, 8, 10, 12, 14 };
    vector<int> expectedPost = { 4, 8, 6, 12, 14, 10 };

    Test("Test3", pNode10, expectedPre, expectedIn, expectedPost, 6);

    DestroyTree(pNode10);
}

// 4.只有最左侧通路
//               5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
void Test4()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

    ConnectTreeNodes(pNode5, pNode4, nullptr);
    ConnectTreeNodes(pNode4, pNode3, nullptr);
    ConnectTreeNodes(pNode3, pNode2, nullptr);
    ConnectTreeNodes(pNode2, pNode1, nullptr);

    vector<int> expectedPre = {5, 4, 3, 2, 1};
    vector<int> expectedIn = {1, 2, 3, 4, 5};
    vector<int> expectedPost = {1, 2, 3, 4, 5};

    Test("Test4", pNode5, expectedPre, expectedIn, expectedPost, 5);

    DestroyTree(pNode5);
}

// 4.只有最右侧通路
// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void Test5()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNode1, nullptr, pNode2);
    ConnectTreeNodes(pNode2, nullptr, pNode3);
    ConnectTreeNodes(pNode3, nullptr, pNode4);
    ConnectTreeNodes(pNode4, nullptr, pNode5);

    vector<int> expectedPre = {1, 2, 3, 4, 5};
    vector<int> expectedIn = { 1, 2, 3, 4, 5 };
    vector<int> expectedPost = {5, 4, 3, 2, 1};

    Test("Test5", pNode1, expectedPre, expectedIn, expectedPost, 5);

    DestroyTree(pNode1);
}

// 树中只有1个结点
void Test6()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

    vector<int> expectedPre = {1};
    vector<int> expectedIn = {1};
    vector<int> expectedPost = {1};

    Test("Test6", pNode1, expectedPre, expectedIn, expectedPost, 1);

    DestroyTree(pNode1);
}

// 树中没有结点
void Test7()
{
    vector<int> expectedPre = {};
    vector<int> expectedIn = {};
    vector<int> expectedPost = {};

    Test("Test7", nullptr, expectedPre, expectedIn, expectedPost, 0);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();

    return 0;
}
前序遍历测试代码

相关文章:

  • 2021-08-22
  • 2021-12-10
  • 2021-08-09
  • 2021-06-09
  • 2021-06-13
  • 2022-12-23
  • 2021-12-10
猜你喜欢
  • 2021-11-06
  • 2021-12-10
  • 2021-12-10
  • 2021-12-10
相关资源
相似解决方案