Add binary search tree.

This commit is contained in:
Oleksii Trekhleb
2018-04-03 08:47:42 +03:00
parent 1c911aadf0
commit 9f8e763d69
4 changed files with 178 additions and 0 deletions

View File

@@ -18,6 +18,38 @@ export default class BinaryTreeNode {
return this;
}
removeChild(nodeToRemove) {
if (this.left && this.left === nodeToRemove) {
this.left = null;
return true;
}
if (this.right && this.right === nodeToRemove) {
this.right = null;
return true;
}
return false;
}
replaceChild(nodeToReplace, replacementNode) {
if (!nodeToReplace || !replacementNode) {
return false;
}
if (this.left && this.left === nodeToReplace) {
this.left = replacementNode;
return true;
}
if (this.right && this.right === nodeToReplace) {
this.right = replacementNode;
return true;
}
return false;
}
traverseInOrder() {
let traverse = [];