diff --git a/.eslintrc b/.eslintrc
index f04f4f6..f7496f8 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -3,7 +3,11 @@
max-len: ["error", { "code": 80, "ignoreComments": true }],
"comma-dangle": ["error", {
"functions": "ignore"
- }]
+ }],
+ no-underscore-dangle: [
+ "error",
+ { "allowAfterThis": true }
+ ]
},
"env": {
"mocha": true,
diff --git a/.npmignore b/.npmignore
index dd6bac0..279b026 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,5 +1,8 @@
.git*
-*.spec.js
.travis.yml
+.gitignore
+.eslintrc
Gruntfile.js
-coverage/
\ No newline at end of file
+coverage/
+node_modules/
+test/
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bfc86a1..e3db403 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+## [3.1.2] - 2020-04-10
+### Fixed
+- README
+- jsdoc
+
## [3.1.1] - 2020-04-01
### Fixed
- use the same balancing algorithm for insert and remove.
diff --git a/README.md b/README.md
index 42ebb3b..d6313c2 100644
--- a/README.md
+++ b/README.md
@@ -7,25 +7,18 @@
Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript.
-
-Binary Search Tree |
-
-
-
-
- |
-
-
-
-
-
-AVL Tree (Self Balancing Tree) |
-
-
-
-
- |
-
+
+ Binary Search Tree |
+
+
+ |
+
+
+ AVL Tree (Self Balancing Tree) |
+
+
+ |
+
# Table of Contents
@@ -33,7 +26,7 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
* [API](#api)
* [require](#require)
* [import](#import)
- * [Creating a Tree](#create-a-tree)
+ * [Construction](#create-a-tree)
* [.insert(key, value)](#insertkey-value)
* [.has(key)](#haskey)
* [.find(key)](#findkey)
@@ -46,6 +39,8 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
* [.traversePostOrder(cb)](#traversepostordercb)
* [.remove(key)](#removekey)
* [.clear()](#clear)
+ * [BinarySearchTreeNode](#binarysearchtreenode)
+ * [AvlTreeNode](#avltreenode)
* [Build](#build)
* [License](#license)
@@ -57,7 +52,7 @@ npm install --save @datastructures-js/binary-search-tree
## API
### require
-Both trees have the same interface except that AVL tree will maintain itself balance due to rotating nodes that becomes unbalanced on insertion and deletion. If your code requires a strictly balanced tree that always benefits from the **log(n)** runtime of insert & remove, you should use AVL.
+Both trees have the same interface except that AVL tree will maintain itself balanced due to rotating nodes that become unbalanced on insertion and deletion. If your code requires a strictly balanced tree that always benefits from the **log(n)** runtime of insert & remove, you should use AVL.
```js
const { BinarySearchTree, AvlTree } = require('@datastructures-js/binary-search-tree');
@@ -65,7 +60,7 @@ const { BinarySearchTree, AvlTree } = require('@datastructures-js/binary-search-
### import
```js
-import { BinarySearchTree } from '@datastructures-js/binary-search-tree';
+import { BinarySearchTree, AvlTree } from '@datastructures-js/binary-search-tree';
```
### Create a Tree
@@ -82,38 +77,30 @@ const bst = new AvlTree();
inserts a node with key/value into the tree. Inserting an node with existing key, would update the existing node's value with the new inserted one. AVL tree will rotate nodes properly if the tree becomes unbalanced with the insertion.
+
+ params |
+ name | type |
+ key | number or string |
+ value | object |
+
+
+
+
runtime |
- params |
- return |
O(log(n)) |
-
- key: {number} or {string}
-
- value: {object}
- |
-
- {BinarySearchTreeNode} for BinarySearchTree
-
- .getKey() {number|string} returns the node's key that is used to compare with other nodes.
- .setValue(value) change the value that is associated with a node.
- .getValue() {object} returns the value that is associated with a node.
- .getLeft() {BinarySearchTreeNode} returns node's left child node.
- .getRight() {BinarySearchTreeNode} returns node's right child node.
- .getParent() {BinarySearchTreeNode} returns node's parent node.
-
- {AvlTreeNode} for AvlTree. It extends the BinarySearchTreeNode and adds the following methods:
-
- .getHeight() {number} the height of the node in the tree. root is 1.
- .getLeftHeight() {number} the height of the left child. 0 if no left child.
- .getRightHeight() {number} the height of the right child. 0 if no right child.
- |
+#### Example
+
```js
bst.insert(50, 'v1');
bst.insert(80, 'v2');
@@ -127,23 +114,28 @@ bst.insert(20, 'v7');
### .has(key)
checks if a node exists by its key.
+
+ params |
+ name | type |
+ key | number or string |
+
+
+
+
runtime |
- params |
- return |
O(log(n)) |
-
- key: {number} or {string}
- |
-
- {boolean}
- |
+#### Example
+
```js
bst.has(50); // true
bst.has(100); // false
@@ -152,25 +144,29 @@ bst.has(100); // false
### .find(key)
finds a node in the tree by its key.
+
+ params |
+ name | type |
+ key | number or string |
+
+
+
+
runtime |
- params |
- return |
O(log(n)) |
-
- key: {number} or {string}
- |
-
- {BinarySearchTreeNode} for BinarySearchTree
-
- {AvlTreeNode} for AvlTree
- |
+#### Example
+
```js
const n60 = bst.find(60);
console.log(n60.getKey()); // 60
@@ -182,21 +178,23 @@ console.log(bst.find(100)); // null
### .min()
finds the node with min key in the tree.
+
+
runtime |
- return |
O(log(n)) |
-
- {BinarySearchTreeNode} for BinarySearchTree
-
- {AvlTreeNode} for AvlTree
- |
+#### Example
+
```js
const min = bst.min();
console.log(min.getKey()); // 20
@@ -206,21 +204,23 @@ console.log(min.getValue()); // v7
### .max()
finds the node with max key in the tree.
+
+
runtime |
- return |
O(log(n)) |
-
- {BinarySearchTreeNode} for BinarySearchTree
-
- {AvlTreeNode} for AvlTree
- |
+#### Example
+
```js
const max = bst.max();
console.log(max.getKey()); // 90
@@ -229,21 +229,23 @@ console.log(max.getValue()); // v4
### .root()
returns the root node of the tree.
+
+
runtime |
- return |
O(1) |
-
- {BinarySearchTreeNode} for BinarySearchTree
-
- {AvlTreeNode} for AvlTree
- |
+#### Example
+
```js
const root = bst.root();
console.log(root.getKey()); // 50
@@ -253,19 +255,22 @@ console.log(root.getValue()); // v1
### .count()
returns the count of nodes in the tree.
+
+
runtime |
- return |
O(1) |
-
- {number}
- |
+#### Example
+
```js
console.log(bst.count()); // 7
```
@@ -273,19 +278,23 @@ console.log(bst.count()); // 7
### .traverseInOrder(cb)
traverses the tree in order (left-node-right).
+
+ params |
+ name | type | description |
+ cb | function | called with each node |
+
+
runtime |
- param |
O(n) |
-
- cb: {function}
- |
+#### Example
+
```js
bst.traverseInOrder((node) => console.log(node.getKey()));
@@ -303,19 +312,23 @@ bst.traverseInOrder((node) => console.log(node.getKey()));
### .traversePreOrder(cb)
traverses the tree pre order (node-left-right).
+
+ params |
+ name | type | description |
+ cb | function | called with each node |
+
+
runtime |
- param |
O(n) |
-
- cb: {function}
- |
+#### Example
+
```js
bst.traversePreOrder((node) => console.log(node.getKey()));
@@ -333,19 +346,23 @@ bst.traversePreOrder((node) => console.log(node.getKey()));
### .traversePostOrder(cb)
traverses the tree post order (left-right-node).
+
+ params |
+ name | type | description |
+ cb | function | called with each node |
+
+
runtime |
- param |
O(n) |
-
- cb: {function}
- |
+#### Example
+
```js
bst.traversePostOrder((node) => console.log(node.getKey()));
@@ -363,23 +380,28 @@ bst.traversePostOrder((node) => console.log(node.getKey()));
### .remove(key)
removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced with the deletion.
+
+ params |
+ name | type |
+ key | number or string |
+
+
+
+
runtime |
- params |
- return |
O(log(n)) |
-
- key: {number} or {string}
- |
-
- {boolean}
- |
+#### Example
+
```js
bst.remove(20); // true
bst.remove(100); // false
@@ -398,12 +420,104 @@ clears the tree.
+#### Example
+
```js
bst.clear();
console.log(bst.count()); // 0
console.log(bst.root()); // null
```
+### BinarySearchTreeNode
+
+#### .getKey()
+returns the node's key that is used to compare with other.
+
+
+ return |
+ number or string |
+
+
+
+#### .setValue(value)
+change the value that is associated with a node.
+
+
+ params |
+ name | type |
+ value | object |
+
+
+#### .getValue()
+returns the value that is associated with a node.
+
+
+
+#### .getLeft()
+returns node's left child node.
+
+
+
+#### .getRight()
+returns node's right child node.
+
+
+
+#### .getParent()
+returns node's parent node.
+
+
+
+### AvlTreeNode
+extends BinarySearchTreeNode and add the following methods:
+
+#### .getHeight()
+the height of the node in the tree. root height is 1.
+
+
+
+#### .getLeftHeight()
+the height of the left child. 0 if no left child.
+
+
+
+#### .getRightHeight()
+the height of the right child. 0 if no right child.
+
+
+
+#### .calculateBalance()
+returns the node's balance by subtracting right height from left height.
+
+
+
## Build
```
grunt build
diff --git a/package.json b/package.json
index 36d50e4..135f105 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@datastructures-js/binary-search-tree",
- "version": "3.1.1",
+ "version": "3.1.2",
"description": "binary search tree & avl tree (self balancing tree) implementation in javascript",
"main": "index.js",
"scripts": {
diff --git a/src/avlTree.js b/src/avlTree.js
index 211af6f..f9782c3 100644
--- a/src/avlTree.js
+++ b/src/avlTree.js
@@ -35,8 +35,8 @@ class AvlTree extends BinarySearchTree {
node.rotateRightLeft();
}
}
- if (node === this.rootNode && (balance < -1 || balance > 1)) {
- this.rootNode = node.getParent();
+ if (node === this._root && (balance < -1 || balance > 1)) {
+ this._root = node.getParent();
}
}
@@ -50,11 +50,11 @@ class AvlTree extends BinarySearchTree {
* @param {object} vaue
* @return {AvlTreeNode} the inserted node
*/
- insert(key, value, node = this.rootNode) {
+ insert(key, value, node = this._root) {
if (node === null) {
- this.rootNode = new AvlTreeNode(key, value);
- this.nodesCount += 1;
- return this.rootNode;
+ this._root = new AvlTreeNode(key, value);
+ this._count += 1;
+ return this._root;
}
if (key < node.getKey() && node.getLeft() === null) {
@@ -62,7 +62,7 @@ class AvlTree extends BinarySearchTree {
node.setLeft(newNode);
newNode.setParent(node);
node.updateHeight();
- this.nodesCount += 1;
+ this._count += 1;
return newNode;
}
@@ -71,7 +71,7 @@ class AvlTree extends BinarySearchTree {
node.setRight(newNode);
newNode.setParent(node);
node.updateHeight();
- this.nodesCount += 1;
+ this._count += 1;
return newNode;
}
@@ -91,7 +91,7 @@ class AvlTree extends BinarySearchTree {
return newNode;
}
- remove(key, node = this.rootNode) {
+ remove(key, node = this._root) {
if (node === null) return false;
if (key < node.getKey()) {
@@ -108,7 +108,7 @@ class AvlTree extends BinarySearchTree {
if (node.getLeft() === null && node.getRight() === null) {
if (node.getParent() === null) {
- this.rootNode = null;
+ this._root = null;
} else if (key < node.getParent().getKey()) {
node.getParent().setLeft(null);
node.getParent().updateHeight();
@@ -116,13 +116,13 @@ class AvlTree extends BinarySearchTree {
node.getParent().setRight(null);
node.getParent().updateHeight();
}
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
if (node.getRight() === null) {
if (node.getParent() === null) {
- this.rootNode = node.getLeft();
+ this._root = node.getLeft();
} else if (key < node.getParent().getKey()) {
node.getParent().setLeft(node.getLeft());
node.getParent().updateHeight();
@@ -131,13 +131,13 @@ class AvlTree extends BinarySearchTree {
node.getParent().updateHeight();
}
node.getLeft().setParent(node.getParent());
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
if (node.getLeft() === null) {
if (node.getParent() === null) {
- this.rootNode = node.getRight();
+ this._root = node.getRight();
} else if (key < node.getParent().getKey()) {
node.getParent().setLeft(node.getRight());
node.getParent().updateHeight();
@@ -146,7 +146,7 @@ class AvlTree extends BinarySearchTree {
node.getParent().updateHeight();
}
node.getRight().setParent(node.getParent());
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
diff --git a/src/avlTreeNode.js b/src/avlTreeNode.js
index 59c5833..648dd6e 100644
--- a/src/avlTreeNode.js
+++ b/src/avlTreeNode.js
@@ -1,5 +1,5 @@
/**
- * datastructures-js/binary-search-tree
+ * @datastructures-js/binary-search-tree
* @copyright 2020 Eyas Ranjous
* @license MIT
*/
@@ -14,7 +14,7 @@ const BinarySearchTreeNode = require('./binarySearchTreeNode');
class AvlTreeNode extends BinarySearchTreeNode {
constructor(key, value) {
super(key, value);
- this.height = 1;
+ this._height = 1;
}
/**
@@ -22,7 +22,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* rotates left (counter-clockwise) and updates parent and children
*/
rotateLeft() {
- const right = this.getRight(); // this.right will be re-assigned
+ const right = this.getRight(); // this._right will be re-assigned
// set the node as a left child of its right child
if (right !== null) {
@@ -31,27 +31,27 @@ class AvlTreeNode extends BinarySearchTreeNode {
}
// rebase right child to node's right left child.
- this.right = right.getLeft();
+ this._right = right.getLeft();
right.setLeft(this);
- right.setParent(this.parent);
+ right.setParent(this._parent);
}
// rebase parent's child to node's right child
- if (this.parent !== null && right !== null) {
- if (this.parent.getKey() < right.getKey()) {
- this.parent.setRight(right);
+ if (this._parent !== null && right !== null) {
+ if (this._parent.getKey() < right.getKey()) {
+ this._parent.setRight(right);
} else {
- this.parent.setLeft(right);
+ this._parent.setLeft(right);
}
}
// rebase parent to node's right child
- this.parent = right;
+ this._parent = right;
this.updateHeight();
- if (this.parent !== null) {
- this.parent.updateHeight();
+ if (this._parent !== null) {
+ this._parent.updateHeight();
}
}
@@ -60,7 +60,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* rotates right (clockwise) and updates parent and children
*/
rotateRight() {
- const left = this.getLeft(); // this.left will be re-assigned
+ const left = this.getLeft(); // this._left will be re-assigned
// set the node as a right child of its left child
if (left !== null) {
@@ -69,27 +69,27 @@ class AvlTreeNode extends BinarySearchTreeNode {
}
// rebase right child to node's right left child.
- this.left = left.getRight();
+ this._left = left.getRight();
left.setRight(this);
- left.setParent(this.parent);
+ left.setParent(this._parent);
}
// rebase parent to node's left child
- if (this.parent !== null && left !== null) {
- if (this.parent.getKey() > left.getKey()) {
- this.parent.setLeft(left);
+ if (this._parent !== null && left !== null) {
+ if (this._parent.getKey() > left.getKey()) {
+ this._parent.setLeft(left);
} else {
- this.parent.setRight(left);
+ this._parent.setRight(left);
}
}
// rebase parent to node's right child
- this.parent = left;
+ this._parent = left;
this.updateHeight();
- if (this.parent !== null) {
- this.parent.updateHeight();
+ if (this._parent !== null) {
+ this._parent.updateHeight();
}
}
@@ -98,8 +98,8 @@ class AvlTreeNode extends BinarySearchTreeNode {
* rotates left child to left then itself to right
*/
rotateLeftRight() {
- if (this.left !== null) {
- this.left.rotateLeft();
+ if (this._left !== null) {
+ this._left.rotateLeft();
}
this.rotateRight();
}
@@ -109,8 +109,8 @@ class AvlTreeNode extends BinarySearchTreeNode {
* rotates right child to right then itself to left
*/
rotateRightLeft() {
- if (this.right !== null) {
- this.right.rotateRight();
+ if (this._right !== null) {
+ this._right.rotateRight();
}
this.rotateLeft();
}
@@ -120,7 +120,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* @return {number}
*/
getLeftHeight() {
- return this.left !== null ? this.left.getHeight() : 0;
+ return this._left !== null ? this._left.getHeight() : 0;
}
/**
@@ -128,7 +128,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* @return {number}
*/
getRightHeight() {
- return this.right !== null ? this.right.getHeight() : 0;
+ return this._right !== null ? this._right.getHeight() : 0;
}
/**
@@ -136,7 +136,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* updates the height of a node as the max height of its children
*/
updateHeight() {
- this.height = Math.max(this.getLeftHeight(), this.getRightHeight()) + 1;
+ this._height = Math.max(this.getLeftHeight(), this.getRightHeight()) + 1;
}
/**
@@ -144,7 +144,7 @@ class AvlTreeNode extends BinarySearchTreeNode {
* @return {number}
*/
getHeight() {
- return this.height;
+ return this._height;
}
/**
diff --git a/src/binarySearchTree.js b/src/binarySearchTree.js
index 7120dda..0acd9cc 100644
--- a/src/binarySearchTree.js
+++ b/src/binarySearchTree.js
@@ -1,5 +1,5 @@
/**
- * datastructures-js/binary-search-tree
+ * @datastructures-js/binary-search-tree
* @copyright 2020 Eyas Ranjous
* @license MIT
*/
@@ -11,29 +11,30 @@ const BinarySearchTreeNode = require('./binarySearchTreeNode');
*/
class BinarySearchTree {
constructor() {
- this.rootNode = null;
- this.nodesCount = 0;
+ this._root = null;
+ this._count = 0;
}
/**
* @public
* inserts a node with a key/value into the tree
* @param {number|string} key
- * @param {object} vaue
+ * @param {object} value
+ * @param {BinarySearchTreeNode} node
* @return {BinarySearchTreeNode}
*/
- insert(key, value, node = this.rootNode) {
+ insert(key, value, node = this._root) {
if (node === null) {
- this.rootNode = new BinarySearchTreeNode(key, value);
- this.nodesCount += 1;
- return this.rootNode;
+ this._root = new BinarySearchTreeNode(key, value);
+ this._count += 1;
+ return this._root;
}
if (key < node.getKey() && node.getLeft() === null) {
const newNode = new BinarySearchTreeNode(key, value);
node.setLeft(newNode);
newNode.setParent(node);
- this.nodesCount += 1;
+ this._count += 1;
return newNode;
}
@@ -41,7 +42,7 @@ class BinarySearchTree {
const newNode = new BinarySearchTreeNode(key, value);
node.setRight(newNode);
newNode.setParent(node);
- this.nodesCount += 1;
+ this._count += 1;
return newNode;
}
@@ -61,9 +62,10 @@ class BinarySearchTree {
* @public
* check if a value exists in the tree by its key
* @param {number|string} key
+ * @param {BinarySearchTreeNode} node
* @return {boolean}
*/
- has(key, node = this.rootNode) {
+ has(key, node = this._root) {
if (node === null) return false;
if (key === node.getKey()) return true;
@@ -77,9 +79,10 @@ class BinarySearchTree {
* @public
* finds the key's node in the tree
* @param {number|string} key
+ * @param {BinarySearchTreeNode} node
* @return {BinarySearchTreeNode}
*/
- find(key, node = this.rootNode) {
+ find(key, node = this._root) {
if (node === null) return null;
if (key === node.getKey()) return node;
@@ -92,9 +95,10 @@ class BinarySearchTree {
/**
* @public
* finds the node with max key (most right) in the tree
+ * @param {BinarySearchTreeNode} node
* @return {BinarySearchTreeNode}
*/
- max(node = this.rootNode) {
+ max(node = this._root) {
if (node === null) return null;
if (node.getRight() === null) return node;
@@ -105,9 +109,10 @@ class BinarySearchTree {
/**
* @public
* finds the node with min key (most left) in the tree
+ * @param {BinarySearchTreeNode} node
* @return {BinarySearchTreeNode}
*/
- min(node = this.rootNode) {
+ min(node = this._root) {
if (node === null) return null;
if (node.getLeft() === null) return node;
@@ -117,29 +122,30 @@ class BinarySearchTree {
/**
* @public
- * gets the tree root node
+ * returns the tree root node
* @return {BinarySearchTreeNode}
*/
root() {
- return this.rootNode;
+ return this._root;
}
/**
* @public
- * gets nodes count in the tree
+ * returns the nodes count in the tree
* @return {number}
*/
count() {
- return this.nodesCount;
+ return this._count;
}
/**
* @public
* remove a node by its key
* @param {number|string} key
+ * @param {BinarySearchTreeNode} node
* @return {boolean}
*/
- remove(key, node = this.rootNode) {
+ remove(key, node = this._root) {
if (node === null) return false;
if (key < node.getKey()) {
@@ -152,39 +158,39 @@ class BinarySearchTree {
if (node.getLeft() === null && node.getRight() === null) {
if (node.getParent() === null) {
- this.rootNode = null;
+ this._root = null;
} else if (node.getKey() < node.getParent().getKey()) {
node.getParent().setLeft(null);
} else {
node.getParent().setRight(null);
}
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
if (node.getRight() === null) {
if (node.getParent() === null) {
- this.rootNode = node.getLeft();
+ this._root = node.getLeft();
} else if (node.getKey() < node.getParent().getKey()) {
node.getParent().setLeft(node.getLeft());
} else {
node.getParent().setRight(node.getLeft());
}
node.getLeft().setParent(node.getParent());
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
if (node.getLeft() === null) {
if (node.getParent() === null) {
- this.rootNode = node.getRight();
+ this._root = node.getRight();
} else if (node.getKey() < node.getParent().getKey()) {
node.getParent().setLeft(node.getRight());
} else {
node.getParent().setRight(node.getRight());
}
node.getRight().setParent(node.getParent());
- this.nodesCount -= 1;
+ this._count -= 1;
return true;
}
@@ -197,8 +203,9 @@ class BinarySearchTree {
* @public
* traverse the tree in-order (left-node-right)
* @param {function} cb
+ * @param {BinarySearchTreeNode} node
*/
- traverseInOrder(cb, node = this.rootNode) {
+ traverseInOrder(cb, node = this._root) {
if (typeof cb !== 'function') {
throw new Error('.traverseInOrder(cb) expects a callback');
}
@@ -214,8 +221,9 @@ class BinarySearchTree {
* @public
* traverse the tree pre-order (node-left-right)
* @param {function} cb
+ * @param {BinarySearchTreeNode} node
*/
- traversePreOrder(cb, node = this.rootNode) {
+ traversePreOrder(cb, node = this._root) {
if (typeof cb !== 'function') {
throw new Error('.traversePreOrder(cb) expects a callback');
}
@@ -231,8 +239,9 @@ class BinarySearchTree {
* @public
* traverse the tree post-order (left-right-node)
* @param {function} cb
+ * @param {BinarySearchTreeNode} node
*/
- traversePostOrder(cb, node = this.rootNode) {
+ traversePostOrder(cb, node = this._root) {
if (typeof cb !== 'function') {
throw new Error('.traversePostOrder(cb) expects a callback');
}
@@ -249,8 +258,8 @@ class BinarySearchTree {
* clears the tree
*/
clear() {
- this.rootNode = null;
- this.nodesCount = 0;
+ this._root = null;
+ this._count = 0;
}
}
diff --git a/src/binarySearchTreeNode.js b/src/binarySearchTreeNode.js
index 42af547..94dd579 100644
--- a/src/binarySearchTreeNode.js
+++ b/src/binarySearchTreeNode.js
@@ -1,5 +1,5 @@
/**
- * datastructures-js/binary-search-tree
+ * @datastructures-js/binary-search-tree
* @copyright 2020 Eyas Ranjous
* @license MIT
*/
@@ -9,11 +9,11 @@
*/
class BinarySearchTreeNode {
constructor(key, value) {
- this.key = key;
- this.value = value;
- this.left = null;
- this.right = null;
- this.parent = null;
+ this._key = key;
+ this._value = value;
+ this._left = null;
+ this._right = null;
+ this._parent = null;
}
/**
@@ -21,7 +21,7 @@ class BinarySearchTreeNode {
* @param {number|string}
*/
setKey(key) {
- this.key = key;
+ this._key = key;
}
/**
@@ -29,7 +29,7 @@ class BinarySearchTreeNode {
* @return {number|string}
*/
getKey() {
- return this.key;
+ return this._key;
}
/**
@@ -37,7 +37,7 @@ class BinarySearchTreeNode {
* @param {object}
*/
setValue(value) {
- this.value = value;
+ this._value = value;
}
/**
@@ -45,7 +45,7 @@ class BinarySearchTreeNode {
* @return {object}
*/
getValue() {
- return this.value;
+ return this._value;
}
/**
@@ -53,7 +53,7 @@ class BinarySearchTreeNode {
* @param {BinarySearchTreeNode}
*/
setLeft(left) {
- this.left = left;
+ this._left = left;
}
/**
@@ -61,7 +61,7 @@ class BinarySearchTreeNode {
* @return {BinarySearchTreeNode}
*/
getLeft() {
- return this.left;
+ return this._left;
}
/**
@@ -69,7 +69,7 @@ class BinarySearchTreeNode {
* @param {BinarySearchTreeNode}
*/
setRight(right) {
- this.right = right;
+ this._right = right;
}
/**
@@ -77,7 +77,7 @@ class BinarySearchTreeNode {
* @return {BinarySearchTreeNode}
*/
getRight() {
- return this.right;
+ return this._right;
}
/**
@@ -85,7 +85,7 @@ class BinarySearchTreeNode {
* @param {BinarySearchTreeNode}
*/
setParent(parent) {
- this.parent = parent;
+ this._parent = parent;
}
/**
@@ -93,7 +93,7 @@ class BinarySearchTreeNode {
* @return {BinarySearchTreeNode}
*/
getParent() {
- return this.parent;
+ return this._parent;
}
}