@@ -148,7 +148,6 @@ void put(int value) {
148148 */
149149 public
150150 boolean remove (int value ) {
151- // temp is the node to be deleted
152151 Node temp = find (value );
153152
154153 // If the value doesn't exist
@@ -160,23 +159,21 @@ boolean remove(int value) {
160159 if (temp .right == null && temp .left == null ) {
161160 if (temp == root ) {
162161 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 ) {
167163 temp .parent .right = null ;
168164 } else {
169165 temp .parent .left = null ;
170166 }
171- size --; // Decrement size on removal
172- return true ;
173- } // Two children
167+ }
168+ // Two children
174169 else if (temp .left != null && temp .right != null ) {
175170 Node successor = findSuccessor (temp );
176171
177172 // The left tree of temp is made the left tree of the successor
178173 successor .left = temp .left ;
179- successor .left .parent = successor ;
174+ if (temp .left != null ) {
175+ temp .left .parent = successor ;
176+ }
180177
181178 // If the successor has a right child, the child's grandparent is its new
182179 // parent
@@ -188,66 +185,44 @@ else if (temp.left != null && temp.right != null) {
188185 successor .parent .left = null ;
189186 }
190187 successor .right = temp .right ;
191- successor .right .parent = successor ;
188+ if (temp .right != null ) {
189+ temp .right .parent = successor ;
190+ }
192191 }
193192
194193 if (temp == root ) {
195194 successor .parent = null ;
196195 root = successor ;
197- } // If you're not deleting the root
198- else {
196+ } else {
199197 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
203198 if (temp .parent .data < temp .data ) {
204199 temp .parent .right = successor ;
205200 } else {
206201 temp .parent .left = successor ;
207202 }
208203 }
209- size --; // Decrement size on removal
210- return true ;
211- } // One child
204+ }
205+ // One child
212206 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
237212 }
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 ;
244217 } else {
245- temp .parent .right = temp . left ;
218+ temp .parent .left = child ;
246219 }
247220 }
248- size --; // Decrement size on removal
249- return true ;
250221 }
222+
223+ // Decrement size regardless of the case of removal
224+ size --;
225+ return true ;
251226 }
252227
253228 /**
0 commit comments