表达式转二叉树并三种方式输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
//将中缀表达式转换为前缀表达式。
typedef struct Node
{
    char key;
    struct Node * left;
    struct Node * right;
}Node;
//search for the operator with the highest grade in a
int search(char a[], int begin, int end)
{
    int tag = -1;
    int isInBrackets = 0;//是否在括号里面
    if(a[begin] == '(' && a[end] == ')')
    {
        begin += 1;
        end -= 1;
    }
    int amExist = 0;//是否存在。
    for(int i = begin; i < end; i++)
    {
        if(a[i] == '(')
            isInBrackets++;
        if(a[i] == ')')
            isInBrackets--;
        if((a[i] == '+' || a[i] == '-') && isInBrackets == 0)
        {
            tag = i;
            amExist = 1;
        }
        if((a[i] == '*' || a[i] == '/') && isInBrackets == 0 && amExist == 0)
        {
            tag = i;
        }
    }
    return tag;
}
//build the binary tree
Node * buildBinaryTree(char a[], int begin, int end)
{
    Node * p = NULL;
    if(begin == end)
    {
        p = (Node *)malloc(sizeof(Node));
        p->key = a[begin];
        p->left = NULL;
        p->right = NULL;
    }
    else
    {
        int tag;
        tag = search(a,begin,end);
        if (tag<0) {
            printf("对不起,请输入正确的表达式。");
            return NULL;//tag小于0的时候不是表达式。
        }
        p = (Node *)malloc(sizeof(Node));
        p->key = a[tag];
        if(a[begin] == '(' && a[end] == ')')
        {
            begin += 1;
            end -= 1;
        }
        p->left = buildBinaryTree(a, begin, tag - 1);
        p->right = buildBinaryTree(a, tag + 1, end);
    }
    return p;
}
void outputBinaryTree_pre(Node * head)
{
    if(head)
    {
        printf("%c", head->key);
        outputBinaryTree_pre(head->left);
        outputBinaryTree_pre(head->right);
    }
}
void outputBinaryTree_in(Node * head)
{
    if(head)
    {
       
        outputBinaryTree_in(head->left);
        printf("%c", head->key);
        outputBinaryTree_in(head->right);
    }
}
void outputBinaryTree_post(Node * head)
{
    if(head)
    {
       
        outputBinaryTree_post(head->left);
        outputBinaryTree_post(head->right);
        printf("%c", head->key);
    }
}


int main()
{
    printf("please input a expression:");
    char input[N];
    while(scanf("%s", input))
    {
        if(strcmp(input, "exit") == 0)
        {
            break;
        }
        Node * head = NULL;
        int length = (int)strlen(input);
        head = buildBinaryTree(input, 0, length - 1);
        printf("前缀输出为:\n");
        outputBinaryTree_pre(head);//这个必须放在在里面,因为他是一个个输出的。
        printf("\n中缀输出为:\n");
        outputBinaryTree_in(head);//这个必须放在在里面,因为他是一个个输出的。
        printf("\n后缀输出为:\n");
        outputBinaryTree_post(head);//这个必须放在在里面,因为他是一个个输出的。
        printf("\n");
    }
   
   
   
   
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值