Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.1.4] - 2020-04-12
### Fixed
- jsdoc

## [3.1.3] - 2020-04-10
### Fixed
- README
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ returns node's parent node.
</table>

### AvlTreeNode
extends <a href="#binarysearchtreenode">BinarySearchTreeNode</a> and add the following methods:
extends <a href="#binarysearchtreenode">BinarySearchTreeNode</a> and adds the following methods:

#### .getHeight()
the height of the node in the tree. root height is 1.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/binary-search-tree",
"version": "3.1.3",
"version": "3.1.4",
"description": "binary search tree & avl tree (self balancing tree) implementation in javascript",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 16 additions & 9 deletions src/avlTree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* datastructures-js/binary-search-tree
* @datastructures-js/binary-search-tree
* @copyright 2020 Eyas Ranjous <eyas.ranjous@gmail.com>
* @license MIT
*/
Expand All @@ -17,7 +17,7 @@ class AvlTree extends BinarySearchTree {
* applies the proper rotation on nodes after an insert or remove
* @param {AvlTreeNode} node
*/
balanceNode(node) {
_balanceNode(node) {
if (!node) return;

node.updateHeight();
Expand All @@ -42,12 +42,11 @@ class AvlTree extends BinarySearchTree {

/**
* @public
*
* inserts a node with a key/value into tree
* and maintains the tree balanced by applying the necessary rotations
*
* @param {number|string} key
* @param {object} vaue
* @param {object} value
* @param {AvlTreeNode} node
* @return {AvlTreeNode} the inserted node
*/
insert(key, value, node = this._root) {
Expand Down Expand Up @@ -82,27 +81,35 @@ class AvlTree extends BinarySearchTree {

if (key < node.getKey()) {
const newNode = this.insert(key, value, node.getLeft());
this.balanceNode(node); // back-tracking
this._balanceNode(node); // back-tracking
return newNode;
}

const newNode = this.insert(key, value, node.getRight());
this.balanceNode(node); // back-tracking
this._balanceNode(node); // back-tracking
return newNode;
}

/**
* @public
* remove a node by its key
* and maintains the tree balanced by applying the necessary rotations
* @param {number|string} key
* @param {AvlTreeNode} node
* @return {boolean}
*/
remove(key, node = this._root) {
if (node === null) return false;

if (key < node.getKey()) {
const removed = this.remove(key, node.getLeft());
this.balanceNode(node);
this._balanceNode(node);
return removed;
}

if (key > node.getKey()) {
const removed = this.remove(key, node.getRight());
this.balanceNode(node);
this._balanceNode(node);
return removed;
}

Expand Down
1 change: 0 additions & 1 deletion src/avlTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const BinarySearchTreeNode = require('./binarySearchTreeNode');
* @class AvlTreeNode
* @extends BinarySearchTreeNode
*/

class AvlTreeNode extends BinarySearchTreeNode {
constructor(key, value) {
super(key, value);
Expand Down