Skip to content

Commit 26ff4c7

Browse files
authored
Merge pull request #4 from datastructures-js/development
v1.0.3
2 parents 24bad66 + 305d86e commit 26ff4c7

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ const binarySearchTree = () => {
139139
/**
140140
* inserts a node by a given value into the tree
141141
* @param {(string|number)} value
142-
* @param {object} node
143142
*/
144143
const insert = (value) => {
145144
const insertFn = (currentNode) => {
@@ -192,7 +191,9 @@ const binarySearchTree = () => {
192191
nodesCount -= 1;
193192
} else if (right === null) {
194193
// remove a node with a left child
195-
if (currentNode.getValue() > parent.getValue()) {
194+
if (parent === null) {
195+
rootNode = left;
196+
} else if (currentNode.getValue() > parent.getValue()) {
196197
parent.setRight(left);
197198
} else {
198199
parent.setLeft(left);
@@ -201,7 +202,9 @@ const binarySearchTree = () => {
201202
nodesCount -= 1;
202203
} else if (left === null) {
203204
// remove a node with a right child
204-
if (currentNode.getValue() > parent.getValue()) {
205+
if (parent === null) {
206+
rootNode = right;
207+
} else if (currentNode.getValue() > parent.getValue()) {
205208
parent.setRight(right);
206209
} else {
207210
parent.setLeft(right);

index.spec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('binarySearchTree tests', () => {
2929
expect(bst.max().getValue(90))));
3030

3131
describe('.root()', () =>
32-
it('should get the node with min value', () =>
32+
it('should get the root node', () =>
3333
expect(bst.root().getValue(50))));
3434

3535
describe('.find(value)', () =>
@@ -79,7 +79,6 @@ describe('binarySearchTree tests', () => {
7979
expect(bst.count()).to.equal(5);
8080
});
8181

82-
8382
it('should remove a node with a left child only', () => {
8483
bst.insert(30);
8584
bst.remove(40);
@@ -96,6 +95,30 @@ describe('binarySearchTree tests', () => {
9695
expect(bst.find(90).getLeft().getValue()).to.equal(60);
9796
expect(bst.count()).to.equal(4);
9897
});
98+
99+
it('should remove root node with right child', () => {
100+
bst.insert(100);
101+
bst.remove(60);
102+
bst.remove(90);
103+
bst.remove(30);
104+
bst.remove(50);
105+
expect(bst.root().getValue()).to.equal(100);
106+
});
107+
108+
it('should remove root node with left child', () => {
109+
bst.insert(20);
110+
bst.insert(30);
111+
bst.insert(25);
112+
bst.remove(30);
113+
bst.remove(25);
114+
bst.remove(100);
115+
expect(bst.root().getValue()).to.equal(20);
116+
});
117+
118+
it('should remove root node', () => {
119+
bst.remove(20);
120+
expect(bst.root()).to.equal(null);
121+
});
99122
});
100123

101124
describe('.clear()', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/binary-search-tree",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "binary search tree implementation in javascript",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)