构造一颗表达式树

来源:互联网 发布:统一台湾契机 知乎 编辑:程序博客网 时间:2024/05/02 00:57

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


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

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


tree.h

#include<string>typedef std::string ElementType;#ifndef _Tree_H#define _Tree_Hstruct 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(")");}elsePrintinOrder(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(")");}elsePrintinOrder(t->right);}}


infixToSuffix.h

#pragma once#include<ctype.h>#include<string>#include<stack>using namespace std;#define MAXN 100char 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;elsereturn 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(")");}elsePrintinOrder(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(")");}elsePrintinOrder(t->right);}}


0 0
原创粉丝点击