数据结构课设——二叉树的构造

题目

任务:已知二叉树的层序和中序遍历序列,或已知二叉树的先序序列、中序序列,试编写算法建立该二叉树( 用递归或非递归的方法都可以)。
要求:能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立建立二叉树存储结构的的输入函数、输出层序遍历序列的函数、输出先序遍历序列的函数;

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef struct Tree //定义二叉树的数据类型
{
    char data;
    struct Tree *left;
    struct Tree *right;
} Tree, *BiTree;
int f;                                                     //表示模式选择
BiTree creat0(int n, char pre[], char inf[]);              //已知先序序列和中序序列
BiTree creat1(char flo[], char inf[], int infl, int infr); //已知层次序列和中序序列
void inordertraverse(BiTree T);                            //遍历二叉树
void bialy(BiTree &T);                                     //层次遍历二叉树
int main()
{
    cout << "Given the first and middle order sequences, enter 0" << endl;
    cout << "Given a hierarchical sequence and a mid-order sequence, enter 1" << endl;
    cout << "choose : ";
    cin >> f;
    if (f == 0) //已知先序序列和中序序列
    {
        cout << "The number of input nodes: ";
        int n; //表示节点的数量
        cin >> n;
        char pre[N]; //先序序列
        char inf[N]; //中序序列
        cout << "Please enter the first sequence: ";
        cin >> pre;
        cout << "Please enter the middle sequence: ";
        cin >> inf;
        BiTree T = creat0(n, pre, inf);
        bialy(T);
        cout<<endl;
        //inordertraverse(T);
    }
    else if (f == 1) //已知层次序列和中序序列
    {
        cout << "The number of input nodes: ";
        int n; //表示节点的数量
        cin >> n;
        char flo[N]; //层次序列
        char inf[N]; //中序序列
        cout << "Please enter the hierarchical sequence: ";
        cin >> flo;
        cout << "Please enter the middle sequence: ";
        cin >> inf;
        BiTree T = creat1(flo, inf, 0, strlen(flo) - 1);
        bialy(T);
        cout<<endl;
        //inordertraverse(T);
    }
    else
        cout << "input error" << endl;
    return 0;
}
BiTree creat1(char flo[], char inf[], int infl, int infr) //已知层次序列和中序序列
{
    if (*flo == '\0' || infl > infr)
        return NULL;
    BiTree T = new Tree;
    char data = flo[0];
    T->data = data;
    int pos = infl;
    while (inf[pos] != data)
        pos++;
    char *floLeft = (char *)malloc(sizeof(char) * (pos - infl + 1));
    char *floRight = (char *)malloc(sizeof(char) * (infr - pos + 1));
    *floLeft = *floRight = NULL;
    int k = 0, m = 0;
    for (int i = 1; i < strlen(flo); i++)
    {
        int j;
        for (j = infl; j < pos; j++)
        {
            if (flo[i] == inf[j])
            {
                floLeft[k++] = flo[i];
                break;
            }
        }
        if (j == pos)
            floRight[m++] = flo[i];
    }
    T->left = creat1(floLeft, inf, infl, pos - 1);
    T->right = creat1(floRight, inf, pos + 1, infr);
    return T;
}
BiTree creat0(int n, char pre[], char inf[]) //已知先序序列和中序序列
{
    if (n <= 0)
        return NULL;
    BiTree T;
    T = new Tree;
    T->data = pre[0];
    T->right = T->left = NULL;
    int i;
    for (i = 0; i < n; i++)
    {
        if (inf[i] == pre[0])
            break;
    }
    T->left = creat0(i, pre + 1, inf);
    T->right = creat0(n - i - 1, pre + 1 + i, inf + 1 + i);
    return T;
}
void inordertraverse(BiTree T) //遍历二叉树
{
    if (T)
    {
        //cout<<T->data<<" "; //先序
        inordertraverse(T->left);
        //cout<<T->data<<" "; //中序
        inordertraverse(T->right);
        //cout<<T->data<<" "; //后序
    }
}
void bialy(BiTree &T) //层次遍历二叉树
{
    BiTree vis[N];
    int front = 1, rear = 1;
    vis[rear] = T;
    while (front <= rear)
    {
        BiTree p = vis[front++];
        cout << p->data << " ";
        if (p->left != NULL)
            vis[++rear] = p->left;
        if (p->right != NULL)
            vis[++rear] = p->right;
    }
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值