NOJ資料結構017——輸出以二叉樹表示的算術表達式

NOJ資料結構017——輸出以二叉樹表示的算術表達式

本題及考查二叉樹的中序周遊。

代碼:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    char data;
    struct Node* LChild;
    struct Node* RChild;
}BiNode, *BiTree;

void CreateTree(BiTree *T);  //建立二叉樹
void PrePrintTree(BiTree T); //先序輸出

int main()
{
    BiTree T;
    CreateTree(&T);
    PrePrintTree(T);
    return 0;
}

void CreateTree(BiTree *T)
{  //建立二叉樹
    char c;
    c = getchar();
    if (c == '#') {
        *T = NULL;
        return;
    }
    *T = (BiTree) malloc (sizeof(BiNode));
    (*T)->data = c;
    CreateTree(&(*T)->LChild);
    CreateTree(&(*T)->RChild);
}

void PrePrintTree(BiTree T)
{  //中序輸出
    if (T == NULL) {
        return;
    }
    PrePrintTree(T->LChild);
    printf("%c", T->data);
    PrePrintTree(T->RChild);
}