@@ -17,9 +17,9 @@ class BinarySearchTree {
1717 const newNode = new TreeNode ( value ) ;
1818
1919 if ( this . root ) {
20- const { node , parent } = this . findNodeAndParent ( value ) ;
21- if ( node ) { // duplicated: value already exist on the tree
22- node . meta . multiplicity = ( node . meta . multiplicity || 1 ) + 1 ;
20+ const { found , parent } = this . findNodeAndParent ( value ) ;
21+ if ( found ) { // duplicated: value already exist on the tree
22+ found . meta . multiplicity = ( found . meta . multiplicity || 1 ) + 1 ;
2323 } else if ( value < parent . value ) {
2424 parent . left = newNode ;
2525 } else {
@@ -38,7 +38,7 @@ class BinarySearchTree {
3838 * @param {any } value value to find
3939 */
4040 find ( value ) {
41- return this . findNodeAndParent ( value ) . node ;
41+ return this . findNodeAndParent ( value ) . found ;
4242 }
4343
4444 /**
@@ -59,7 +59,7 @@ class BinarySearchTree {
5959 node = value >= node . value ? node . right : node . left ;
6060 }
6161
62- return { node, parent } ;
62+ return { found : node , parent } ;
6363 }
6464
6565 /**
@@ -94,38 +94,52 @@ class BinarySearchTree {
9494 const found = this . find ( value ) ;
9595 if ( ! found ) return false ;
9696
97- const { parent } = found ;
97+ // Combine left and right children into one subtree.
98+ const newSubtree = this . combineLeftIntoRightSubtree ( found ) ;
9899
99100 if ( found === this . root ) {
100- // removing root
101- this . root = found . right || found . left ; // replace root
102- this . root . parent = null ;
103- // if the root had any left subtree,
104- // place it at the end of the new root subtree.
105- if ( found . left ) {
106- const newRootLeftmost = this . getMin ( this . root . left ) ;
107- newRootLeftmost . left = found . left ;
108- }
109- } else if ( parent . left === found ) {
110- // removing node on the left
111- parent . left = found . right || found . left ;
112-
113- if ( found . left ) {
114- parent . left . left = found . left ;
115- }
116- } else if ( parent . right === found ) {
117- // removing node on the right
118- parent . right = found . right || found . left ;
119-
120- if ( found . left ) {
121- parent . right . left = found . left ;
122- }
101+ // Replace (root) node to delete with the combined subtree.
102+ this . root = newSubtree ;
103+ this . root . parent = null ; // clearing up old parent
104+ } else {
105+ const side = found . isParentLeftChild ? 'left' : 'right' ;
106+ const { parent } = found ; // get parent
107+ // Replace node to delete with the combined subtree.
108+ parent [ side ] = newSubtree ;
123109 }
124110
125111 this . size -= 1 ;
126112 return true ;
127113 }
128114
115+ /**
116+ * Combine left children/subtree into right children/subtree.
117+ *
118+ * @example combineLeftIntoRightSubtree(30)
119+ *
120+ * 30* 40
121+ * / \ / \
122+ * 10 40 combined 35 50
123+ * \ / \ ----------> /
124+ * 15 35 50 10
125+ * \
126+ * 15
127+ *
128+ * It takes node 30 left subtree (10 and 15) and put it in the
129+ * leftmost node of the right subtree (40, 35, 50).
130+ *
131+ * @param {TreeNode } node
132+ * @returns {TreeNode } combined subtree
133+ */
134+ combineLeftIntoRightSubtree ( node ) {
135+ if ( node . right ) {
136+ const leftmost = this . getLeftmost ( node . right ) ;
137+ leftmost . left = node . left ;
138+ return node . right ;
139+ }
140+ return node . left ;
141+ }
142+
129143 /**
130144 * Breath-first search for a tree (always starting from the root element).
131145 *
@@ -245,4 +259,12 @@ class BinarySearchTree {
245259 }
246260}
247261
262+ // aliases
263+ BinarySearchTree . prototype . insert = BinarySearchTree . prototype . add ;
264+ BinarySearchTree . prototype . delete = BinarySearchTree . prototype . remove ;
265+ BinarySearchTree . prototype . getLeftmost = BinarySearchTree . prototype . getMin ;
266+ BinarySearchTree . prototype . minimum = BinarySearchTree . prototype . getMin ;
267+ BinarySearchTree . prototype . getRightmost = BinarySearchTree . prototype . getMax ;
268+ BinarySearchTree . prototype . maximum = BinarySearchTree . prototype . getMax ;
269+
248270module . exports = BinarySearchTree ;
0 commit comments