-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-d_tree.c
244 lines (216 loc) · 6.38 KB
/
2-d_tree.c
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
/* 2-d_tree.c -- 2-d树实现文件 */
#include <stdio.h>
#include <stdlib.h>
#include "2-d_tree.h"
/* 局部函数声明 */
static Node * Make_Node (const Item * const pkey1, const Item * const pkey2) ;
void Copy_Key1_To_Node (const Item * const pkey1, Node * const pnode) ;
void Copy_Key2_To_Node (const Item * const pkey2, Node * const pnode) ;
int Left_Is_Greater_Than_Right (const Item * const pleft, const Item * const pright) ;
int Left_Is_Lesser_Than_Right (const Item * const pleft, const Item * const pright) ;
int Left_Is_Equal_To_Right (const Item * const pleft, const Item * const prifht) ;
int Left_Is_Equal_Or_Greater_Than_Right (const Item * const pleft, const Item * const pright) ;
int Left_Is_Equal_Or_Lesser_Than_Right (const Item * const pleft, const Item * const pright) ;
void Set_The_Layer (const Node * const pnode, Node * const new_node) ;
/* 接口函数定义 */
void InitializeTree (Tree * const ptree)
{
*ptree = NULL ;
}
int TreeIsEmpty (const Tree * const ptree)
{
return NULL == *ptree ;
}
Node * FindMin (const Tree * const ptree)
{
if (NULL == *ptree)
return NULL ;
else if (NULL == (*ptree) -> left)
return *ptree ;
else
return FindMin (&(*ptree) -> left) ;
}
Node * FindMax (const Tree * const ptree)
{
Node * scan = *ptree ;
if (scan != NULL)
while (scan -> right != NULL)
scan = scan -> right ;
return scan ;
}
int Insert (Tree * const ptree, const Item * const pkey1, const Item * const pkey2)
{
Node * new_node, * parent = NULL, * scan = *ptree ;
new_node = Make_Node (pkey1, pkey2) ;
if (NULL == new_node)
return 0 ;
if (TreeIsEmpty (ptree))
{
new_node -> layer = ODD ;
*ptree = new_node ;
return 1 ;
}
while (scan != NULL)
{
parent = scan ;
if (ODD == scan -> layer)
{
if (Left_Is_Greater_Than_Right (&new_node -> key1, &scan -> key1))
scan = scan -> right ;
else if (Left_Is_Lesser_Than_Right (&new_node -> key1, &scan -> key1))
scan = scan -> left ;
else
{
free (scan) ;
return 0 ;
}
}
else if (EVEN == scan -> layer)
{
if (Left_Is_Greater_Than_Right (&new_node -> key2, &scan -> key2))
scan = scan -> right ;
else if (Left_Is_Lesser_Than_Right (&new_node -> key2, &scan -> key2))
scan = scan -> left ;
else
{
free (scan) ;
return 0 ;
}
}
}
Set_The_Layer (parent, new_node) ;
/* 考虑到树在很丰满的情况下只用最多3次的条件检测就可以决定新结点的位置, 没有采用花花的操作.虽然代码略显臃肿, 但可读性可效率都不差 */
if (ODD == parent -> layer)
{
if (Left_Is_Greater_Than_Right (&new_node -> key1, &parent -> key1))
parent -> right = new_node ;
else
parent -> left = new_node ;
}
else if (EVEN == parent -> layer)
{
if (Left_Is_Greater_Than_Right (&new_node -> key2, &parent -> key2))
parent -> right = new_node ;
else
parent -> left = new_node ;
}
return 1 ;
}
void InorderSeekTraversal (const Tree * const ptree, const Item low1, const Item low2, const Item high1, const Item high2, void (* pfun) (const Item * const pkey1, const Item * const pejy2))
{
if (*ptree != NULL)
{
InorderSeekTraversal (&(*ptree) -> left, low1, low2, high1, high2, pfun) ;
if (Left_Is_Equal_Or_Greater_Than_Right (&(*ptree) -> key1, &low1) && Left_Is_Equal_Or_Lesser_Than_Right (&(*ptree) -> key1, &high1) &&
Left_Is_Equal_Or_Greater_Than_Right (&(*ptree) -> key2, &low2) && Left_Is_Equal_Or_Lesser_Than_Right (&(*ptree) -> key2, &high2))
(* pfun) (&(*ptree) -> key1, &(*ptree) -> key2) ;
InorderSeekTraversal (&(*ptree) -> right, low1, low2, high1, high2, pfun) ;
}
}
Tree Delete (Tree * const ptree, const Item * const pkey1, const Item * const pkey2)
{
char index = FALSE ;
Node * temp ;
if (NULL == *ptree)
return NULL ;
if (ODD == (*ptree) -> layer)
{
if (Left_Is_Greater_Than_Right (pkey1, &(*ptree) -> key1))
(*ptree) -> right = Delete (&(*ptree) -> right, pkey1, pkey2) ;
else if (Left_Is_Lesser_Than_Right (pkey1, &(*ptree) -> key1))
(*ptree) -> left = Delete (&(*ptree) -> left, pkey1, pkey2) ;
else
{
if (Left_Is_Equal_To_Right (pkey2, &(*ptree) -> key2))
index = TRUE ;
}
}
else if (EVEN == (*ptree) -> layer)
{
if (Left_Is_Greater_Than_Right (pkey2, &(*ptree) -> key2))
(*ptree) -> right = Delete (&(*ptree) -> right, pkey1, pkey2) ;
else if (Left_Is_Lesser_Than_Right (pkey2, &(*ptree) -> key2))
(*ptree) -> left = Delete (&(*ptree) -> left, pkey1, pkey2) ;
else
{
if (Left_Is_Equal_To_Right (pkey1, &(*ptree) -> key1))
index = TRUE ;
}
}
if (index)
{
if ((*ptree) -> left != NULL && (*ptree) -> right != NULL)
{
temp = FindMin (&(*ptree) -> right) ;
Copy_Key1_To_Node (&temp -> key1, *ptree) ;
Copy_Key2_To_Node (&temp -> key2, *ptree) ;
(*ptree) -> right = Delete (&(*ptree) -> right, &temp -> key1, &temp -> key2) ;
}
else
{
temp = *ptree ;
if (NULL == (*ptree) -> left && NULL == (*ptree) -> right)
*ptree = (*ptree) -> right ;
else if (NULL == (*ptree) -> left)
{
Set_The_Layer (*ptree, (*ptree) -> right) ;
*ptree = (*ptree) -> right ;
}
else if (NULL == (*ptree) -> right)
{
Set_The_Layer (*ptree, (*ptree) -> left) ;
*ptree = (*ptree) -> left ;
}
free (temp) ;
}
}
return *ptree ;
}
/* 局部函数定义 */
static Node * Make_Node (const Item * const pkey1, const Item * const pkey2)
{
Node * new_node ;
new_node = (Node *) malloc (sizeof (Node)) ;
if (NULL == new_node)
return NULL ;
Copy_Key1_To_Node (pkey1, new_node) ;
Copy_Key2_To_Node (pkey2, new_node) ;
new_node -> left = NULL ;
new_node -> right = NULL ;
return new_node ;
}
void Copy_Key1_To_Node (const Item * const pkey1, Node * const pnode)
{
pnode -> key1 = *pkey1 ;
}
void Copy_Key2_To_Node (const Item * const pkey2, Node * const pnode)
{
pnode -> key2 = *pkey2 ;
}
int Left_Is_Greater_Than_Right (const Item * const pleft, const Item * const pright)
{
return *pleft > *pright ;
}
int Left_Is_Lesser_Than_Right (const Item * const pleft, const Item * const pright)
{
return *pleft < *pright ;
}
int Left_Is_Equal_To_Right (const Item * const pleft, const Item * const pright)
{
return *pleft == *pright ;
}
int Left_Is_Equal_Or_Greater_Than_Right (const Item * const pleft, const Item * const pright)
{
return *pleft >= *pright ;
}
int Left_Is_Equal_Or_Lesser_Than_Right (const Item * const pleft, const Item * const pright)
{
return *pleft <= *pright ;
}
void Set_The_Layer (const Node * const pnode, Node * const new_node)
{
if (ODD == pnode -> layer)
new_node -> layer = EVEN ;
else
new_node -> layer = ODD ;
}