From 0ed7b09e54c0adab012eae7893057e35c7d64ccf Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Sun, 29 Mar 2020 12:58:33 -0600 Subject: [PATCH 1/3] return the right updated node --- src/binarySearchTree.js | 10 +++++----- test/binarySearchTree.test.js | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/binarySearchTree.js b/src/binarySearchTree.js index 045d3b5..7120dda 100644 --- a/src/binarySearchTree.js +++ b/src/binarySearchTree.js @@ -23,15 +23,14 @@ class BinarySearchTree { * @return {BinarySearchTreeNode} */ insert(key, value, node = this.rootNode) { - const newNode = new BinarySearchTreeNode(key, value); - if (node === null) { - this.rootNode = newNode; + this.rootNode = new BinarySearchTreeNode(key, value); this.nodesCount += 1; - return newNode; + return this.rootNode; } if (key < node.getKey() && node.getLeft() === null) { + const newNode = new BinarySearchTreeNode(key, value); node.setLeft(newNode); newNode.setParent(node); this.nodesCount += 1; @@ -39,6 +38,7 @@ class BinarySearchTree { } if (key > node.getKey() && node.getRight() === null) { + const newNode = new BinarySearchTreeNode(key, value); node.setRight(newNode); newNode.setParent(node); this.nodesCount += 1; @@ -47,7 +47,7 @@ class BinarySearchTree { if (key === node.getKey()) { node.setValue(value); - return newNode; + return node; } if (key < node.getKey()) { diff --git a/test/binarySearchTree.test.js b/test/binarySearchTree.test.js index ae5c42d..ce6cc3c 100644 --- a/test/binarySearchTree.test.js +++ b/test/binarySearchTree.test.js @@ -7,14 +7,18 @@ describe('binarySearchTree tests', () => { describe('.insert(key, value)', () => { it('should insert nodes to the tree', () => { - bst.insert(50, 'n1'); - bst.insert(80, 'n2'); - bst.insert(30, 'n3'); - bst.insert(90, 'n4'); - bst.insert(60, 'n5'); - bst.insert(40, 'n6'); - bst.insert(20, 'n20'); - bst.insert(20, 'n7'); // updates value of existing node + expect(bst.insert(50, 'n1')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(80, 'n2')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(30, 'n3')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(90, 'n4')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(60, 'n5')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(40, 'n6')).to.be.instanceof(BinarySearchTreeNode); + expect(bst.insert(20, 'n20')).to.be.instanceof(BinarySearchTreeNode); + + // updates value of existing node + const updated = bst.insert(20, 'n7'); + expect(updated).to.be.instanceof(BinarySearchTreeNode); + expect(updated.getParent().getKey()).to.equal(30); }); }); From c6a6597e660dd6211f449923fc382b64702c87ae Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Sun, 29 Mar 2020 12:59:10 -0600 Subject: [PATCH 2/3] v3.0.1 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a8f5c0..d2bcda3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.0.1] - 2020-03-29 +### Fixed +- return the updated node in `.insert` + ## [3.0.0] - 2020-03-28 ### Changed - New release From ecbe4861026d407b15c46c1f50fcaba7da9a1025 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Sun, 29 Mar 2020 12:59:18 -0600 Subject: [PATCH 3/3] v3.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ab9938..1e8e7a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@datastructures-js/binary-search-tree", - "version": "3.0.0", + "version": "3.0.1", "description": "binary search tree implementation in javascript", "main": "index.js", "scripts": {