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

//简单起见,每个运算数节点存储的为小写英文字母
#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;
}

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据中缀表达式转换表达式的算法,可以分为以下步骤: 1. 创建一个空栈和一个空的表达式。 2. 从左到右扫描中缀表达式中的每个元素。 3. 如果当前元素是操作数,则创建一个只包含该操作数的表达式,并将其压入栈中。 4. 如果当前元素是操作符,则创建一个只包含该操作符的表达式,并将其弹出栈两次,作为该操作符的左右子,然后将该表达式压入栈中。 5. 重复步骤2-4,直到扫描完整个中缀表达式。 6. 最后,栈中只剩下一个表达式,即为所求的表达式。 下面是一个Python实现的例子: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def infix_to_expression_tree(infix): precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3} stack = [] for char in infix: if char.isalnum(): node = TreeNode(char) stack.append(node) elif char in precedence: node = TreeNode(char) while stack and stack[-1].val != '(' and precedence[char] <= precedence[stack[-1].val]: node.right = stack.pop() node.left = stack.pop() stack.append(node) stack.append(node) elif char == '(': stack.append(TreeNode(char)) elif char == ')': while stack and stack[-1].val != '(': node = stack.pop() if node.val in precedence: node.right = stack.pop() node.left = stack.pop() stack.append(node) stack.pop() while stack: node = stack.pop() if node.val in precedence: node.right = stack.pop() node.left = stack.pop() stack.append(node) return stack[0] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值