@@ -10,7 +10,8 @@ class BinarySearchTree {
1010
1111 /**
1212 * Insert value on the BST.
13- * It the value is already in the tree, then It increase the multiplicity value
13+ * If the value is already in the tree, t
14+ * then it increase the multiplicity value
1415 * @param {any } value value to insert in the tree
1516 */
1617 add ( value ) {
@@ -58,50 +59,50 @@ class BinarySearchTree {
5859
5960 /**
6061 * Get the node with the max value of subtree: the right-most value.
61- * @param {TreeNode } root subtree's root
62+ * @param {TreeNode } node subtree's root
63+ * @returns {TreeNode } right-most node (max value)
6264 */
63- getMax ( root = this . root ) {
64- let current = root ;
65- while ( current && current . right ) {
66- current = current . right ;
65+ getRightmost ( node = this . root ) {
66+ if ( ! node || ! node . right ) {
67+ return node ;
6768 }
68- return current ;
69+ return this . getMax ( node . right ) ;
6970 }
7071
7172 /**
7273 * Get the node with the min value of subtree: the left-most value.
73- * @param {TreeNode } root subtree's root
74+ * @param {TreeNode } node subtree's root
75+ * @returns {TreeNode } left-most node (min value)
7476 */
75- getMin ( root = this . root ) {
76- let current = root ;
77- while ( current && current . left ) {
78- current = current . left ;
77+ getLeftmost ( node = this . root ) {
78+ if ( ! node || ! node . left ) {
79+ return node ;
7980 }
80- return current ;
81+ return this . getMin ( node . left ) ;
8182 }
8283
8384 /**
8485 * Remove a node from the tree
8586 * @returns {boolean } false if not found and true if it was deleted
8687 */
8788 remove ( value ) {
88- const nodeToRemove = this . find ( value ) ;
89+ const { found : nodeToRemove , parent } = this . findNodeAndParent ( value ) ;
90+
8991 if ( ! nodeToRemove ) return false ;
9092
9193 // Combine left and right children into one subtree without nodeToRemove
92- const nodeToRemoveChildren = this . combineLeftIntoRightSubtree ( nodeToRemove ) ;
94+ const removedNodeChildren = this . combineLeftIntoRightSubtree ( nodeToRemove ) ;
9395
9496 if ( nodeToRemove . meta . multiplicity && nodeToRemove . meta . multiplicity > 1 ) {
95- nodeToRemove . meta . multiplicity -= 1 ; // handle duplicated
97+ nodeToRemove . meta . multiplicity -= 1 ; // handles duplicated
9698 } else if ( nodeToRemove === this . root ) {
9799 // Replace (root) node to delete with the combined subtree.
98- this . root = nodeToRemoveChildren ;
100+ this . root = removedNodeChildren ;
99101 this . root . parent = null ; // clearing up old parent
100102 } else {
101103 const side = nodeToRemove . isParentLeftChild ? 'left' : 'right' ;
102- const { parent } = nodeToRemove ; // get parent
103104 // Replace node to delete with the combined subtree.
104- parent [ side ] = nodeToRemoveChildren ;
105+ parent [ side ] = removedNodeChildren ;
105106 }
106107
107108 this . size -= 1 ;
@@ -258,9 +259,9 @@ class BinarySearchTree {
258259// aliases
259260BinarySearchTree . prototype . insert = BinarySearchTree . prototype . add ;
260261BinarySearchTree . prototype . delete = BinarySearchTree . prototype . remove ;
261- BinarySearchTree . prototype . getLeftmost = BinarySearchTree . prototype . getMin ;
262+ BinarySearchTree . prototype . getMin = BinarySearchTree . prototype . getLeftmost ;
262263BinarySearchTree . prototype . minimum = BinarySearchTree . prototype . getMin ;
263- BinarySearchTree . prototype . getRightmost = BinarySearchTree . prototype . getMax ;
264+ BinarySearchTree . prototype . getMax = BinarySearchTree . prototype . getRightmost ;
264265BinarySearchTree . prototype . maximum = BinarySearchTree . prototype . getMax ;
265266
266267module . exports = BinarySearchTree ;
0 commit comments