From d19149de8e1f0df8fb48feaccc3489f5c8922c9e Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 5 Apr 2018 08:29:13 +0300 Subject: [PATCH] Fix binary tree node. --- src/data-structures/tree/BinaryTreeNode.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index 6b0c5d2e..d95d3ce3 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -6,15 +6,24 @@ export default class BinaryTreeNode { this.value = value; } - get height() { - if (!this.left && !this.right) { + get leftHeight() { + if (!this.left) { return 0; } - const leftHeight = this.left ? this.left.height : 0; - const rightHeight = this.right ? this.right.height : 0; + return this.left.height + 1; + } - return Math.max(leftHeight, rightHeight) + 1; + get rightHeight() { + if (!this.right) { + return 0; + } + + return this.right.height + 1; + } + + get height() { + return Math.max(this.leftHeight, this.rightHeight); } setLeft(node) {