@@ -10,7 +10,8 @@ class BinarySearchTree {
10
10
11
11
/**
12
12
* 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
14
15
* @param {any} value value to insert in the tree
15
16
*/
16
17
add(value) {
@@ -58,50 +59,50 @@ class BinarySearchTree {
58
59
59
60
/**
60
61
* 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)
62
64
*/
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;
67
68
}
68
- return current ;
69
+ return this.getMax(node.right) ;
69
70
}
70
71
71
72
/**
72
73
* 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)
74
76
*/
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;
79
80
}
80
- return current ;
81
+ return this.getMin(node.left) ;
81
82
}
82
83
83
84
/**
84
85
* Remove a node from the tree
85
86
* @returns {boolean} false if not found and true if it was deleted
86
87
*/
87
88
remove(value) {
88
- const nodeToRemove = this.find(value);
89
+ const { found: nodeToRemove, parent } = this.findNodeAndParent(value);
90
+
89
91
if (!nodeToRemove) return false;
90
92
91
93
// Combine left and right children into one subtree without nodeToRemove
92
- const nodeToRemoveChildren = this.combineLeftIntoRightSubtree(nodeToRemove);
94
+ const removedNodeChildren = this.combineLeftIntoRightSubtree(nodeToRemove);
93
95
94
96
if (nodeToRemove.meta.multiplicity && nodeToRemove.meta.multiplicity > 1) {
95
- nodeToRemove.meta.multiplicity -= 1; // handle duplicated
97
+ nodeToRemove.meta.multiplicity -= 1; // handles duplicated
96
98
} else if (nodeToRemove === this.root) {
97
99
// Replace (root) node to delete with the combined subtree.
98
- this.root = nodeToRemoveChildren ;
100
+ this.root = removedNodeChildren ;
99
101
this.root.parent = null; // clearing up old parent
100
102
} else {
101
103
const side = nodeToRemove.isParentLeftChild ? 'left' : 'right';
102
- const { parent } = nodeToRemove; // get parent
103
104
// Replace node to delete with the combined subtree.
104
- parent[side] = nodeToRemoveChildren ;
105
+ parent[side] = removedNodeChildren ;
105
106
}
106
107
107
108
this.size -= 1;
@@ -258,9 +259,9 @@ class BinarySearchTree {
258
259
// aliases
259
260
BinarySearchTree.prototype.insert = BinarySearchTree.prototype.add;
260
261
BinarySearchTree.prototype.delete = BinarySearchTree.prototype.remove;
261
- BinarySearchTree.prototype.getLeftmost = BinarySearchTree.prototype.getMin ;
262
+ BinarySearchTree.prototype.getMin = BinarySearchTree.prototype.getLeftmost ;
262
263
BinarySearchTree.prototype.minimum = BinarySearchTree.prototype.getMin;
263
- BinarySearchTree.prototype.getRightmost = BinarySearchTree.prototype.getMax ;
264
+ BinarySearchTree.prototype.getMax = BinarySearchTree.prototype.getRightmost ;
264
265
BinarySearchTree.prototype.maximum = BinarySearchTree.prototype.getMax;
265
266
266
267
module.exports = BinarySearchTree;
0 commit comments