@@ -91,29 +91,29 @@ class BinarySearchTree {
9191 * @returns {boolean } false if not found and true if it was deleted
9292 */
9393 remove ( value ) {
94- const found = this . find ( value ) ;
95- if ( ! found ) return false ;
94+ const nodeToRemove = this . find ( value ) ;
95+ if ( ! nodeToRemove ) return false ;
9696
97- // Combine left and right children into one subtree.
98- const newSubtree = this . combineLeftIntoRightSubtree ( found ) ;
97+ // Combine left and right children into one subtree without nodeToRemove
98+ const nodeToRemoveChildren = this . combineLeftIntoRightSubtree ( nodeToRemove ) ;
9999
100- if ( found === this . root ) {
100+ if ( nodeToRemove === this . root ) {
101101 // Replace (root) node to delete with the combined subtree.
102- this . root = newSubtree ;
102+ this . root = nodeToRemoveChildren ;
103103 this . root . parent = null ; // clearing up old parent
104104 } else {
105- const side = found . isParentLeftChild ? 'left' : 'right' ;
106- const { parent } = found ; // get parent
105+ const side = nodeToRemove . isParentLeftChild ? 'left' : 'right' ;
106+ const { parent } = nodeToRemove ; // get parent
107107 // Replace node to delete with the combined subtree.
108- parent [ side ] = newSubtree ;
108+ parent [ side ] = nodeToRemoveChildren ;
109109 }
110110
111111 this . size -= 1 ;
112112 return true ;
113113 }
114114
115115 /**
116- * Combine left children/subtree into right children/ subtree.
116+ * Combine left into right children into one subtree without given parent node .
117117 *
118118 * @example combineLeftIntoRightSubtree(30)
119119 *
0 commit comments