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

//简单起见,每个运算数节点存储的为小写英文字母
#include 
#include 
#include 
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* stack[100];
    int top=-1;
    gets(data);
    for(int i=0;data[i]!='\0';i++){
        if(strchr("+-*/",data[i])!=NULL){
            struct BinTreeNode* temp=CreateNode(data[i]);
            struct BinTreeNode* t1=stack[top--];
            struct BinTreeNode* t2=stack[top--];
            temp->Left=t2;
            temp->Right=t1;
            stack[++top]=temp;
        }
        else{
            struct BinTreeNode* temp=CreateNode(data[i]);
            stack[++top]=temp;
        }
    }
    struct BinTreeNode* ExpTree=stack[top];
    //为了验证,先序遍历一遍
    PreTrversal(ExpTree);
    return 0;
}

你可能感兴趣的:(树--二叉树)