《数据结构》二叉树的遍历(先序、中序、后序、层次)(递归+非递归)

前序遍历(递归版)

void BinTree::p_dfs(treeNode* u, int& inf, int* path) const{
    if(u == NULL) return;
    if(inf == total) return;

    path[inf ++] = u->data;
    p_dfs(u->left, inf, path);
    p_dfs(u->right, inf, path);
}

int* BinTree::p_recursion() const{
    if(root == NULL) return NULL;
    int* path = new int[total];
    int inf = 0;
    p_dfs(root, inf, path);
    for (int i = 0; i < total; i ++) printf("%d ", path[i]);
    puts("");
    return path;
}

前序遍历(非递归版)

int* BinTree::my_pre_traversal() const{
    if(root == NULL) return NULL;
    treeNode* now = root;
    stack<treeNode*> st;
    int* path = new int[total];
    int inf = 0;
    while(true)
    {
        while(now) // 走完左分支,在遍历了当前结点之后,将控制权直接交给左儿子
        {
            path[inf ++] = now->data;
            st.push(now);
            now = now->left;
        }

        if(st.size()) // 检查栈
        {
            treeNode* u = st.top();
            st.pop();
            now = u->right; // 将控制权交给右儿子
        }
        else break; // 如果栈已经空了,直接退出循环
    }
    for (int i = 0; i < total; i ++)
        printf("%d ", path[i]);
    puts("");
    return path;
}

中序遍历(递归版)

void BinTree::m_dfs(treeNode* u, int& inf, int* path) const{
    if(u == NULL) return;
    if(inf == total) return;

    m_dfs(u->left, inf, path);
    path[inf ++] = u->data;
    m_dfs(u->right, inf, path);
}

int* BinTree::m_recursion() const{
    if(root == NULL) return NULL;
    int* path = new int[total];
    int inf = 0;
    m_dfs(root, inf, path);
    for (int i = 0; i < total; i ++) printf("%d ", path[i]);
    puts("");
    return path;
}

中序遍历(非递归版)

int* BinTree::my_mid_traversal() const{
    if(root == NULL) return NULL;
    stack<treeNode*> st;
    treeNode* now = root;
    int* path = new int[total];
    int inf = 0;
    while(true)
    {
        while(now)
        {
            st.push(now);
            now = now->left;
        }

        if(st.size())
        {
            treeNode* u = st.top();
            st.pop();
            path[inf ++] = u->data;
            now = u->right;
        }
        else break;
    }

    for (int i = 0; i < total; i ++)
        printf("%d ", path[i]);
    puts("");
    return path;
}

后序遍历(递归版)

int* BinTree::po_recursion() const{
    if(root == NULL) return NULL;
    int* path = new int[total];
    int inf = 0;
    po_dfs(root, inf, path);
    for (int i = 0; i < total; i ++)
        printf("%d ", path[i]);
    puts("");
    return path;
}

void BinTree::po_dfs(treeNode* u, int& inf, int* path) const{
    if(u == NULL) return;
    if(inf == total) return;

    po_dfs(u->left, inf, path);
    po_dfs(u->right, inf, path);
    path[inf ++] = u->data;
}

后序遍历(非递归版)

int* BinTree::post_traversal() const{
    if(root == NULL) return NULL;
    treeNode* now = root;
    stack<treeNode*> st;
    int* path = new int[total];
    int inf = 0;
    treeNode* last = NULL; // 记录上次遍历结果
    while(true)
    {
        while(now)
        {
            st.push(now);
            now = now->left;
        }
        if(st.size())
        {
            treeNode* u = st.top();
            st.pop();
            if((!u->right) || last == u->right) // 当该节点没有右子树或者已经遍历过了右子树
                path[inf ++] = u->data, last = u;
            else{
                st.push(u); // 如果右子树还没有遍历过,就将这个节点重新再push进栈
                now = u->right;
            }
        }
        else break;
    }
    for (int i = 0; i < total; i ++) printf("%d ", path[i]);
        puts("");
    return path;
}

层次遍历

int* BinTree::floor_traversal() const{
    if(root == NULL) return NULL;
    queue<treeNode*> que;
    int* path = new int[total];
    int inf = 0;
    que.push(root);
    while(que.size())
    {
        treeNode* u = que.front();
        que.pop();
        path[inf ++] = u->data;
        if(u->left) que.push(u->left);
        if(u->right) que.push(u->right);
    }
    for (int i = 0; i < total; i ++) printf("%d ", path[i]);
    puts("");
    return path;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Victayria

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值