Add binary search tree.

This commit is contained in:
Oleksii Trekhleb
2018-04-03 07:50:30 +03:00
parent b89e406444
commit 1c911aadf0
4 changed files with 22 additions and 4 deletions

View File

@@ -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');

View File

@@ -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));
}
}