Skip to content

Commit 96ba2d3

Browse files
committed
--fix : unit test title & use loops to create BST
1 parent 69c973a commit 96ba2d3

File tree

10 files changed

+52
-88
lines changed

10 files changed

+52
-88
lines changed

src/_DataStructures_/DoublyLinkedList/doublyLinkedList.test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
const DLL = require('.');
22

33
describe('Doubly Linked List', () => {
4+
it('Should be class', () => {
5+
expect(typeof DLL.prototype.constructor).toEqual('function');
6+
});
7+
48
const doublyLinkedList = new DLL();
59

6-
it('create DLL', () => {
10+
it('It should create a DLL', () => {
711
expect(doublyLinkedList.head.next).toEqual(doublyLinkedList.tail);
812
expect(doublyLinkedList.tail.previous).toEqual(doublyLinkedList.head);
913
expect(doublyLinkedList.length()).toEqual(0);
1014
});
1115

12-
it('addAtBeginning', () => {
16+
it('It should add at beginning (addAtBeginning)', () => {
1317
doublyLinkedList.addAtBeginning(1);
1418
doublyLinkedList.addAtBeginning(2);
1519
doublyLinkedList.addAtBeginning(3);
1620
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1]);
1721
});
1822

19-
it('addAtEnd', () => {
23+
it('It should add at end (addAtEnd)', () => {
2024
doublyLinkedList.addAtEnd(1);
2125
doublyLinkedList.addAtEnd(2);
2226
doublyLinkedList.addAtEnd(3);
2327
expect(doublyLinkedList.traverse()).toEqual([3, 2, 1, 1, 2, 3]);
2428
});
2529

26-
it('removeAtBeginning', () => {
30+
it('It should remove at beginning (removeAtBeginning)', () => {
31+
doublyLinkedList.removeAtBeginning();
2732
doublyLinkedList.removeAtBeginning();
2833
doublyLinkedList.removeAtBeginning();
29-
doublyLinkedList.removeAtBeginning();
3034
expect(doublyLinkedList.traverse()).toEqual([1, 2, 3]);
3135
});
3236

33-
it('removeAtEnd', () => {
37+
it('It should remove at end (removeAtEnd)', () => {
3438
doublyLinkedList.removeAtEnd();
3539
doublyLinkedList.removeAtEnd();
3640
doublyLinkedList.removeAtEnd();

src/_DataStructures_/Queue/Queue.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Queue = require('.');
22

33
describe('Data Structure : Queue', () => {
4-
it('Should be class', () => {
4+
it('Queue should be class', () => {
55
expect(typeof Queue.prototype.constructor).toEqual('function');
66
});
77

@@ -56,7 +56,7 @@ describe('Data Structure : Queue', () => {
5656
expect(queue.dequeue()).toEqual(null);
5757
});
5858

59-
it('Length of linkedlist', () => {
59+
it('Shoud return size of Queue', () => {
6060
const queue2 = new Queue();
6161
queue2.enqueue(2);
6262
queue2.enqueue(1);
@@ -65,7 +65,7 @@ describe('Data Structure : Queue', () => {
6565
expect(queue2.length()).toEqual(4);
6666
});
6767

68-
it('Destroy linkedList', () => {
68+
it('Should Destroy Queue', () => {
6969
queue.destroy();
7070
expect(queue.length()).toEqual(0);
7171
});

src/_DataStructures_/Trees/BinarySearchTree/bst-isEmpty.test.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@ const BinarySearchTree = require('./index');
33
describe('Binary Search Tree', () => {
44
describe('Is Empty', () => {
55
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', () => {
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
it('should return False when BST is not empty', () => {
139
expect(bst.isEmpty()).toEqual(false);
1410
});
1511

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-
12+
it('should return True when BST is empty', () => {
13+
keys.push(6);
14+
keys.forEach(el => bst.remove(el));
2515
expect(bst.isEmpty()).toEqual(true);
2616
});
2717
});

src/_DataStructures_/Trees/BinarySearchTree/bst-maximum.test.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ const BinarySearchTree = require('./index');
33
describe('Binary Search Tree', () => {
44
describe('Find maximum value in BST', () => {
55
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', () => {
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should expect maximum key', () => {
1310
expect(bst.getMaximum()).toEqual(12);
1411
});
1512

16-
it('should return 20', () => {
13+
it('It should expect new maximum key added in BST', () => {
1714
bst.add(20);
1815
expect(bst.getMaximum()).toEqual(20);
1916
});

src/_DataStructures_/Trees/BinarySearchTree/bst-minimum.test.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
const BinarySearchTree = require('./index');
22

33
describe('Binary Search Tree', () => {
4-
describe('Find minimum value in BST', () => {
4+
describe('It should Find the minimum value in BST', () => {
55
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', () => {
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
8+
9+
it('It should expect minimum key', () => {
1310
expect(bst.getMinimum()).toEqual(2);
1411
});
1512

16-
it('should return 1', () => {
13+
it('It should expect new minimum key added to BST', () => {
1714
bst.add(1);
1815
expect(bst.getMinimum()).toEqual(1);
1916
});

src/_DataStructures_/Trees/BinarySearchTree/bst-remove.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const BST = require('.');
22

33
describe('Data Structure : Binary Search Tree', () => {
4-
it('Should be class', () => {
4+
it('Binary Search Tree should be a Class', () => {
55
expect(typeof BST.prototype.constructor).toEqual('function');
66
});
77

src/_DataStructures_/Trees/BinarySearchTree/bst-search.test.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
const BinarySearchTree = require('./index');
22

33
describe('Binary Search Tree', () => {
4-
describe('Find maximum value in BST', () => {
4+
describe('It should Find the key in BST', () => {
55
const bst = new BinarySearchTree(6);
6+
const keys = [4, 9, 2, 5, 8, 12];
7+
keys.forEach(el => bst.add(el));
68

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', () => {
9+
it('It should return true for 8', () => {
1410
expect(bst.searchFor(8)).toEqual(true);
1511
});
1612

17-
it('search for 100', () => {
13+
it('It should return false for 100', () => {
1814
expect(bst.searchFor(100)).toEqual(false);
1915
});
2016
});

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ describe('Binary search tree traversals', () => {
88
it('should create BST', () => {
99
// Creates BST
1010
bst = new BinarySearchTree(6);
11-
bst.add(4);
12-
bst.add(9);
13-
bst.add(2);
14-
bst.add(5);
15-
bst.add(8);
16-
bst.add(12);
11+
const keys = [4, 9, 2, 5, 8, 12];
12+
keys.forEach(el => bst.add(el));
1713
});
1814
});
1915

@@ -49,18 +45,12 @@ describe('Binary search tree traversals', () => {
4945
});
5046
});
5147

52-
describe('Check bst was created as expected', () => {
48+
describe('When root left subtree height is greater than right', () => {
5349
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);
50+
const keys = [11, 20, 9, 8, 7, 6, 5, 4];
51+
keys.forEach(el => bst2.add(el));
6252

63-
it('Height should be 7', () => {
53+
it('should return height of BST ', () => {
6454
expect(heightOfBST(bst2.root)).toEqual(7);
6555
});
6656
});

src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
* Given values of two values n1 and n2 in a Binary Search Tree, find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree.
55
*/
66

7-
function lca(node, n1, n2) {
8-
if (node == null) return null;
7+
function lowestCommonAncestor(node, n1, n2) {
8+
if (node === null) return null;
99

1010
// If both n1 and n2 are smaller than root, then LCA lies in left
11-
if (node.value > n1 && node.value > n2) { return lca(node.leftChild, n1, n2); }
11+
if (node.value > n1 && node.value > n2) { return lowestCommonAncestor(node.leftChild, n1, n2); }
1212

1313
// If both n1 and n2 are greater than root, then LCA lies in right
14-
if (node.value < n1 && node.value < n2) { return lca(node.rightChild, n1, n2); }
14+
if (node.value < n1 && node.value < n2) { return lowestCommonAncestor(node.rightChild, n1, n2); }
1515

1616
return node;
1717
}
1818

1919
module.exports = {
20-
lca,
20+
lowestCommonAncestor,
2121
};

src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.test.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,21 @@ const BinarySearchTree = require('../index');
44
// Quick JSON equivalent
55
// {"left":{"left":{"data":4},"right":{"left":{"data":10},"right":{"data":14},"data":12},"data":8},"right":{"data":22},"data":20}
66

7-
describe('LCA', () => {
7+
describe('Lowest Common Ancestor in BST', () => {
88
const bst = new BinarySearchTree(20);
9-
bst.add(22);
10-
bst.add(8);
11-
bst.add(12);
12-
bst.add(4);
13-
bst.add(14);
14-
bst.add(10);
9+
const keys = [22, 8, 12, 4, 14, 10];
10+
keys.forEach(el => bst.add(el));
1511

16-
it('Should return 12', () => {
12+
it('Should return Lowest Common Ancestor Node ', () => {
1713
expect(lca(bst.root, 10, 14).value).toEqual(12);
18-
});
19-
20-
it('Should return 8', () => {
2114
expect(lca(bst.root, 14, 8).value).toEqual(8);
22-
});
23-
24-
it('Should return 20', () => {
2515
expect(lca(bst.root, 10, 22).value).toEqual(20);
2616
});
2717

2818
const bst2 = new BinarySearchTree(6);
2919
bst2.remove(6);
3020

31-
it('Should return Null', () => {
21+
it('Should return Null when root is null', () => {
3222
expect(lca(bst2.root, 10, 22)).toEqual(null);
3323
});
3424
});

0 commit comments

Comments
 (0)