构造一颗表达式树

数据结构与算法分析——c语言描述 第四章树 构造一颗表达式树


输入一个表达式,程序转换成逆序,再然后根据逆序构建表达式树,然后再用中序遍历输出表达式。

以前的文章也实现的用逆序转表达式,用栈来实现。这个用树来实现。树更加万能吧,逆序,中序,前序什么的都有。


tree.h

#include<string>
typedef std::string ElementType;
#ifndef _Tree_H
#define _Tree_H

struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *Tree;

Tree createSearchTree();
void makeEmpty(Tree t);
Tree insert(ElementType X, Tree t);
void  PrintinOrder(Tree t);
#endif


tree.cpp

#include"tree.h"
#include"fatal.h"

extern double operatorPriority(char c);
extern double operatorCmp(char c1, char c2);


struct TreeNode {
	ElementType element;
	Tree left;
	Tree right;
};

Tree createSearchTree()
{
	return NULL;
}

void makeEmpty(Tree t) {
	if (t) {
		makeEmpty(t->left);
		makeEmpty(t->right);
		free(t);
	}
}

Tree insert(ElementType X, Tree t) {
	if (t == NULL) {//包含树没有初始化
		t = new TreeNode;
		if (t == NULL)
			Error("out of memory");
		t->element = X;
		t->left = NULL;
		t->right = NULL;
	}
	else {
		if (X < t->element)
			t->left = insert(X, t->left);
		else if (X>t->element)
			t->right = insert(X, t->right);
	}
	return t;//两种情况
}

void PrintinOrder(Tree t)
{
	if (t->left)
	{
		if (operatorCmp(t->element[0], t->left->element[0]) > 0)
		{
			printf("(");
			PrintinOrder(t->left);
			printf(")");
		}
		else
			PrintinOrder(t->left);
	}
	printf("%s", t->element.c_str());
	if (t->right)
	{
		if (operatorCmp(t->element[0], t->right->element[0]) > 0)
		{
			printf("(");
			PrintinOrder(t->right);
			printf(")");
		}
		else
			PrintinOrder(t->right);
	}

}


infixToSuffix.h

#pragma once
#include<ctype.h>
#include<string>
#include<stack>
using namespace std;
#define MAXN 100

char expression[MAXN];//输入的表达式
string postfix[MAXN];//转换成的逆序表达式

int i = 0, j = 0;//i是expression的游标,postfix是逆序表达式的游标

double operatorCmp(char c1, char c2);//c1为将要插入栈的运算符,c2为栈顶运算符,1表示c1优先级大于c2,0为相等,-1为小于
#define putNum()  postfix[j++] = num,num.clear();//把数字输出到逆序表达式
#define putOperator() oper= operator_stack.top(),operator_stack.pop(),postfix[j++] = oper;//把符号输出到逆序表达式

double operatorPriority(char c) {//运算符优先级比较
	if (c == '^')
		return 3;
	else if (c == '*' || c == '/')
		return 2;
	else if (c == '+' || c == '-')
		return  1;
}


void ProcessingOperator(char &c, char &oper, string &num, stack<char>&operator_stack) {//i指向的表达是为运算符字符处理
	if (!num.empty())
		putNum();
	if (operator_stack.empty())
		operator_stack.push(c);
	else if (operatorCmp(c, operator_stack.top()) >0) {
		operator_stack.push(c);
	}
	else {
		while (1) {
			putOperator();
			if (operatorCmp(c, oper) >0 || operator_stack.empty()) {
				operator_stack.push(c);
				break;
			}
		}
	}
}

int midToPostfix() {//中缀转后缀,返回int是指括号后面是否直接是数字
	int flag = 0;
	stack<char>operator_stack;
	string num;
	char c, oper;
	for (; expression[i] != '\0'; i++) {
		c = expression[i];
		if (c == '(') {
			if (isdigit(expression[i - 1]) || expression[i - 1] == ')')//遇到3(4)(3)的情况,要在括号前自行处理省去的*
			{
				c = '*';
				ProcessingOperator(c, oper, num, operator_stack);
			}
			i++;
			if (midToPostfix())//处理(4)4的情况
			{
				c = '*';
				ProcessingOperator(c, oper, num, operator_stack);
			}
			continue;
		}
		else if (c == ')') {
			if (isdigit(expression[i + 1]))
				flag = 1;
			break;
		}
		else if (isdigit(c) || c == '.')
			num += c;
		else {//运算符
			ProcessingOperator(c, oper, num, operator_stack);
		}
	}
	if (!num.empty())//处理表达式最后的数字
		postfix[j++] = num;
	while (!operator_stack.empty()) {//处理留在栈的运算符
		postfix[j++] = operator_stack.top();
		operator_stack.pop();
	}
	if (flag)
		return 1;
	else
		return 0;
}

double operatorCmp(char c1, char c2) {
	double t1, t2;
	t1 = operatorPriority(c1);
	t2 = operatorPriority(c2);
	return t1 - t2;
}


main.cpp

#include"tree.h"
#include"fatal.h"

extern double operatorPriority(char c);
extern double operatorCmp(char c1, char c2);


struct TreeNode {
	ElementType element;
	Tree left;
	Tree right;
};

Tree createSearchTree()
{
	return NULL;
}

void makeEmpty(Tree t) {
	if (t) {
		makeEmpty(t->left);
		makeEmpty(t->right);
		free(t);
	}
}

Tree insert(ElementType X, Tree t) {
	if (t == NULL) {//包含树没有初始化
		t = new TreeNode;
		if (t == NULL)
			Error("out of memory");
		t->element = X;
		t->left = NULL;
		t->right = NULL;
	}
	else {
		if (X < t->element)
			t->left = insert(X, t->left);
		else if (X>t->element)
			t->right = insert(X, t->right);
	}
	return t;//两种情况
}

void PrintinOrder(Tree t)
{
	if (t->left)
	{
		if (operatorCmp(t->element[0], t->left->element[0]) > 0)
		{
			printf("(");
			PrintinOrder(t->left);
			printf(")");
		}
		else
			PrintinOrder(t->left);
	}
	printf("%s", t->element.c_str());
	if (t->right)
	{
		if (operatorCmp(t->element[0], t->right->element[0]) > 0)
		{
			printf("(");
			PrintinOrder(t->right);
			printf(")");
		}
		else
			PrintinOrder(t->right);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值