-
Notifications
You must be signed in to change notification settings - Fork 9
/
二叉树_线索_表达式求值__.cpp
147 lines (136 loc) · 2.85 KB
/
二叉树_线索_表达式求值__.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct TNode
{
char data;
struct TNode * lChild;//左孩子
struct TNode * rChild;//右孩子
}TNode,*BitTree;
/**
expr:指向表达式字符串的指针
start:为要装换的表达式字符串的起始位置
end:为要装换的表达式字符串的结束位置的后一位
root_pos:记录当前要转化的表达式生成二叉树的根节点操作符的位置
flag:记录是否是当前搜索在括号里面
multiplication_division_pos:记录当前表达式中括号外面最右端的*、/、^位置
plus_minus_pos:记录当前表达式中括号外面最右端的+、-位置
**/
BitTree buildTree(char expr[],int start,int end)
{
//printf("输入的表达式:%s\n",expr);
//printf("表达式长度:%d\n",end-start);
if(end-start==1)
{
BitTree T=(BitTree)malloc(sizeof(TNode));
T->data=expr[start];
//printf("[%c]\n",T->data);
T->lChild=NULL;
T->rChild=NULL;
return T;
}
int root_pos = 0;
int flag = 0;
int plus_minus_pos = 0;
int multiplication_division_pos = 0;
for(int i=start;i<end;i++)
{
if(expr[i]=='(' || expr[i]=='[')
{
flag++;
}
if(expr[i]==')' || expr[i]==']')
{
flag--;
}
if(flag==0)
{
if(expr[i]=='*' || expr[i]=='/' || expr[i]=='^')
{
multiplication_division_pos = i;
}
else if(expr[i]=='+' || expr[i]=='-')
{
plus_minus_pos=i;
}
}
}
if((plus_minus_pos==0) && (multiplication_division_pos == 0))
{
buildTree(expr,start+1,end-1);
}
else
{
if(plus_minus_pos>0)
{
root_pos=plus_minus_pos;
}
else if(multiplication_division_pos>0)
{
root_pos=multiplication_division_pos;
}
BitTree T=(BitTree)malloc(sizeof(TNode));
T->data=expr[root_pos];
//printf("%d\n",root_pos);
cout<<start<<" "<<root_pos<<endl;
T->lChild=buildTree(expr,start,root_pos);
T->rChild=buildTree(expr,root_pos+1,end);
return T;
}
}
int visit(TNode *&T)
{
printf("%c",T->data);
}
int PreOrderTraverse(BitTree T)
{
if(T)
{
visit(T);
PreOrderTraverse(T->lChild);
PreOrderTraverse(T->rChild);
}
}
int InOrderTraverse(BitTree T)
{
if(T)
{
InOrderTraverse(T->lChild);
visit(T);
InOrderTraverse(T->rChild);
}
}
int PostOrderTraverse(BitTree T)
{
if(T)
{
PostOrderTraverse(T->lChild);
PostOrderTraverse(T->rChild);
visit(T);
}
}
int main()
{
char expr[200];
int start,end;
printf("请输入表达式:");
scanf("%s", expr);
start=0;
end=strlen(expr);
BitTree T;
T=buildTree(expr,start,end);
printf("\n\n");
printf("先序序列:\n");
PreOrderTraverse(T);
printf("\n\n:");
printf("中序序列:\n");
InOrderTraverse(T);
printf("\n\n");
printf("后序序列:\n");
PostOrderTraverse(T);
//printf("Your result is %d\n",cal(b));
}