把中缀表达式转换为表达式树

本文介绍了一种使用C语言构建表达式树的方法,并通过先序遍历进行验证。通过对输入的中缀表达式进行解析,利用栈来辅助构造二叉表达式树,最终实现表达式的树状结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//简单起见,每个运算数节点存储的为小写英文字母
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct BinTreeNode{
    char Element;
    struct BinTreeNode* Left;
    struct BinTreeNode* Right;
};
struct BinTreeNode* CreateNode(char ch)
{
    struct BinTreeNode* temp;
    temp=(struct BinTreeNode*)malloc(sizeof(struct BinTreeNode));
    temp->Left=NULL;
    temp->Right=NULL;
    temp->Element=ch;
    return temp;
}
void PreTrversal(struct BinTreeNode* ExpTree)
{
    if(ExpTree==NULL)
        return;
    printf("%c ",ExpTree->Element);
    if(ExpTree->Left!=NULL)
        PreTrversal(ExpTree->Left);
    if(ExpTree->Right!=NULL)
        PreTrversal(ExpTree->Right);
    return;
}
int main()
{
    char data[100];
    struct BinTreeNode* stack1[100];//存运算数
    struct BinTreeNode* stack2[100];//存运算符
    int top1=-1;
    int top2=-1;
    gets(data);
    for(int i=0;data[i]!='\0';i++){
        if(data[i]=='('){
            struct BinTreeNode* temp;
            temp=CreateNode(data[i]);
            stack2[++top2]=temp;
        }
        else if(data[i]==')'){
            while(stack2[top2]->Element!='('){
               struct BinTreeNode* temp=stack2[top2];
               struct BinTreeNode* t1=stack1[top1--];
               struct BinTreeNode* t2=stack1[top1--];
               temp->Left=t2;
               temp->Right=t1;
               stack1[++top1]=temp;
               top2--;
            }
            top2--;
        }
        else if(strchr("+-*/",data[i])!=NULL){
            if(data[i]=='+'||data[i]=='-'){
                while(top2>=0&&stack2[top2]->Element!='('){
                   struct BinTreeNode* temp=stack2[top2];
                   struct BinTreeNode* t1=stack1[top1--];
                   struct BinTreeNode* t2=stack1[top1--];
                   temp->Left=t2;
                   temp->Right=t1;
                   stack1[++top1]=temp;
                   top2--;
                }
            }
            else if(data[i]=='*'||data[i]=='/'){
                while(top2>=0&&stack2[top2]->Element!='('&&stack2[top2]->Element!='+'&&stack2[top2]->Element!='-'){
                   struct BinTreeNode* temp=stack2[top2];
                   struct BinTreeNode* t1=stack1[top1--];
                   struct BinTreeNode* t2=stack1[top1--];
                   temp->Left=t2;
                   temp->Right=t1;
                   stack1[++top1]=temp;
                   top2--;
                }
            }
            struct BinTreeNode* temp=CreateNode(data[i]);
            stack2[++top2]=temp;
        }
        else{
            struct BinTreeNode* temp=CreateNode(data[i]);
            stack1[++top1]=temp;
        }
    }
    while(top2>=0){//处理栈中剩余的运算符
        struct BinTreeNode* temp=stack2[top2];
        struct BinTreeNode* t1=stack1[top1--];
        struct BinTreeNode* t2=stack1[top1--];
        temp->Left=t2;
        temp->Right=t1;
        stack1[++top1]=temp;
        top2--;
    }
    struct BinTreeNode* ExpTree=stack1[top1];
    //为了验证,先序遍历一遍
    PreTrversal(ExpTree);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值