二叉树解决波兰表达式(C语言)

二叉树解决波兰表达式(C语言),第1张

 输入波兰表达式,并创建二叉树

tree* creat(tree *T)
{
    char data;

    if(x>'0'&&i<2)//如果根节点为数字时,将它的左右孩子为空
    {
        i++;
        return NULL;
    }
    scanf("%c",&data);
    getchar();
    x=data;//储存根节点的数据
    i=0;
    T=(tree*)malloc(sizeof(tree));
    T->data=data;
    T->lchild=creat(T->lchild);
    T->rchild=creat(T->rchild);
    return T;

}

根据波兰表达式计算

 

void count(tree *p)
{
    int i=0;
    stack *s;
    tree *T=p;
    s=(stack*)malloc(sizeof(stack));
    s->top=-1;//初始化栈
    push(s,T);
    while(!empty(p))
    {
        push(s,T);
        while(T->lchild)
        {

            T=T->lchild;
            push(s,T);
        }
        pop(s);
        T=pop(s);
        if(empty(s))
        {
            T=p;
            i=1;
        }
        switch(T->data)
        {
        case '+':
            T->data=(T->lchild->data-'0')+(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '-':
            T->data=(T->lchild->data-'0')-(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '*':
            T->data=(T->lchild->data-'0')*(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '/':
            T->data=(T->lchild->data-'0')/(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        default:
            break;
        }
        if(!empty(s))
        {
            T=pop(s);
            if(T->rchild)
            {
                T=T->rchild;
            }
        }
        if(i==1)
        {
            return;
        }

    }
}

 

 以下为全部代码

#include 
#include 
#define maxsize 20

char x=0;
int i=0;

typedef struct tree
{
    char data;
    struct tree *lchild;
    struct tree *rchild;
}tree;

typedef struct
{
    tree *data[maxsize];
    int top;
}stack;

tree* creat(tree *T)
{
    char data;

    if(x>'0'&&i<2)
    {
        i++;
        return NULL;
    }
    scanf("%c",&data);
    getchar();
    x=data;
    i=0;
    T=(tree*)malloc(sizeof(tree));
    T->data=data;
    T->lchild=creat(T->lchild);
    T->rchild=creat(T->rchild);
    return T;

}

/栈的 *** 作

void push(stack *s,tree *e)
{
    if(s->top>=maxsize)
    {
        return;
    }
    else
    {
        s->top++;
        s->data[s->top]=e;
    }
}

tree* pop(stack *s)
{
    tree *r;
    if(s->top==-1)
    {
        return;
    }
    else
    {
        r=s->data[s->top];
        s->top--;
    }
    return r;
}

 int empty(stack *s)
{
    if(s->top==-1)
        return 1;
    else
        return 0;
}


int count(tree *p)
{
    int i=0;
    stack *s;
    tree *T=p;
    s=(stack*)malloc(sizeof(stack));
    s->top=-1;//初始化栈
    push(s,T);
    while(!empty(p))
    {
        push(s,T);
        while(T->lchild)
        {

            T=T->lchild;
            push(s,T);
        }
        pop(s);
        T=pop(s);
        if(empty(s))
        {
            T=p;
            i=1;
        }
        switch(T->data)
        {
        case '+':
            T->data=(T->lchild->data-'0')+(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '-':
            T->data=(T->lchild->data-'0')-(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '*':
            T->data=(T->lchild->data-'0')*(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        case '/':
            T->data=(T->lchild->data-'0')/(T->rchild->data-'0');T->lchild=T->rchild=NULL;T->data=T->data+'0';
            break;
        default:
            break;
        }
        if(!empty(s))
        {
            T=pop(s);
            if(T->rchild)
            {
                T=T->rchild;
            }
        }
        if(i==1)
        {
            return;
        }

    }
}

void show1(tree *T)
{
    if(T==NULL)
    {
        return;
    }
    printf("%c",T->data);
    show1(T->lchild);
    show1(T->rchild);
}

int main()
{
    tree *t;
    t=creat(t);
    show1(t);
    printf("\n");
    count(t);
    printf("%c",t->data);
    return 0;
}

 样例:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/921399.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存