由遍历序列构造二叉树

  1. 根据前序中序确定二叉树
  2. 根据后序中序确定二叉树
  3. 根据层次中序确定二叉树
    注意,根据前序和后序无法确定一棵二叉树
#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 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 preInCreate(string pre_str, string in_str, tree & t) {

    if (!pre_str.empty()&&!in_str.empty()) {      //数组内还有结点时,申请空间构造结点
        t = (treeNode *)malloc(sizeof(treeNode));
        t->data = pre_str[0];
        int pos = in_str.find(pre_str[0]);
        string lpstr = pre_str.substr(1, pos);
        string rpstr = pre_str.substr(pos + 1, pre_str.length() - 1 - pos);
        string listr = in_str.substr(0, pos);
        string ristr = in_str.substr(pos + 1, in_str.length() - 1 - pos);
        preInCreate(lpstr, listr, t->lchild);
        preInCreate(rpstr, ristr, t->rchild);

    }
    else
        t = NULL;
}

//根据后序中序建立树
void postInCreate(string in_str, string post_str, tree & t) {
    if (!in_str.empty() && !post_str.empty()) {
        t = (treeNode*)malloc(sizeof(treeNode));
        t->data = post_str[post_str.length() - 1];
        int pos = in_str.find(post_str[post_str.length() - 1]);
        string listr = in_str.substr(0, pos);
        string ristr = in_str.substr(pos + 1, in_str.length() - 1 - pos);
        string lpstr = post_str.substr(0, pos);
        string rpstr = post_str.substr(pos, in_str.length() - 1 - pos);
        postInCreate(listr, lpstr, t->lchild);
        postInCreate(ristr, rpstr, t->rchild);
    }
    else
        t = NULL;
}

int main() {
    string pre_str;
    string in_str;
    string post_str;
    tree t;

    //std::cin >> pre_str >> in_str;
    //preInCreate(pre_str, in_str, t);
    postInCreate(in_str, post_str, t);
    preOrder(t);
    cout << endl;

    //preInCreate(pre_str, in_str, t);
    postInCreate(in_str, post_str, t);
    inOrder(t);
    cout << endl;

    //preInCreate(pre_str, in_str, t);
    postInCreate(in_str, post_str, t);
    postOrder(t);
    cout << endl;

    //preInCreate(pre_str, in_str, t);
    postInCreate(in_str, post_str, t);
    levelOrder(t);
    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值