c_cpp 表达式树 - 中缀到后缀

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 表达式树 - 中缀到后缀相关的知识,希望对你有一定的参考价值。

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

typedef struct node {
    int data;
    struct node *left,
                *right,
                *parent;
}node_t;

void insertTreeNode(node_t **node, int input){
    if(*node == NULL){
        *node = (node_t*)malloc(sizeof(node_t));
        (*node)->data = input;
        (*node)->left = (*node)->right = (*node)->parent = NULL; 
    }
    else{
        if(input <= (*node)->data){
            insertTreeNode(&((*node)->left), input);
            (*node)->left->parent = (*node);
        }
        else{
            insertTreeNode(&((*node)->right), input);
            (*node)->right->parent = (*node);
        }
    }
}

void printTreepostOrder(node_t *node, char *data){
    if(node == NULL) return;
    printTreepostOrder(node->left, data);
    printTreepostOrder(node->right, data);
    printf("%c ", data[node->data]);
}

bool isOperator(char a){
    if(a=='+' or a=='-' or a=='*' or a=='/' or a=='^' or a=='(' or a==')')
        return true;
    return false;
}

int valueOperator(char a){
    if(a=='+' or a=='-') return 1;
    if(a=='*' or a=='/') return 2;
    if(a=='^') return 3;
    if(a=='(') return -4;
    if(a==')') return 4;
}

int main(){
    node_t *root = NULL;
    char infix[1000];
    char v_operator[1000], v_operand[1000];
    int p_operator[1000], p_operand[1000];
    for(int i=0; i<1000; i++){
        v_operand[i]=v_operator[i]='\0';
    }
    gets(infix);
    
    int cnttor=0, cntand=0;
    
    for(int i=0; i<strlen(infix); i++){
        if(isOperator(infix[i])){
            v_operator[cnttor] = infix[i];
            p_operator[cnttor++] = i;
        }
        else{
            v_operand[cntand] = infix[i];
            p_operand[cntand++] = i;
        }
    }
    
    bool flag[strlen(v_operator)];
    for(int i=0; i<strlen(v_operator); i++){
        flag[i]=false;
    }
    
    // puts(v_operand);
    // printf("%d ", strlen(v_operator)); puts(v_operator);
    
    int bracket=0;
    while(true){
        int lowest = 99999, index=-1, real_index, tmp;
        for(int i=strlen(v_operator)-1; i>=0; i--){
            tmp = valueOperator(v_operator[i]);
            if(tmp==4 or tmp==-4)
                bracket += tmp;
            if(tmp+bracket < lowest and flag[i]==false and v_operator[i]!='(' and v_operator[i]!=')'){
                // printf("v = %c %d\n", v_operator[i], tmp+bracket);
                lowest = tmp + bracket;
                if(index!=-1)
                    flag[index] = false;
                index = i;
                real_index = p_operator[i];
                flag[i] = true;
            }
        }
        
        // printf("index = %d %d\n", index, real_index);
        insertTreeNode(&root, real_index);
        
        int count = 0;
        for(int i=0; i<strlen(v_operator); i++)
            if(v_operator[i]=='(' or v_operator[i]==')' or flag[i]==true) count++;
        if(count==strlen(v_operator)) break;
    }
    
    for(int i=0; i<strlen(v_operand); i++){
        // printf("haiii %d\n", i);
        insertTreeNode(&root, p_operand[i]);
    }
    
    printTreepostOrder(root, infix);
    
    return 0;
}

/*
(1*(1*(8+4)))*(1/(2+1))
*/

以上是关于c_cpp 表达式树 - 中缀到后缀的主要内容,如果未能解决你的问题,请参考以下文章

前缀中缀后缀表达式

表达树—轻松获取前缀中缀后缀表达式

后缀表达式转为中缀表达式

C语言后缀表达式转换成中缀表达式

算术表达式的前缀表达式,中缀表达式和后缀表达式

中缀表达式转换前\后缀表达式的总结