求把这个前序和中序构建二叉树改成中序和后序

问题遇到的现象和发生背景

麻烦讲一下这个前中序构建二叉树的原理并把这个改成中后序构建二叉树及原理,通过测试了采纳

遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
BitNode* Built(char p[], char q[], int& count, int plength, int qlength) {
    if (plength == 0 || qlength == 0) {
        count--;
        return NULL;
    }
    if (count >= plength) {
        count--;
        return NULL;
    }
    BitNode* T = new BitNode(p[count]);
    int n = 0;
    while (q[n] != p[count]) {
        n++;
    }
    char middleleft[100]; int leftlength = 0;
    char middleright[100]; int rightlength = 0;

    for (; leftlength < n; leftlength++)
        middleleft[leftlength] = q[leftlength];

    if (leftlength == 0)
        middleleft[leftlength] = '\0';

    for (int i = n + 1; i < qlength; i++)
        middleright[rightlength++] = q[i];

    if (rightlength == 0)
        middleright[rightlength] = '\0';
    T->leftchild = Built(p, middleleft, ++count, plength, leftlength);
    T->rightchild = Built(p, middleright, ++count, plength, rightlength);
    return T;
}

运行结果及详细报错内容
我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

可以把这个函数中的前序和中序构建二叉树改成中序和后序,主要是把函数的第一个和第二个参数以及部分代码中的变量名称改一下即可。

首先,把函数的第一个参数改成后序序列,第二个参数改成中序序列。函数的签名应该是这样:

BitNode* Built(char p[], char q[], int& count, int plength, int qlength)

改成这样:

BitNode* Built(char p[], char q[], int& count, int plength, int qlength)

然后,把代码中的变量名称改一下,比如把middleleft改成middleleft,把middleright改成middleright等等。

改完后,函数的代码应该是这样的:

BitNode* Built(char p[], char q[], int& count, int plength, int qlength) {
    if (plength == 0 || qlength == 0) {
        count--;
        return NULL;
    }
    if (count >= plength) {
        count--;
        return NULL;
    }
    BitNode* T = new BitNode(p[count]);
    int n = 0;
    while (q[n] != p[count]) {
        n++;
    }
    char middleleft[100]; int leftlength = 0;
    char middleright[100]; int rightlength = 0;
    for (; leftlength < n; leftlength++)
        middleleft[leftlength] = q[leftlength];
    if (leftlength == 0)
        middleleft[leftlength] = '\0';
    for (int i = n + 1; i < qlength; i++)
        middleright[rightlength++] = q[i];
    if (rightlength == 0)
        middleright[rightlength] = '\0';
    T->leftchild = Built(p, middleleft, ++count, plength, leftlength);
    T->rightchild = Built(p, middleright, ++count, plength, rightlength);
    return T;
}


把这个函数改成中序和后序构建二叉树后,可以这样调用这个函数来构建二叉树:

int count = 0;
char p[] = {'d', 'e', 'b', 'f', 'c', 'a'}; // 后序序列
char q[] = {'d', 'b', 'e', 'a', 'f', 'c'}; // 中序序列
int plength = sizeof(p) / sizeof(p[0]);
int qlength = sizeof(q) / sizeof(q[0]);
BitNode* root = Built(p, q, count, plength, qlength);

希望这个修改能帮到您!

在讲如何把前序和中序构建二叉树改成中序和后序构建二叉树之前,我们先来了解一下前序和中序构建二叉树的原理。

二叉树是一种重要的数据结构,在树的遍历算法、二叉搜索树等方面都有重要的应用。在构建二叉树时,可以通过前序遍历和中序遍历来构建一棵二叉树。

具体来说,前序遍历顺序是:根节点、左子树、右子树;中序遍历顺序是:左子树、根节点、右子树。如果给定一棵二叉树的前序遍历和中序遍历序列,就可以唯一地确定这棵二叉树。

具体来说,构建二叉树的过程如下:

从前序遍历序列中取出第一个节点作为根节点。
在中序遍历序列中找到根节点的位置,在根节点左边的序列为左子树,在根节点右边的序列为右子树。
递归构建左子树和右子树。
例如,给定一棵二叉树的前序遍历序列为{'a', 'b', 'd', 'e', 'c', 'f'},中序遍历序列为{'d', 'b', 'e', 'a', 'f', 'c'},那么可以通过上述步骤构建出这棵二叉树:
a
/
b c
/ \
d e f
现在,让我们来看看如何把前序和中序构建二叉树改成中序和后序构建二叉树。

其实,把前序和中序构建二叉树改成中序和后序构建二叉树并不复杂,只需要把前序遍历序列和中序遍历序列换成后序遍历序列和中序遍历序列即可。

在中序和后序构建二叉树的过程中,可以先从后序遍历序列中取出最后一个节点作为根节点。然后,在中序遍历序列中找到根节点的位置,在根节点左边的序列为左子树,在根节点右边的序列为右子树。最后,递归构建左子树和右子树。

例如,给定一棵二叉树的后序遍历序列为{'d', 'e', 'b', 'f', 'c', 'a'},中序遍历序列为{'d', 'b', 'e', 'a', 'f', 'c'},那么可以通过上述步骤构建出这棵二叉树:
a
/
b c
/ \
d e f
希望这些解释能帮到你!


#include <iostream>
#include<cmath>
using namespace std;

class Link {
public:
    char content;
    Link* next;
    Link() { this->next == nullptr; }
    Link(char a) {
        this->content = a;
        this->next = NULL;
    }

};
class Queue {
public:
    Link* first, * last;
};
void initialQueue(Queue& Y) {
    Y.first = Y.last = new Link();
    Y.first->next = NULL;

}
bool Empty(Queue Y) {
    if (Y.first == Y.last)
        return 1;
    else
        return 0;
}
void InQueue(Queue& Y, char& x) {
    Link* y = new Link(x);
    y->next = nullptr;
    Y.last->next = y;
    Y.last = y;
}
bool OutQueue(Queue& Y, char& x) {
    if (Y.first == Y.last)
        return false;

    Link* y = Y.first->next;
    x = y->content;
    Y.first->next = y->next;
    if (Y.last == y)
        Y.last = Y.first;
    delete y;
    return true;
}
void DestroyQueue(Queue& Y) {
    Link* y;
    while (Y.first != Y.last) {
        y = Y.first->next;
        Y.first->next = y->next;
        if (Y.last == y)
            Y.last = Y.first;
        delete y;
    }
    delete Y.first;
}
class BitNode {
public:
    char content;
    BitNode* leftchild{}, * rightchild{};
    BitNode() {
        this->content = '\0';
        this->leftchild = this->rightchild = NULL;
    }
    BitNode(char x) {
        this->content = x;
        this->leftchild = this->rightchild = NULL;
    }
};
class Link2 {
public:
    BitNode* content;
    Link2* next;
    Link2() { this->next == nullptr; }
    Link2(BitNode* a) {
        this->content = a;
        this->next = NULL;
    }

};
class Queue2 {
public:
    Link2* first, * last;
};
void initialQueue2(Queue2& Y) {
    Y.first = Y.last = new Link2();
    Y.first->next = NULL;

}
bool Empty2(Queue2 Y) {
    if (Y.first == Y.last)
        return 1;
    else
        return 0;
}
void InQueue2(Queue2& Y, BitNode* x) {
    Link2* y = new Link2(x);
    y->next = nullptr;
    Y.last->next = y;
    Y.last = y;
}
bool OutQueue2(Queue2& Y, BitNode*& x) {
    if (Y.first == Y.last)
        return false;

    Link2* y = Y.first->next;
    x = y->content;

    Y.first->next = y->next;
    if (Y.last == y)
        Y.last = Y.first;
    delete y;
    return true;
}
void DestroyQueue2(Queue2& Y) {
    Link2* y;
    while (Y.first != Y.last) {
        y = Y.first->next;
        Y.first->next = y->next;
        if (Y.last == y)
            Y.last = Y.first;
        delete y;
    }
    delete Y.first;
}
BitNode* Built(char p[], char q[], int& count, int plength, int qlength) {
    if (plength == 0 || qlength == 0) {
        count--;
        return NULL;
    }
    if (count >= plength) {
        count--;
        return NULL;
    }
    BitNode* T = new BitNode(p[count]);
    int n = 0;
    while (q[n] != p[count]) {
        n++;
    }
    char middleleft[100]; int leftlength = 0;
    char middleright[100]; int rightlength = 0;
    for (; leftlength < n; leftlength++)
        middleleft[leftlength] = q[leftlength];
    if (leftlength == 0)
        middleleft[leftlength] = '\0';
    for (int i = n + 1; i < qlength; i++)
        middleright[rightlength++] = q[i];
    if (rightlength == 0)
        middleright[rightlength] = '\0';
    T->leftchild = Built(p, middleleft, ++count, plength, leftlength);
    T->rightchild = Built(p, middleright, ++count, plength, rightlength);
    return T;
}

void PreOrder(BitNode* T, Queue& Y) {
    if (T == NULL)
        return;
    InQueue(Y, T->content);
    PreOrder(T->leftchild, Y);
    PreOrder(T->rightchild, Y);
}

void LevelOrder(BitNode* T) {
    Queue2 Y;
    initialQueue2(Y);
    BitNode* p;
    InQueue2(Y, T);
    int k = 1;
    if (T->leftchild == NULL && T->rightchild == NULL)
        k = 10;
    while (!Empty2(Y)) {
        OutQueue2(Y, p);

        if (!Empty2(Y) || k == 1)
            cout << p->content << ",";
        else
            cout << p->content;

        if (p->leftchild != NULL)
            InQueue2(Y, p->leftchild);

        if (p->rightchild != NULL)
            InQueue2(Y, p->rightchild);
        k++;
    }

}

int main() {

    Queue Y;
    initialQueue(Y);
    char temp{};
    cout << "Input" << endl;
    int count = 0;
    char p[99]; int plength = 0;
    char q[99]; int qlength = 0;
    cin >> q;
    cin >> p;
    while (p[plength] != '\0')
        plength++;

    while (q[qlength] != '\0')
        qlength++;
    BitNode* T = Built(p, q, count, plength, qlength);
    cout << "Output" << endl;
    PreOrder(T, Y);
    while (!Empty(Y)) {
        OutQueue(Y, temp);
        if (!Empty(Y))
            cout << temp << ",";
        else
            cout << temp;
    }
    cout << endl;
    LevelOrder(T);
    cout << endl;
    cout << "End";

    return 0;
}

能麻烦看一下代码有什么问题嘛运行不了QAQ

img