Add possibility for tree nodes to have height.

This commit is contained in:
Oleksii Trekhleb
2018-04-04 09:11:26 +03:00
parent d98c52cba8
commit 857edbf3a8
2 changed files with 49 additions and 0 deletions

View File

@@ -6,6 +6,17 @@ export default class BinaryTreeNode {
this.value = value;
}
get height() {
if (!this.left && !this.left) {
return 0;
}
const leftHeight = this.left ? this.left.height : 0;
const rightHeight = this.right ? this.right.height : 0;
return Math.max(leftHeight, rightHeight) + 1;
}
setLeft(node) {
this.left = node;
this.left.parent = this;