mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add binary search tree.
This commit is contained in:
@@ -14,7 +14,7 @@ export default class BinarySearchTree {
|
||||
}
|
||||
|
||||
remove(value) {
|
||||
const nodeToRemove = this.findNode(value);
|
||||
const nodeToRemove = this.root.find(value);
|
||||
|
||||
if (!nodeToRemove) {
|
||||
throw new Error('Item not found in the tree');
|
||||
|
||||
@@ -12,14 +12,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
if (this.left) {
|
||||
this.left.insert(value);
|
||||
} else {
|
||||
this.left = new BinarySearchTreeNode(value);
|
||||
this.setLeft(new BinarySearchTreeNode(value));
|
||||
}
|
||||
} else if (value > this.value) {
|
||||
// Insert to the right.
|
||||
if (this.right) {
|
||||
this.right.insert(value);
|
||||
} else {
|
||||
this.right = new BinarySearchTreeNode(value);
|
||||
this.setRight(new BinarySearchTreeNode(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user