树的遍历(C/C++)

/* test data1 */
/* 输入 */
8        // 节点数
A 1 2    //节点数据、左孩子、右孩子
B 3 4
C 5 #
D # #
E 6 #
G 7 #
F # #
H # #
/* 输出 */
A B D E F C G H    // 前序
D B F E A H G C    // 中序
D F E B H G C A    // 后序
A B C D E G F H    // 层序

/* test data2 */
/* 输入 */
8 
G # 4 
B 7 6
F # #
A 5 1
H # #
C 0 #
D # #
E 2 #
/* 输出 */
A C G H B E F D
G H C A F E B D
H G C F E D B A
A C B G E D H F
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <cctype>
/**
 * 结构数组表示二叉树
 * 前序遍历
 * 中序遍历
 * 后序遍历
 * 层序遍历
 * 递归打印控制最后一个空格不打印
 **/

struct TNode
{
    char data;
    signed char left, right;
};

int buildTree(std::vector<TNode> &tree, int);
void preOrder(const std::vector<TNode> &, const int, bool&);
void inOrder(const std::vector<TNode> &, const int, bool&);
void postOrder(const std::vector<TNode> &, const int, bool&);
void levelOrder(const std::vector<TNode> &, int, bool &);
int main()
{
    int nodeNum;
    std::cin >> nodeNum;
    std::vector<TNode> tree(nodeNum);
    int root{buildTree(tree, nodeNum)};

    bool firstprint = true;
    preOrder(tree, root, firstprint);
    std::cout << "\n";
    firstprint = true;
    inOrder(tree, root, firstprint);
    std::cout << "\n";
    firstprint = true;
    postOrder(tree, root, firstprint);
    std::cout << "\n";
    firstprint = true;
    levelOrder(tree, root, firstprint);
        
    return 0;
}
int buildTree(std::vector<TNode> &tree, int nodeNum)
{
    std::vector<bool> check(nodeNum,false);
    for (unsigned int i = 0; i != nodeNum; ++i)
    {
        std::cin >> tree[i].data >> tree[i].left >> tree[i].right;
        if (std::isdigit(tree[i].left))
        {
            tree[i].left -= '0';
            check[tree[i].left] = true;
        }
        else
            tree[i].left = -1;
        if (std::isdigit(tree[i].right))
        {
            tree[i].right -= '0';
            check[tree[i].right] = true;
        }
        else
            tree[i].right = -1;
    }
    return std::find(check.begin(), check.end(), false)-check.begin();
}
void preOrder(const std::vector<TNode> &tree, const int root, bool &firstprint)
{
    if (root == -1)
        return;
    firstprint ? std::cout << tree[root].data : std::cout << " " << tree[root].data;
    firstprint = false;
    preOrder(tree, tree[root].left, firstprint);
    preOrder(tree, tree[root].right, firstprint);
}
void inOrder(const std::vector<TNode> &tree, const int root, bool &firstprint)
{
    if (root == -1)
        return;
    inOrder(tree, tree[root].left, firstprint);
    firstprint ? std::cout << tree[root].data : std::cout << " " << tree[root].data;
    firstprint = false;
    inOrder(tree, tree[root].right, firstprint);
}
void postOrder(const std::vector<TNode> &tree, const int root, bool &firstprint)
{
    if (root == -1)
        return;
    postOrder(tree, tree[root].left, firstprint);
    postOrder(tree, tree[root].right, firstprint);
    firstprint ? std::cout << tree[root].data : std::cout << " " << tree[root].data;
    firstprint = false;
}
void levelOrder(const std::vector<TNode> &tree, int root, bool &firstprint)
{
    std::deque<int> Queue{root};
    while (!Queue.empty())
    {
        root = Queue.front();
        Queue.pop_front(); 
        firstprint ? std::cout << tree[root].data : std::cout << " " << tree[root].data;
        firstprint = false;
        if (tree[root].left != -1)
            Queue.push_back(tree[root].left);
        if (tree[root].right != -1)
            Queue.push_back(tree[root].right);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值