树的递归与非递归遍历(C++读文件)

该博客介绍了如何从‘tree.in’文件中读取数据构造树,并针对遇到的‘0’处理为空树的情况。接着详细讲解了如何对构建的树进行递归和非递归的四种遍历方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从“tree.in”文件里读取字符串,构造树,若遇到’0’,则认为是空树,然后对该树进行四种遍历

#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;

#define ElemType char

typedef struct treeNode {
    struct treeNode * lchild;
    struct treeNode * rchild;
    ElemType data;
}treeNode,* tree;

void createTree(tree & t, string str) {
    static int N = 0;
    if (str.length() <= N)
        return;
    ElemType ch = str[N++];
    if (ch == '0')
        t = NULL;
    else {
        t = (treeNode*)malloc(sizeof(treeNode));
        t->data = ch;
        createTree(t->lchild, str);
        createTree(t->rchild, str);
    }
}

//递归前中后序
void preOrder(tree & t) {
    if (t) {
        std::cout << t->data<<" ";
        preOrder(t->lchild);
        preOrder(t->rchild);
    }
}
void inOrder(tree & t) {
    if (t) {
        inOrder(t->lchild);
        std::cout << t->data<<" ";
        inOrder(t->rchild);
    }
}
void postOrder(tree & t) {
    if (t) {
        postOrder(t->lchild);
        postOrder(t->rchild);
        std::cout << t->data<<" ";
    }
}
void levelOrder(tree & t) {
    queue<tree> q;
    if (t) {
        q.push(t);
        while (!q.empty()) {
            tree temp = q.front();
            cout << temp->data << " ";
            q.pop();
            if (temp->lchild)
                q.push(temp->lchild);
            if (temp->rchild)
                q.push(temp->rchild);
        }
    }
}

//非递归前中后序
void preOrder2(tree &t) {
    stack<treeNode *> s;
    while (!s.empty() || t) {
        if (t) {
            cout << t->data;
            s.push(t);
            t = t->lchild;
        }
        else {
            t=s.top();
            s.pop();
            t = t->rchild;
        }
    }
}
void inOrder2(tree &t) {
    stack<treeNode *> s;
    while (!s.empty ()|| t) {
        if (t) {
            s.push(t);
            t = t->lchild;
        }
        else {
            t = s.top();
            s.pop();
            cout << t->data;
            t = t->rchild;
        }
    }
}
void postOrder2(tree & t) {
    stack<treeNode*> s;
    treeNode * r = NULL;           //辅助指针,指向其最近访问过的结点

    while (!s.empty ()|| t) {
        if (t) {
            s.push(t);
            t = t->lchild;
        }
        else {
            t = s.top();
            if (t->rchild&&t->rchild != r) {
                t = t->rchild;
                s.push(t);
                t = t->lchild;
            }
            else {
                t = s.top();
                s.pop();
                cout << t->data;
                r = t;
                //当出栈的节点没有右孩子或右孩子刚刚被访问过时,访问过它后,再出栈一个元素
                t = NULL;  
            }
        }
    }
}
int main() {
    ifstream ifs;
    ifs.open("tree.in");
    string str;
    ifs >> str;
    cout << str << endl;
    tree t;
    createTree(t, str);
    preOrder(t);
    cout << endl;
    createTree(t, str);
    inOrder(t);
    cout << endl;
    createTree(t, str);
    postOrder(t);
    cout << endl;
    createTree(t, str);
    levelOrder(t);
    cout << endl;
    ifs.close();
    system("PAUSE");
    return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值