delete.c
5.9 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
245
246
247
248
249
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "trbase.h"
#include "tr/tree.h"
TR_Tree TR_inOrderSuccessor(TR_Tree);
void TR_treeRotateLeft(TR_Tree *, TR_Tree);
void TR_treeRotateRight(TR_Tree *, TR_Tree);
void *
TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp)
{
TR_Tree node = *this;
TR_Tree del_node;
void * data;
/*
* first search for it and if its found return the data
* and we are done...
*/
while (NULL != node) {
int comparison = comp(node->data, search);
if (0 < comparison) {
node = TR_TREE_LEFT(node);
continue;
}
if (0 > comparison) {
node = TR_TREE_RIGHT(node);
continue;
}
if (0 == comparison) {
break;
}
}
/*
* nothing was found...return NULL to indicate this.
*/
if (NULL == node) {
return NULL;
}
/*
* we found an element, store its data pointer as we are
* up to delete it.
*/
data = node->data;
/*
* now remove the element.
*/
/*
* if we have two children replace data with the one from
* out inOrderSuccessor and remove the inOrderSuccessor.
*/
if (NULL != TR_TREE_LEFT(node) && NULL != TR_TREE_RIGHT(node)) {
TR_Tree successor = TR_inOrderSuccessor(node);
node->data = successor->data;
node = successor;
}
{
TR_Tree child = TR_TREE_CHILD(node);
/*
* if we still have one child replace ourself with it.
*/
TR_TREE_REPLACE_NODE(this, node, child);
/*
* and finally delete the node...and prepare ourselfs
* for rebalancing.
*/
if (rbBlack == node->color) {
if (NULL != child && rbRed == child->color) {
child->color = rbBlack;
TR_delete(node);
return data;
} else {
del_node = node;
if (NULL != child) {
node = child;
} else {
node->color = rbBlack;
node->left = NULL;
node->right = NULL;
}
}
} else {
TR_delete(node);
return data;
}
}
/*
* now comes rebalancing...note that if we came to this point
* the node is still not deleted.
* This is because I am not sure if it is needed during the
* rebalancing process...(this does not make much sense, but
* to be honest I don't know now.)
*/
while(1) {
// case 1
if (NULL == TR_TREE_PARENT(node)) {
break;
}
// case 2
if (NULL != TR_TREE_SIBLING(node)
&& rbRed == TR_TREE_SIBLING(node)->color) {
TR_TREE_PARENT(node)->color = rbRed;
TR_TREE_SIBLING(node)->color = rbBlack;
if (NULL != TR_TREE_PARENT(node)->right &&
node != TR_TREE_PARENT(node)->right) {
//TREE_ROTATE_LEFT(this, TREE_PARENT(node));
TR_treeRotateLeft(this, TR_TREE_PARENT(node));
} else {
//TREE_ROTATE_RIGHT(this, TREE_PARENT(node));
TR_treeRotateRight(this, TR_TREE_PARENT(node));
}
}
// case 3 / 4
if (NULL == TR_TREE_SIBLING(node)
|| (rbBlack == TR_TREE_SIBLING(node)->color
&& (NULL == TR_TREE_SIBLING(node)->left
|| rbBlack == TR_TREE_SIBLING(node)->left->color)
&& (NULL == TR_TREE_SIBLING(node)->right
|| rbBlack == TR_TREE_SIBLING(node)->right->color))) {
if (NULL != TR_TREE_SIBLING(node)) {
TR_TREE_SIBLING(node)->color = rbRed;
}
/*
* this is the point where during the balancing our tree
* node can be finally deleted.
*/
if (rbBlack == TR_TREE_PARENT(node)->color) {
// case 3
TR_Tree parent = node->parent;
node = parent;
continue;
} else {
// case 4
TR_TREE_PARENT(node)->color = rbBlack;
break;
}
}
// case 5
if (NULL != TR_TREE_SIBLING(node)
&& rbBlack == TR_TREE_SIBLING(node)->color) {
if (node == TR_TREE_PARENT(node)->left
&& (NULL == TR_TREE_SIBLING(node)->right
|| rbBlack == TR_TREE_SIBLING(node)->right->color)
&& (NULL != TR_TREE_SIBLING(node)->left
&& rbRed == TR_TREE_SIBLING(node)->left->color)) {
TR_TREE_SIBLING(node)->color = rbRed;
TR_TREE_SIBLING(node)->left->color = rbBlack;
//TREE_ROTATE_RIGHT(this, TREE_SIBLING(node));
TR_treeRotateRight(this, TR_TREE_SIBLING(node));
} else if (node == TR_TREE_PARENT(node)->right
&& (NULL == TR_TREE_SIBLING(node)->left
|| rbBlack == TR_TREE_SIBLING(node)->left->color)
&& (NULL != TR_TREE_SIBLING(node)->right
&& rbRed == TR_TREE_SIBLING(node)->right->color)) {
TR_TREE_SIBLING(node)->color = rbRed;
TR_TREE_SIBLING(node)->right->color = rbBlack;
//TREE_ROTATE_LEFT(this, TREE_SIBLING(node));
TR_treeRotateLeft(this, TR_TREE_SIBLING(node));
}
}
// case 6
if (NULL != TR_TREE_SIBLING(node)) {
TR_TREE_SIBLING(node)->color = TR_TREE_PARENT(node)->color;
}
if (NULL != node && NULL != TR_TREE_PARENT(node)) {
TR_TREE_PARENT(node)->color = rbBlack;
if (NULL != TR_TREE_PARENT(node)->right
&& node != TR_TREE_PARENT(node)->right) {
if (NULL != TR_TREE_SIBLING(node)->right) {
TR_TREE_SIBLING(node)->right->color = rbBlack;
}
//TREE_ROTATE_LEFT(this, TREE_PARENT(node));
TR_treeRotateLeft(this, TR_TREE_PARENT(node));
} else {
if (NULL != TR_TREE_SIBLING(node)->left) {
TR_TREE_SIBLING(node)->left->color = rbBlack;
}
//TREE_ROTATE_RIGHT(this, TREE_PARENT(node));
TR_treeRotateRight(this, TR_TREE_PARENT(node));
}
}
break;
}
TR_delete(del_node);
/*
* not sure if deleting here is correct.
*/
return data;
}
// vim: set ts=4 sw=4: