-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.1.cpp
217 lines (217 loc) · 4.26 KB
/
8.1.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<iostream>
using namespace std;
#define datatype char
#define pointer struct node *
#define bitree pointer
#define MAXSIZE 100
struct node {
datatype data;
pointer lchild;
pointer rchild;
};
typedef struct {
pointer data[MAXSIZE];
int front, rear;
}sqqueue;
void initiateQueue(sqqueue *sq) {
sq -> front = 0;
sq -> rear = 0;
}
bool isEmpty(sqqueue *sq) {
if(sq -> rear == sq -> front)
return true;
else
return false;
}
bool isFull(sqqueue *sq) {
if((sq -> rear - sq -> front + MAXSIZE + 1) % MAXSIZE == 0){
return true;
}
else
return false;
}
void enSqqueue(sqqueue *sq, pointer t) {
if(isFull(sq) == 1)
return;
sq -> rear = (sq -> rear + 1) % MAXSIZE;
sq -> data[sq ->rear] = t;
return;
}
void deSqqueue(sqqueue *sq, pointer *t) {
if(isEmpty(sq) == 1)
return;
sq -> front = (sq -> front + 1) % MAXSIZE;
*t = sq -> data[sq -> front];
}
void preorder(bitree t) {
if(t == NULL)
return;
cout<<t -> data;
cout<<" ";
preorder(t -> lchild);
preorder(t -> rchild);
}
void inorder(bitree t) {
if(t == NULL)
return;
inorder(t -> lchild);
cout<<t -> data;
cout<<" ";
inorder(t -> rchild);
}
void postorder(bitree t) {
if(t == NULL)
return;
postorder(t -> lchild);
postorder(t -> rchild);
cout<<t -> data;
cout<<" ";
}
void floororder(bitree t) {
sqqueue queue;
sqqueue *sq = &queue;
initiateQueue(sq);
enSqqueue(sq , t);
pointer p;
while(isEmpty(sq) == 0) {
deSqqueue(sq, &p);
cout<<p -> data;
cout<<" ";
if(p -> lchild != NULL)
enSqqueue(sq, p -> lchild);
if(p -> rchild != NULL)
enSqqueue(sq, p -> rchild);
}
cout<<endl;
return;
}
bitree preCreate() {
bitree t;
char ch;
cin>>ch;
if(ch == '@')
return NULL;
t = new node;
t -> data = ch;
t -> lchild = preCreate();
t -> rchild = preCreate();
return t;
}
void leaf(bitree t, int *num) {
if(t == NULL)
return;
if(t -> lchild == NULL && t -> rchild == NULL) {
++*num;
}
if(t -> lchild != NULL)
leaf(t -> lchild, num);
if(t -> rchild != NULL)
leaf(t -> rchild, num);
}
void coutLeaf(bitree t) {
int num = 0;
leaf(t, &num);
cout<<"叶子节点数"<<endl;
cout<<num<<endl;
}
void node(bitree t, int *num) {
if(t == NULL)
return;
++*num;
if(t -> lchild != NULL)
node(t -> lchild, num);
if(t -> rchild != NULL)
node(t -> rchild, num);
}
void coutNode(bitree t) {
int num = 0;
node(t, &num);
cout<<"节点数"<<endl;
cout<<num<<endl;
}
void changeChild(bitree t) {
if(t == NULL)
return;
pointer p = t -> lchild;
t -> lchild = t -> rchild;
t -> rchild = p;
changeChild(t -> lchild);
changeChild(t -> rchild);
}
void number(bitree t, bool *flag) {
if(t == NULL)
return;
if(t -> data >= '9' || t -> data <= '0'){
*flag = false;
return;
}
if(t -> lchild != NULL)
number(t -> lchild, flag);
if(t -> rchild != NULL)
number(t -> rchild, flag);
}
void onlyNumber(bitree t) {
bool flag = true;
number(t, &flag);
if(!flag)
cout<<"有非数字字符"<<endl;
else
cout<<"都是数字字符"<<endl;
}
void Equals(bitree t1, bitree t2, bool *flag) {
if(t1 == NULL && t2 == NULL) {
return;
}
if(t1 || t2) {
if(t1 && t2);
else {
*flag = false;
return;
}
}
if(t1 -> data != t2 -> data) {
*flag = false;
return;
}
Equals(t1 -> lchild, t2 -> lchild, flag);
Equals(t1 -> rchild, t2 -> rchild, flag);
}
void allEquals(bitree t) {
cout<<"请用前序创建树2"<<endl;
bitree t2 = preCreate();
bool flag = true;
if(t == NULL && t2 == NULL) {
cout<<"两树相等"<<endl;
return;
}
Equals(t, t2, &flag);
if(flag)
cout<<"两树相等"<<endl;
else
cout<<"两树不等"<<endl;
}
int main(void) {
cout<<"请用前序创建树"<<endl;
bitree t = preCreate();
int judge;
while(true){
cout<<"输入0进行前序遍历,输入1中序遍历,输入2后序遍历,输入3层次遍历\n输入4求叶子,输入5所有节点,输入6交换子树\n输入7只有数字,输入8两树相等,输入其他结束。"<<endl;
cin>>judge;
switch(judge){
case 0:cout<<"先序"<<endl;preorder(t);cout<<endl;break;
case 1:cout<<"中序"<<endl;inorder(t);cout<<endl;break;
case 2:cout<<"后序"<<endl;postorder(t);cout<<endl;break;
case 3:cout<<"层次"<<endl;floororder(t);break;
case 4:coutLeaf(t);break;
case 5:coutNode(t);break;
case 6:cout<<"交换子树"<<endl;changeChild(t);break;
case 7:onlyNumber(t);break;
case 8:allEquals(t);break;
default:return 0;
}
system("pause");
system("cls");
}
return 0;
}
//先序建立用:ABD@G@@@CE@@F@@ ppt:42