-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.1.cpp
More file actions
244 lines (214 loc) · 5.99 KB
/
7.1.cpp
File metadata and controls
244 lines (214 loc) · 5.99 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char TElemType;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
// 队列结构
typedef struct QueueNode {
BiTree data;
struct QueueNode *next;
} QueueNode;
typedef struct {
QueueNode *front, *rear;
} LinkQueue;
// 初始化队列
void InitQueue(LinkQueue *Q) {
Q->front = Q->rear = (QueueNode*)malloc(sizeof(QueueNode));
Q->front->next = NULL;
}
// 入队
void EnQueue(LinkQueue *Q, BiTree T) {
QueueNode *p = (QueueNode*)malloc(sizeof(QueueNode));
p->data = T;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
}
// 出队
int DeQueue(LinkQueue *Q, BiTree *T) {
if (Q->front == Q->rear) return 0;
QueueNode *p = Q->front->next;
*T = p->data;
Q->front->next = p->next;
if (Q->rear == p) Q->rear = Q->front;
free(p);
return 1;
}
// 4. 判断队列空
int QueueEmpty(LinkQueue Q) {
return Q.front == Q.rear;
}
// 先序创建二叉树
void CreateBTree(BiTree *T) {
char ch;
while ((ch = getchar()) == ' ' || ch == '\n');
if (ch == '#') *T = NULL;
else {
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data = ch;
CreateBTree(&((*T)->lchild));
CreateBTree(&((*T)->rchild));
}
}
// 先序遍历
void PreOrderTraverse(BiTree T) {
if (T) {
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
// 中序遍历
void InOrderTraverse(BiTree T) {
if (T) {
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
}
// 后序遍历
void PostOrderTraverse(BiTree T) {
if (T) {
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
// 统计叶子结点数
int CountLeaf(BiTree T) {
if (!T) return 0;
if (!T->lchild && !T->rchild) return 1;
return CountLeaf(T->lchild) + CountLeaf(T->rchild);
}
// 统计度为1的结点数
int CountDegree1(BiTree T) {
if (!T) return 0;
if ((T->lchild && !T->rchild) || (!T->lchild && T->rchild))
return 1 + CountDegree1(T->lchild) + CountDegree1(T->rchild);
return CountDegree1(T->lchild) + CountDegree1(T->rchild);
}
// 统计度为2的结点数
int CountDegree2(BiTree T) {
if (!T) return 0;
if (T->lchild && T->rchild)
return 1 + CountDegree2(T->lchild) + CountDegree2(T->rchild);
return CountDegree2(T->lchild) + CountDegree2(T->rchild);
}
// 树的深度
int TreeDepth(BiTree T) {
if (!T) return 0;
int ldepth = TreeDepth(T->lchild);
int rdepth = TreeDepth(T->rchild);
return (ldepth > rdepth ? ldepth : rdepth) + 1;
}
// 树的宽度
int TreeWidth(BiTree T) {
if (!T) return 0;
LinkQueue Q;
InitQueue(&Q);
EnQueue(&Q, T);
int width = 0, levelSize = 0;
while (!QueueEmpty(Q)) {
levelSize = 0;
// 统计当前层结点数
QueueNode *p = Q.front->next;
while (p) { levelSize++; p = p->next; }
if (levelSize > width) width = levelSize;
// 层序遍历,出队当前层,入队下一层
for (int i = 0; i < levelSize; i++) {
BiTree node;
DeQueue(&Q, &node);
if (node->lchild) EnQueue(&Q, node->lchild);
if (node->rchild) EnQueue(&Q, node->rchild);
}
}
return width;
}
// 查询结点层次
void LocateNode(BiTree T, char c, int l, int *level) {
if (!T || *level != 0) return;
l++;
if (T->data == c) { *level = l; return; }
LocateNode(T->lchild, c, l, level);
LocateNode(T->rchild, c, l, level);
}
// 找结点最大值
TElemType MaxElem(BiTree T) {
if (!T) return '\0';
TElemType max = T->data;
TElemType lmax = MaxElem(T->lchild);
TElemType rmax = MaxElem(T->rchild);
if (lmax > max) max = lmax;
if (rmax > max) max = rmax;
return max;
}
// 交换所有结点的左右子树
void SwapChildren(BiTree T) {
if (!T) return;
BiTree temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
SwapChildren(T->lchild);
SwapChildren(T->rchild);
}
// 收集叶子结点
void CollectLeaves(BiTree T, char *leaves, int *idx) {
if (!T) return;
if (!T->lchild && !T->rchild) {
leaves[(*idx)++] = T->data;
return;
}
CollectLeaves(T->lchild, leaves, idx);
CollectLeaves(T->rchild, leaves, idx);
}
// 删除所有叶子结点
void DeleteLeaves(BiTree *T) {
if (!*T) return;
if (!(*T)->lchild && !(*T)->rchild) {
free(*T);
*T = NULL;
return;
}
DeleteLeaves(&((*T)->lchild));
DeleteLeaves(&((*T)->rchild));
}
int main() {
BiTree T = NULL;
char leaves[20] = {0};
int idx = 0, level = 0;
char queryNode;
printf("请按先序序列输入二叉树中结点的值(每个结点一个字符,#表示空树):\n");
CreateBTree(&T);
printf("先序遍历序列为:");
PreOrderTraverse(T);
printf("\n中序遍历序列为:");
InOrderTraverse(T);
printf("\n后序遍历序列为:");
PostOrderTraverse(T);
printf("\n");
printf("叶子结点个数为:%d\n", CountLeaf(T));
printf("度为1的结点个数为:%d\n", CountDegree1(T));
printf("度为2的结点个数为:%d\n", CountDegree2(T));
printf("树的深度为:%d\n", TreeDepth(T));
printf("树的宽度为:%d\n", TreeWidth(T));
printf("\n请输入要查询的结点:\n");
while ((queryNode = getchar()) == ' ' || queryNode == '\n');
LocateNode(T, queryNode, 0, &level);
printf("结点%c在第%d层\n", queryNode, level);
printf("值最大的结点为:%c\n", MaxElem(T));
SwapChildren(T);
printf("交换每个结点的左右子树后先序遍历序列为:");
PreOrderTraverse(T);
printf("\n");
CollectLeaves(T, leaves, &idx);
DeleteLeaves(&T);
printf("删除的叶子结点为:");
for (int i = 0; i < idx; i++) printf("%c ", leaves[i]);
printf("\n");
return 0;
}