表达式树的构造算法

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 
 
/************************************************************************** 文件名:treeexpression.c** 文件描述:本程序给出表达式树的构造算法*           在turboc 2.0 调试通过,注意只能输入个位数,且没有加上*           对括号的处理,读者可以自己将其加上去* 创建人: 颜清国   2006年5月4日* 说明: *************************************************************************/#include "stdio.h"#include "string.h"#include "stdlib.h"#include "conio.h"#define MAX 20#define MAXS 100/********************************** 用来求后缀表达式的栈************************************/typedef struct sstack{	char str[MAXS];	int top;}stack;/************************************** 定义树的结构体***************************************/typedef struct tagtree{ char data;                /*数据域*/ int flag;                 /*非递归操作时用来做标志*/ struct tagtree*lchild;    /*指向左孩子的指针域*/ struct tagtree*rchild;    /*指向右孩子的指针域*/}tree;/*入栈操作*/void push(stack *ta,char p){	ta->top++;	ta->str[ta->top]=p;}/*出栈,返回栈顶的值*/char pop(stack *ta){	char temp;	if(ta->top==-1)/*栈已经空*/	{		printf("stack is empty!");		return 0;	}	else	{		temp=ta->str[ta->top];		ta->top--;	}	return temp;}/******************************将中缀表达式转化为后缀表达式*******************************/  void trans(char mid[],char last[]){	stack temp;          /*临时栈,用来调整成后缀表达式*/	int lm=0,la=0,len=strlen(mid);	temp.top=-1;           /*初始栈为空*/	push(&temp,'(');    /*整个表达式要加上括号*/	while(lm < len)	{		switch(mid[lm])		{		case '-':    /*'+''-'转化时,'('前的OP均出栈*/		case '+':       /*注意必须先将整个表达式要加上括号*/			while(temp.str[temp.top]!='(')			{				last[la++]=pop(&temp);			}			push(&temp,mid[lm]);/*自己入栈*/			break;		case '*':		case '/':			while((temp.str[temp.top]=='*') 				||(temp.str[temp.top]=='/'))			{				last[la++]=pop(&temp);/*栈顶是'*','/'则出栈*/			}			push(&temp,mid[lm]); /*自己入栈*/			break;		case '(':			push(&temp,'('); /*是'('直接入栈*/			break;		case ')':			while(temp.str[temp.top]!='(')			{				last[la++]=pop(&temp); /*将'('前所有OP出栈*/			}			pop(&temp); /*将'('出栈,自己不入栈*/			break;     		default:			if((mid[lm] >= '0')&&(mid[lm] <= '9'))/*可以屏蔽其它字符*/			{				while((mid[lm] >='0')&&(mid[lm] <= '9'))				{					last[la++]=mid[lm++]; /*是数字保存到字串中*/				}				lm--;            /*需要退回来*/			}			break;		}		lm++;                       /*依次扫描待转换的字串*/	}	while(temp.top > 0)                /*第0个元素为'(',不用保存*/	{		last[la++]=pop(&temp);	}	last[la]='\0';   /*标志后缀表达式结束*/}/**************************************** 根据后缀和中缀表达式构造二叉树*****************************************/tree*PostCreateTree(char *post,char *mid,int n){ tree*root=NULL; int lp=0; if(n==0) return NULL; while(lp<n root-="" root="(tree*)malloc(sizeof(tree));"></n>data = post[n-1]; /*printf("\n%c",root->data); getch();*/ root->lchild = PostCreateTree(post,mid,lp); root->rchild = PostCreateTree(post+lp,mid+lp+1,n-lp-1); return root;}/************************************************ 用树形表示法输出树*************************************************/void DispTree(tree *root,int x,int y,int n)     /*n用来控制第一层树的高度*/{ int i=0; if(root !=NULL)  {    gotoxy(x,y);                               /*到相应结点输出*/    printf("%c",root->data);    if(root->lchild != NULL)                  /*处理左子树,这里只有第一次N为可变的,*/    {       i=1;                                   /*为的是能够输出整棵树,而不会被覆盖,*/       while(ilchild,x-n,y+n,2);       /*递归处理左子树*/     }     if(root->rchild != NULL)    {       i=1;       while(irchild,x+n,y+n,2);       /*递归处理右子树*/     }   }}void main(){	char mid[100],post[100];        tree *root;        printf("Please input the expression: ");	gets(mid);	trans(mid,post);        root=PostCreateTree(post,mid,strlen(mid));        printf("\nthe ruselt is:");        DispTree(root,10,4,5);	getch();}     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值