怎样建立代数式的二叉树?

来源:百度知道 编辑:UC知道 时间:2024/06/10 21:43:06
比如user输入代数式:A+B-C*D/E。
建立的binary search tree为:
-
+ /
A B * E
# # # # C D # #
怎样建立这个树呢?
谢谢了!

#include<stdio.h>
#include<stdlib.h>
#include<fstream.h>
#include<crtdbg.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef struct tree{
char c;
struct tree *lchild;
struct tree *rchild;
}tree,*Tree;

//***************************************************************************
//栈的储存结构

typedef struct{
//运算符栈
char *base;
char *top;
int stacksize;
}SqStack1;

typedef struct{
//运算数栈
Tree *base;
Tree *top;
int stacksize;
}SqStack2;

//***************************************************************************
//以下是运算符栈的基本操作函数

Status InitStack(SqStack1 &S){
//初始化一个栈
S.base=(char