From 770528cc7dbb8b3f16a4b9183b709d048fa98c44 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Sun, 7 Jan 2024 21:45:53 -0800 Subject: [PATCH] update --- .eslintrc | 3 ++- src/avlTree.js | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4b4c3a3..20b4b65 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,7 +7,8 @@ "no-underscore-dangle": [ "error", { "allowAfterThis": true } - ] + ], + "class-methods-use-this": "off" }, "env": { "mocha": true, diff --git a/src/avlTree.js b/src/avlTree.js index 30fc56c..5533ba6 100644 --- a/src/avlTree.js +++ b/src/avlTree.js @@ -7,17 +7,6 @@ const { BinarySearchTree } = require('./binarySearchTree'); const { AvlTreeNode } = require('./avlTreeNode'); -/** - * Get the node's height - * @private - * @param {AvlTreeNode} node - * @return {number} - */ -function getNodeHeight(node) { - if (!(node instanceof AvlTreeNode)) return 0; - return node.getHeight(); -} - /** * @class AvlTree * @extends BinarySearchTree @@ -31,6 +20,17 @@ class AvlTree extends BinarySearchTree { super(compare, options); } + /** + * Get the node's height + * @private + * @param {AvlTreeNode} node + * @return {number} + */ + _getNodeHeight(node) { + if (!(node instanceof AvlTreeNode)) return 0; + return node.getHeight(); + } + /** * Applies the proper rotation on a node * @private @@ -42,17 +42,17 @@ class AvlTree extends BinarySearchTree { node.updateHeight(); const balance = node.getBalance(); if (balance > 1) { - const LL = node.getLeft().getLeft(); - const LR = node.getLeft().getRight(); - if (getNodeHeight(LL) >= getNodeHeight(LR)) { + const leftLeft = node.getLeft().getLeft(); + const leftRight = node.getLeft().getRight(); + if (this._getNodeHeight(leftLeft) >= this._getNodeHeight(leftRight)) { node.rotateRight(); } else if (node.getLeft().hasRight()) { node.rotateLeftRight(); } } else if (balance < -1) { - const RR = node.getRight().getRight(); - const RL = node.getRight().getLeft(); - if (getNodeHeight(RR) >= getNodeHeight(RL)) { + const rightRight = node.getRight().getRight(); + const rightLeft = node.getRight().getLeft(); + if (this._getNodeHeight(rightRight) >= this._getNodeHeight(rightLeft)) { node.rotateLeft(); } else if (node.getRight().hasLeft()) { node.rotateRightLeft();