Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
eyas-ranjous committed Jan 8, 2024
1 parent 0455a62 commit 770528c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"no-underscore-dangle": [
"error",
{ "allowAfterThis": true }
]
],
"class-methods-use-this": "off"
},
"env": {
"mocha": true,
Expand Down
34 changes: 17 additions & 17 deletions src/avlTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 770528c

Please sign in to comment.