-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.2.cpp
More file actions
207 lines (198 loc) · 3.83 KB
/
8.2.cpp
File metadata and controls
207 lines (198 loc) · 3.83 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
#include <stdio.h>
#include <stdlib.h>
typedef int KeyType;
typedef struct node
{
KeyType key;
struct node *lchild;
struct node *rchild;
} BSTNode;
// 二叉排序树插入
int InsertBST(BSTNode *&p, KeyType k)
{
if (p == NULL)
{
p = (BSTNode *)malloc(sizeof(BSTNode));
p->key = k;
p->lchild = p->rchild = NULL;
return 1;
}
else if (k == p->key)
{
return 0; // 已存在
}
else if (k < p->key)
{
return InsertBST(p->lchild, k);
}
else
{
return InsertBST(p->rchild, k);
}
}
// 构造二叉排序树
BSTNode *CreatBST(KeyType A[], int n)
{
BSTNode *bt = NULL;
for (int i = 0; i < n; i++)
{
InsertBST(bt, A[i]);
}
return bt;
}
// 中序遍历
void InOrder(BSTNode *bt)
{
if (bt != NULL)
{
InOrder(bt->lchild);
printf("%d ", bt->key);
InOrder(bt->rchild);
}
}
// 查找关键字的父结点
BSTNode *FindParent(BSTNode *bt, KeyType k)
{
if (bt == NULL || bt->key == k)
return NULL;
BSTNode *parent = NULL;
while (bt != NULL)
{
if (bt->key == k)
return parent;
parent = bt;
if (k < bt->key)
bt = bt->lchild;
else
bt = bt->rchild;
}
return NULL;
}
// 二叉排序树删除
void Delete(BSTNode *&p)
{
BSTNode *q, *r;
if (p->rchild == NULL)
{
q = p;
p = p->lchild;
free(q);
}
else if (p->lchild == NULL)
{
q = p;
p = p->rchild;
free(q);
}
else
{
q = p;
r = p->lchild;
while (r->rchild != NULL)
{
q = r;
r = r->rchild;
}
p->key = r->key;
if (q == p)
q->lchild = r->lchild;
else
q->rchild = r->lchild;
free(r);
}
}
void PrintBST(BSTNode *bt)
{
if (bt == NULL)
return;
printf("%d", bt->key);
if (bt->lchild != NULL || bt->rchild != NULL)
{
printf("(");
PrintBST(bt->lchild);
if (bt->rchild != NULL)
{
printf(",");
PrintBST(bt->rchild);
}
printf(")");
}
}
int DeleteBST(BSTNode *&bt, KeyType k)
{
if (bt == NULL)
return 0;
BSTNode *p = bt;
BSTNode *parent = FindParent(bt, k);
while (p != NULL && p->key != k)
{
if (k < p->key)
p = p->lchild;
else
p = p->rchild;
}
if (p == NULL)
return 0;
if (parent == NULL)
{
Delete(bt);
}
else if (parent->lchild == p)
{
Delete(parent->lchild);
}
else
{
Delete(parent->rchild);
}
return 1;
}
// 层序遍历
void LevelOrder(BSTNode *bt)
{
if (bt == NULL)
return;
BSTNode *queue[100];
int front = 0, rear = 0;
queue[rear++] = bt;
printf("二叉排序树结构:");
while (front < rear)
{
BSTNode *p = queue[front++];
printf("%d ", p->key);
if (p->lchild != NULL)
queue[rear++] = p->lchild;
if (p->rchild != NULL)
queue[rear++] = p->rchild;
}
printf("\n");
}
int main() {
KeyType A[] = {12, 7, 17, 11, 16, 2, 13, 9, 21, 4};
int n = sizeof(A) / sizeof(KeyType);
BSTNode *bt = CreatBST(A, n);
printf("二叉排序树为:");
PrintBST(bt);
printf("\n");
//中序序列
printf("中序序列为:");
InOrder(bt);
printf("\n");
// 删除11
printf("删除11\n");
DeleteBST(bt, 11);
printf("二叉排序树为:");
PrintBST(bt);
printf("\n中序序列为:");
InOrder(bt);
printf("\n");
// 删除17
printf("删除17\n");
DeleteBST(bt, 17);
printf("二叉排序树为:");
PrintBST(bt);
printf("\n中序序列:");
InOrder(bt);
printf("\n");
return 0;
}