Skip to content

Commit cf5637e

Browse files
committed
--fix: eslint & increase code coverage near 100%
1 parent 9840f5e commit cf5637e

File tree

8 files changed

+142
-29
lines changed

8 files changed

+142
-29
lines changed

src/_DataStructures_/Trees/BinarySearchTree/BinarySearchTree.test.js

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Is Empty', () => {
5+
const bst = new BinarySearchTree(6);
6+
bst.add(4);
7+
bst.add(9);
8+
bst.add(2);
9+
bst.add(5);
10+
bst.add(8);
11+
bst.add(12);
12+
it('should return False', () => {
13+
expect(bst.isEmpty()).toEqual(false);
14+
});
15+
16+
it('should return True', () => {
17+
bst.remove(6);
18+
bst.remove(4);
19+
bst.remove(9);
20+
bst.remove(2);
21+
bst.remove(5);
22+
bst.remove(8);
23+
bst.remove(12);
24+
25+
expect(bst.isEmpty()).toEqual(true);
26+
});
27+
});
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Find maximum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
bst.add(4);
7+
bst.add(9);
8+
bst.add(2);
9+
bst.add(5);
10+
bst.add(8);
11+
bst.add(12);
12+
it('should return 12', () => {
13+
expect(bst.getMaximum()).toEqual(12);
14+
});
15+
16+
it('should return 20', () => {
17+
bst.add(20);
18+
expect(bst.getMaximum()).toEqual(20);
19+
});
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Find minimum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
bst.add(4);
7+
bst.add(9);
8+
bst.add(2);
9+
bst.add(5);
10+
bst.add(8);
11+
bst.add(12);
12+
it('should return 4', () => {
13+
expect(bst.getMinimum()).toEqual(2);
14+
});
15+
16+
it('should return 1', () => {
17+
bst.add(1);
18+
expect(bst.getMinimum()).toEqual(1);
19+
});
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const BST = require('.');
2+
3+
describe('Data Structure : Binary Search Tree', () => {
4+
it('Should be class', () => {
5+
expect(typeof BST.prototype.constructor).toEqual('function');
6+
});
7+
8+
describe('Binary Search Tree API', () => {
9+
let bst = null;
10+
11+
beforeEach(() => {
12+
bst = new BST(5);
13+
});
14+
15+
it('Should delete() an element from Binary Search Tree', () => {
16+
bst.add(4);
17+
bst.add(9);
18+
bst.add(2);
19+
bst.delete(bst.root, 4);
20+
expect(bst.traverseInorder()).toEqual([2, 5, 9]);
21+
bst.delete(bst.root, 2);
22+
expect(bst.traverseInorder()).toEqual([5, 9]);
23+
});
24+
25+
it('Should return NULL if root is empty', () => {
26+
const bst2 = new BST(6);
27+
bst2.remove(6);
28+
bst2.remove(9);
29+
expect(bst2.root).toEqual(null);
30+
});
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Binary Search Tree', () => {
4+
describe('Find maximum value in BST', () => {
5+
const bst = new BinarySearchTree(6);
6+
7+
bst.add(4);
8+
bst.add(9);
9+
bst.add(2);
10+
bst.add(5);
11+
bst.add(8);
12+
bst.add(12);
13+
it('search for 8', () => {
14+
expect(bst.searchFor(8)).toEqual(true);
15+
});
16+
17+
it('search for 100', () => {
18+
expect(bst.searchFor(100)).toEqual(false);
19+
});
20+
});
21+
});

src/_DataStructures_/Trees/BinarySearchTree/bst-deletion.test.js src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/height-of-bst.test.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const BinarySearchTree = require('./index');
2-
const heightOfBST = require('./height-of-bst/index');
1+
const BinarySearchTree = require('../index');
2+
const heightOfBST = require('./index');
33

44
describe('Binary search tree traversals', () => {
55
let bst;
@@ -48,4 +48,20 @@ describe('Binary search tree traversals', () => {
4848
expect(heightOfBST(bst.root)).toEqual(2);
4949
});
5050
});
51+
52+
describe('Check bst was created as expected', () => {
53+
const bst2 = new BinarySearchTree(10);
54+
bst2.add(11);
55+
bst2.add(20);
56+
bst2.add(9);
57+
bst2.add(8);
58+
bst2.add(7);
59+
bst2.add(6);
60+
bst2.add(5);
61+
bst2.add(4);
62+
63+
it('Height should be 7', () => {
64+
expect(heightOfBST(bst2.root)).toEqual(7);
65+
});
66+
});
5167
});

src/_DataStructures_/Trees/BinarySearchTree/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
const Node = require('./Node');
23

34
class BinarySearchTree {
@@ -23,7 +24,6 @@ class BinarySearchTree {
2324
root.rightChild = this.insert(root.rightChild, value);
2425
return root;
2526
}
26-
return root;
2727
}
2828

2929
preorder(root) {
@@ -80,7 +80,6 @@ class BinarySearchTree {
8080
if (value > root.value) {
8181
return this.search(root.rightChild, value);
8282
}
83-
return false;
8483
}
8584

8685
delete(root, value) {

0 commit comments

Comments
 (0)