Skip to content

Commit

Permalink
Merge pull request #16 from datastructures-js/development
Browse files Browse the repository at this point in the history
v3.0.1
  • Loading branch information
eyas-ranjous authored Mar 29, 2020
2 parents 4be470e + ecbe486 commit 7d85bfa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
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.0.1] - 2020-03-29
### Fixed
- return the updated node in `.insert`

## [3.0.0] - 2020-03-28
### Changed
- New release
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.0.0",
"version": "3.0.1",
"description": "binary search tree implementation in javascript",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions src/binarySearchTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ 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;
return newNode;
}

if (key > node.getKey() && node.getRight() === null) {
const newNode = new BinarySearchTreeNode(key, value);
node.setRight(newNode);
newNode.setParent(node);
this.nodesCount += 1;
Expand All @@ -47,7 +47,7 @@ class BinarySearchTree {

if (key === node.getKey()) {
node.setValue(value);
return newNode;
return node;
}

if (key < node.getKey()) {
Expand Down
20 changes: 12 additions & 8 deletions test/binarySearchTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 7d85bfa

Please sign in to comment.