@@ -148,7 +148,6 @@ void put(int value) {
148
148
*/
149
149
public
150
150
boolean remove (int value ) {
151
- // temp is the node to be deleted
152
151
Node temp = find (value );
153
152
154
153
// If the value doesn't exist
@@ -160,23 +159,21 @@ boolean remove(int value) {
160
159
if (temp .right == null && temp .left == null ) {
161
160
if (temp == root ) {
162
161
root = null ;
163
- } // This if/else assigns the new node to be either the left or right
164
- // child of the
165
- // parent
166
- else if (temp .parent .data < temp .data ) {
162
+ } else if (temp .parent .data < temp .data ) {
167
163
temp .parent .right = null ;
168
164
} else {
169
165
temp .parent .left = null ;
170
166
}
171
- size --; // Decrement size on removal
172
- return true ;
173
- } // Two children
167
+ }
168
+ // Two children
174
169
else if (temp .left != null && temp .right != null ) {
175
170
Node successor = findSuccessor (temp );
176
171
177
172
// The left tree of temp is made the left tree of the successor
178
173
successor .left = temp .left ;
179
- successor .left .parent = successor ;
174
+ if (temp .left != null ) {
175
+ temp .left .parent = successor ;
176
+ }
180
177
181
178
// If the successor has a right child, the child's grandparent is its new
182
179
// parent
@@ -188,66 +185,44 @@ else if (temp.left != null && temp.right != null) {
188
185
successor .parent .left = null ;
189
186
}
190
187
successor .right = temp .right ;
191
- successor .right .parent = successor ;
188
+ if (temp .right != null ) {
189
+ temp .right .parent = successor ;
190
+ }
192
191
}
193
192
194
193
if (temp == root ) {
195
194
successor .parent = null ;
196
195
root = successor ;
197
- } // If you're not deleting the root
198
- else {
196
+ } else {
199
197
successor .parent = temp .parent ;
200
-
201
- // This if/else assigns the new node to be either the left or right
202
- // child of the parent
203
198
if (temp .parent .data < temp .data ) {
204
199
temp .parent .right = successor ;
205
200
} else {
206
201
temp .parent .left = successor ;
207
202
}
208
203
}
209
- size --; // Decrement size on removal
210
- return true ;
211
- } // One child
204
+ }
205
+ // One child
212
206
else {
213
- // If it has a right child
214
- if (temp .right != null ) {
215
- if (temp == root ) {
216
- root = temp .right ;
217
- root .parent = null ; // Update parent reference
218
- size --; // Decrement size on removal
219
- return true ;
220
- }
221
-
222
- temp .right .parent = temp .parent ;
223
-
224
- // Assigns temp to left or right child
225
- if (temp .data < temp .parent .data ) {
226
- temp .parent .left = temp .right ;
227
- } else {
228
- temp .parent .right = temp .right ;
229
- }
230
- } // If it has a left child
231
- else {
232
- if (temp == root ) {
233
- root = temp .left ;
234
- root .parent = null ; // Update parent reference
235
- size --; // Decrement size on removal
236
- return true ;
207
+ Node child = (temp .left != null ) ? temp .left : temp .right ;
208
+ if (temp == root ) {
209
+ root = child ;
210
+ if (child != null ) {
211
+ child .parent = null ; // Update parent reference
237
212
}
238
-
239
- temp .left .parent = temp .parent ;
240
-
241
- // Assigns temp to left or right side
242
- if (temp .data < temp .parent .data ) {
243
- temp .parent .left = temp .left ;
213
+ } else {
214
+ child .parent = temp .parent ;
215
+ if (temp .parent .data < temp .data ) {
216
+ temp .parent .right = child ;
244
217
} else {
245
- temp .parent .right = temp . left ;
218
+ temp .parent .left = child ;
246
219
}
247
220
}
248
- size --; // Decrement size on removal
249
- return true ;
250
221
}
222
+
223
+ // Decrement size regardless of the case of removal
224
+ size --;
225
+ return true ;
251
226
}
252
227
253
228
/**
0 commit comments