From 2205e8684926542e22dc966ff530268dfa3c988c Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 19:58:55 -0500 Subject: [PATCH 01/18] update --- .eslintrc | 6 +++++- .npmignore | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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/ From 1128822f52bf34932286a4b4d02b1168044f8f9a Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 19:59:32 -0500 Subject: [PATCH 02/18] jsdoc --- src/avlTree.js | 30 ++++++++-------- src/avlTreeNode.js | 60 ++++++++++++++++---------------- src/binarySearchTree.js | 69 +++++++++++++++++++++---------------- src/binarySearchTreeNode.js | 32 ++++++++--------- 4 files changed, 100 insertions(+), 91 deletions(-) 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; } } From cc7eb2d5aa074c1603fb088ef5e37561958d0baa Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:02:17 -0500 Subject: [PATCH 03/18] update --- README.md | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 42ebb3b..d19657e 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
-Binary Search Tree -
- - - - - - - - + + + + + + + +
AVL Tree (Self Balancing Tree)
-AVL Tree -
Binary Search Tree + Binary Search Tree +
AVL Tree (Self Balancing Tree) + AVL Tree +
# Table of Contents @@ -65,7 +58,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 From e50a5b61387efce2d9214d00a5063f14040f222a Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:04:07 -0500 Subject: [PATCH 04/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d19657e..e6d03b2 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript Binary Search Tree - Binary Search Tree + Binary Search Tree AVL Tree (Self Balancing Tree) - AVL Tree + AVL Tree From b49142169ee2b836161a18aface7200063b72f2f Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:04:37 -0500 Subject: [PATCH 05/18] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e6d03b2..3927241 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript - From 8f7972a9879d8737fd53ee0a41f9c1cd69760127 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:05:04 -0500 Subject: [PATCH 06/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3927241..3d20dde 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search Tree + Binary Search Tree
- + - + From 4d703617382996bd5d53f7d809dc4293086b53d8 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:05:31 -0500 Subject: [PATCH 07/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3d20dde..ccfe23a 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From 5a2642b3a9e42f315003daf5ea18ca65fa149b35 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:05:53 -0500 Subject: [PATCH 08/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ccfe23a..dccbfd7 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From bd7456074061749ab64ba257cd1113938a828660 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:06:08 -0500 Subject: [PATCH 09/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dccbfd7..ba6fe2a 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From 2b76e46e0cc9e3e1585aa0496b3d9d63425b304e Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:06:28 -0500 Subject: [PATCH 10/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba6fe2a..9eab825 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From 53fe17b9a8bf27a9e6506c652de85ad10aa38d9a Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:06:44 -0500 Subject: [PATCH 11/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9eab825..7049843 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From c9980fe991d00ba4d1358e8ce7412f53a15cbaa8 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:07:11 -0500 Subject: [PATCH 12/18] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7049843..0d98882 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
- + - + From d58f141ff0ac8a2ffdc0aa9def1507c917dad487 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 20:07:35 -0500 Subject: [PATCH 13/18] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d98882..d97f9f9 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript - + From 77ab33d737fa21c1f9cc4d25306dcaa756c49064 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 23:05:52 -0500 Subject: [PATCH 14/18] update --- README.md | 109 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index d97f9f9..77a29f7 100644 --- a/README.md +++ b/README.md @@ -26,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) @@ -39,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) @@ -50,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'); @@ -75,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. +
Binary Search TreeBinary Search Tree Binary Search Tree
AVL Tree (Self Balancing Tree)AVL Tree (Self Balancing Tree) AVL Tree
AVL Tree (Self Balancing Tree)AVL Tree
(Self Balancing Tree)
AVL Tree
+ + + + +
params
nametype
keynumber or string
valueobject
+ + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ - - - -
runtimeparamsreturn
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'); @@ -120,23 +114,28 @@ bst.insert(20, 'v7'); ### .has(key) checks if a node exists by its key. + + + + +
params
nametype
keynumber or string
+ + + + +
return
boolean
+ - - - -
runtimeparamsreturn
O(log(n)) - key: {number} or {string} - - {boolean} -
+#### Example + ```js bst.has(50); // true bst.has(100); // false @@ -145,25 +144,29 @@ bst.has(100); // false ### .find(key) finds a node in the tree by its key. + + + + +
params
nametype
keynumber or string
+ + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ - - - -
runtimeparamsreturn
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 @@ -397,6 +400,26 @@ console.log(bst.count()); // 0 console.log(bst.root()); // null ``` +### BinarySearchTreeNode + {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.
+### AvlTreeNode +extends BinarySearchTreeNode and add the following methods: + +#### .getHeight() + ## Build ``` grunt build From bd670a53114a08b69341acc959e27aea86627135 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Thu, 9 Apr 2020 23:10:41 -0500 Subject: [PATCH 15/18] update --- README.md | 89 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 77a29f7..314da40 100644 --- a/README.md +++ b/README.md @@ -178,21 +178,23 @@ console.log(bst.find(100)); // null ### .min() finds the node with min key in the tree. + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ - -
runtimereturn
O(log(n)) - {BinarySearchTreeNode} for BinarySearchTree -

- {AvlTreeNode} for AvlTree -
+#### Example + ```js const min = bst.min(); console.log(min.getKey()); // 20 @@ -202,21 +204,23 @@ console.log(min.getValue()); // v7 ### .max() finds the node with max key in the tree. + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ - -
runtimereturn
O(log(n)) - {BinarySearchTreeNode} for BinarySearchTree -

- {AvlTreeNode} for AvlTree -
+#### Example + ```js const max = bst.max(); console.log(max.getKey()); // 90 @@ -225,21 +229,23 @@ console.log(max.getValue()); // v4 ### .root() returns the root node of the tree. + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ - -
runtimereturn
O(1) - {BinarySearchTreeNode} for BinarySearchTree -

- {AvlTreeNode} for AvlTree -
+#### Example + ```js const root = bst.root(); console.log(root.getKey()); // 50 @@ -249,19 +255,22 @@ console.log(root.getValue()); // v1 ### .count() returns the count of nodes in the tree. + + + +
return
number
+ - -
runtimereturn
O(1) - {number} -
+#### Example + ```js console.log(bst.count()); // 7 ``` @@ -269,19 +278,23 @@ console.log(bst.count()); // 7 ### .traverseInOrder(cb) traverses the tree in order (left-node-right). + + + + +
params
nametypedescription
cbfunctioncalled with each node
+ - -
runtimeparam
O(n) - cb: {function} -
+#### Example + ```js bst.traverseInOrder((node) => console.log(node.getKey())); @@ -299,19 +312,23 @@ bst.traverseInOrder((node) => console.log(node.getKey())); ### .traversePreOrder(cb) traverses the tree pre order (node-left-right). + + + + +
params
nametypedescription
cbfunctioncalled with each node
+ - -
runtimeparam
O(n) - cb: {function} -
+#### Example + ```js bst.traversePreOrder((node) => console.log(node.getKey())); @@ -329,19 +346,23 @@ bst.traversePreOrder((node) => console.log(node.getKey())); ### .traversePostOrder(cb) traverses the tree post order (left-right-node). + + + + +
params
nametypedescription
cbfunctioncalled with each node
+ - -
runtimeparam
O(n) - cb: {function} -
+#### Example + ```js bst.traversePostOrder((node) => console.log(node.getKey())); From 0fffb09b9c7c988c130310cd432bcc52926d0011 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Fri, 10 Apr 2020 00:45:49 -0500 Subject: [PATCH 16/18] update --- README.md | 123 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 100 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 314da40..d6313c2 100644 --- a/README.md +++ b/README.md @@ -380,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
nametype
keynumber or string
+ + + + +
return
boolean
+ - - - -
runtimeparamsreturn
O(log(n)) - key: {number} or {string} - - {boolean} -
+#### Example + ```js bst.remove(20); // true bst.remove(100); // false @@ -415,6 +420,8 @@ clears the tree. +#### Example + ```js bst.clear(); console.log(bst.count()); // 0 @@ -422,24 +429,94 @@ console.log(bst.root()); // null ``` ### BinarySearchTreeNode - {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.
+ +#### .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
nametype
valueobject
+ +#### .getValue() +returns the value that is associated with a node. + + + + +
return
object
+ +#### .getLeft() +returns node's left child node. + + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ +#### .getRight() +returns node's right child node. + + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ +#### .getParent() +returns node's parent node. + + + + + +
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
+ ### AvlTreeNode -extends BinarySearchTreeNode and add the following methods: +extends BinarySearchTreeNode and add the following methods: #### .getHeight() +the height of the node in the tree. root height is 1. + + + + +
return
number
+ +#### .getLeftHeight() +the height of the left child. 0 if no left child. + + + + +
return
number
+ +#### .getRightHeight() +the height of the right child. 0 if no right child. + + + + +
return
number
+ +#### .calculateBalance() +returns the node's balance by subtracting right height from left height. + + + + +
return
number
## Build ``` From 2022598261b91425390482d2346f690c57439a89 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Fri, 10 Apr 2020 00:47:18 -0500 Subject: [PATCH 17/18] v3.1.2 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) 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. From 125839762eb7fd71073f6e6af40ee97ccb671654 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Fri, 10 Apr 2020 00:47:22 -0500 Subject: [PATCH 18/18] v3.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": {