Tree

2022-11-10
一、目的 Objective
通过实验强化对二叉树的理解;
二、内容 Contents
1. 执行并分析下面提供的源码,画出生成的二叉树,并验证遍历的准确性;
2. 利用源码,生成下图所示二叉树的创建,并分别通过:先序遍历、中序遍历、后序遍历得到输出序列并验证准确性;
3. 根据二叉树的遍历序列,画出对应的二叉树,并通过代码验证准确性;
. 中序遍历:BDCEAFHG
. 后序遍历:DECBHGFA
三、参考代码 Source
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    char data;
    struct Node *leftChild;
    struct Node *rightChild;
} Tree;

Tree *createNode(char data)
{
    Tree *node = (Tree *)malloc(sizeof(Tree));
    node->data = data;
    node->leftChild = NULL;
    node->rightChild = NULL;
    return node;
}

void insertNode(Tree *tree, Tree *l, Tree *r)
{
    tree->leftChild = l;
    tree->rightChild = r;
}

void preOrder(Tree *tree)
{
    if (tree)
    {
        printf("%c", tree->data);
        preOrder(tree->leftChild);
        preOrder(tree->rightChild);
    }
}

void inOrder(Tree *tree)
{
    if (tree)
    {
        inOrder(tree->leftChild);
        printf("%c", tree->data);
        inOrder(tree->rightChild);
    }
}

void postOrder(Tree *tree)
{
    if (tree)
    {
        postOrder(tree->leftChild);
        postOrder(tree->rightChild);
        printf("%c", tree->data);
    }
}

int main(void)
{
    Tree *A = createNode('A');
    Tree *B = createNode('B');
    Tree *C = createNode('C');
    Tree *D = createNode('D');
    Tree *E = createNode('E');
    Tree *F = createNode('F');
    Tree *G = createNode('G');
    Tree *H = createNode('H');
    Tree *I = createNode('I');
    Tree *J = createNode('J');
    insertNode(A, B, C);
    insertNode(B, D, NULL);
    insertNode(C, NULL, E);
    insertNode(D, F, G);
    insertNode(E, H, NULL);
    insertNode(G, I, J);
    preOrder(A);
    printf("\n");
    inOrder(A);
    printf("\n");
    postOrder(A);
    printf("\n");
    return 0;
}
[Section End]
要求 Requirement
1. 利用 线上环境 或线下环境,独立完成;
2. 写出实验的具体流程和实验的结果,相应的地方需要截图说明;
3. 程序调试运行中出现的错误信息原因分析;
4. 完成 实验报告 并提交到 学习通;详见作业要求;