cxfans
9/3/2019 - 1:30 AM

p127-20

p127-20

#include <stdio.h>

typedef struct node {
    char data[10];
    struct node *left, *right;
} BTree;

char convert(BTree *t) {
    if (t) {
        if (t->left) {
            if (!t->left->left) printf("(");
            convert(t->left);
        }
        printf("%s", t->data);
        if (t->right) {
            if (!t->right->left && t->right->right) printf("(");
            convert(t->right);
            printf(")");
        }
    }
}


void BtreeToExp(BTree *root, int deep) {
    if (!root) return;
    else if (!root->left && !root->right) {
        printf("%s", root->data);
    } else {
        if (deep > 1)printf("(");
        BtreeToExp(root->left, deep + 1);
        printf("%s", root->data);
        BtreeToExp(root->right, deep + 1);
        if (deep > 1)printf(")");
    }
}

void BtreeToE(BTree *root) {
    BtreeToExp(root, 1);
}