Add binary search tree.

This commit is contained in:
Oleksii Trekhleb
2018-04-02 17:50:56 +03:00
parent 00e40a0eca
commit d6be33842c
8 changed files with 184 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
export default class BinaryTreeNode {
constructor(value = null, left = null, right = null) {
this.left = left;
this.right = right;
this.value = value;
}
addLeft(node) {
this.left = node;
return this;
}
addRight(node) {
this.right = node;
return this;
}
hasLeft() {
return !!this.left;
}
hasRight() {
return !!this.right;
}
traverseInOrder() {
return Array.prototype.concat(
this.left ? this.left.traverseInOrder() : [null],
[this.value],
this.right ? this.right.traverseInOrder() : [null],
);
}
toString() {
return this.traverseInOrder().filter(value => !!value).toString();
}
}