Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dc95e90

Browse files
committedDec 16, 2019
refactor: move all tests to a single file
1 parent 29f1b16 commit dc95e90

File tree

13 files changed

+185
-220
lines changed

13 files changed

+185
-220
lines changed
 

‎src/_DataStructures_/Trees/BinarySearchTree/bst-insertion.test.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎src/_DataStructures_/Trees/BinarySearchTree/bst-traversals.test.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
const BinarySearchTree = require('./index');
2+
3+
describe('Data Structure : Binary Search Tree', () => {
4+
let bst;
5+
let rootsLeftChild;
6+
let rootsRightChild;
7+
let rootsLeftChildsLeftChild;
8+
let rootsLeftChildsRightChild;
9+
let rootsRightChildsLeftChild;
10+
let rootsRightChildsRightChild;
11+
12+
it('Binary Search Tree should be a Class', () => {
13+
expect(typeof BinarySearchTree.prototype.constructor).toEqual('function');
14+
});
15+
16+
describe('Creation of BST', () => {
17+
it('Should create a BST with root 100', () => {
18+
bst = new BinarySearchTree(100);
19+
expect(bst.root.value).toEqual(100);
20+
});
21+
22+
it('Should add element 20 to the left of root node', () => {
23+
bst.add(20);
24+
rootsLeftChild = bst.root.leftChild;
25+
expect(rootsLeftChild.value).toEqual(20);
26+
});
27+
28+
it('Should add element 500 to the right of root node', () => {
29+
bst.add(500);
30+
rootsRightChild = bst.root.rightChild;
31+
expect(rootsRightChild.value).toEqual(500);
32+
});
33+
34+
it('Should add element 10 to the left of root"s left child', () => {
35+
bst.add(10);
36+
rootsLeftChildsLeftChild = bst.root.leftChild.leftChild;
37+
expect(rootsLeftChildsLeftChild.value).toEqual(10);
38+
});
39+
40+
it('Should add element 30 to the right of root"s left child', () => {
41+
bst.add(30);
42+
rootsLeftChildsRightChild = bst.root.leftChild.rightChild;
43+
expect(rootsLeftChildsRightChild.value).toEqual(30);
44+
});
45+
46+
it("Should add element 400 to the left of root's right child", () => {
47+
bst.add(400);
48+
rootsRightChildsLeftChild = bst.root.rightChild.leftChild;
49+
expect(rootsRightChildsLeftChild.value).toEqual(400);
50+
});
51+
52+
it("Should add element 600 to the right of root's right child", () => {
53+
bst.add(600);
54+
rootsRightChildsRightChild = bst.root.rightChild.rightChild;
55+
expect(rootsRightChildsRightChild.value).toEqual(600);
56+
});
57+
});
58+
59+
describe('Check insertion was as expected', () => {
60+
it('Inorder traversal of the created bst should be [ 10, 20, 30, 100, 400, 500, 600 ]', () => {
61+
expect(bst.inorder()).toEqual([10, 20, 30, 100, 400, 500, 600]);
62+
});
63+
64+
it('Preorder traversal of the created bst should be [ 100, 20, 10, 30, 500, 400, 600 ]', () => {
65+
expect(bst.preorder()).toEqual([100, 20, 10, 30, 500, 400, 600]);
66+
});
67+
68+
it('Postorder traversal of the created bst should be [ 10, 30, 20, 400, 600, 500, 100 ]', () => {
69+
expect(bst.postorder()).toEqual([10, 30, 20, 400, 600, 500, 100]);
70+
});
71+
});
72+
73+
describe('Check if BST `Is Empty`', () => {
74+
bst = new BinarySearchTree(6);
75+
const keys = [4, 9, 2, 5, 8, 12];
76+
keys.forEach(el => bst.add(el));
77+
it('Should return `false` when BST is not empty', () => {
78+
expect(bst.isEmpty()).toEqual(false);
79+
});
80+
81+
it('Should return `true` when BST is empty', () => {
82+
keys.push(6);
83+
keys.forEach(el => bst.remove(el));
84+
expect(bst.isEmpty()).toEqual(true);
85+
});
86+
});
87+
88+
describe('Find maximum value in BST', () => {
89+
bst = new BinarySearchTree(6);
90+
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
91+
92+
it('Should expect maximum key', () => {
93+
expect(bst.getMaximum()).toEqual(12);
94+
});
95+
96+
it('Should expect new maximum key added in BST', () => {
97+
bst.add(20);
98+
expect(bst.getMaximum()).toEqual(20);
99+
});
100+
});
101+
102+
describe('Find the minimum value in BST', () => {
103+
bst = new BinarySearchTree(6);
104+
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
105+
106+
it('Should expect minimum key', () => {
107+
expect(bst.getMinimum()).toEqual(2);
108+
});
109+
110+
it('Should expect new minimum key added to BST', () => {
111+
bst.add(1);
112+
expect(bst.getMinimum()).toEqual(1);
113+
});
114+
});
115+
116+
describe('Remove Node in BST', () => {
117+
bst = null;
118+
119+
beforeEach(() => {
120+
bst = new BinarySearchTree(5);
121+
});
122+
123+
it('Should delete() an element from Binary Search Tree', () => {
124+
bst.add(4);
125+
bst.add(9);
126+
bst.add(2);
127+
bst.delete(bst.root, 4);
128+
expect(bst.inorder()).toEqual([2, 5, 9]);
129+
bst.delete(bst.root, 2);
130+
expect(bst.inorder()).toEqual([5, 9]);
131+
});
132+
133+
it('Should return NULL if root is empty', () => {
134+
const bst2 = new BinarySearchTree(6);
135+
bst2.remove(6);
136+
bst2.remove(9);
137+
expect(bst2.root).toEqual(null);
138+
});
139+
});
140+
141+
describe('Search value in BST', () => {
142+
bst = new BinarySearchTree(6);
143+
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
144+
145+
it('Should return `true` for 8', () => {
146+
expect(bst.searchFor(8)).toEqual(true);
147+
});
148+
149+
it('Should return `false` for 100', () => {
150+
expect(bst.searchFor(100)).toEqual(false);
151+
});
152+
});
153+
154+
describe('Traversals in BST', () => {
155+
it('Should return the `Preorder Traversal` for given BST', () => {
156+
const preOrderTraversal = bst.preorder();
157+
expect(preOrderTraversal).toEqual([6, 4, 2, 5, 9, 8, 12]);
158+
});
159+
160+
it('Should return the `Inorder Traversal` for given BST', () => {
161+
const inOrderTraversal = bst.inorder();
162+
expect(inOrderTraversal).toEqual([2, 4, 5, 6, 8, 9, 12]);
163+
});
164+
165+
it('Should return the `Postorder Traversal` for given BST', () => {
166+
const postOrderTraversal = bst.postorder();
167+
expect(postOrderTraversal).toEqual([2, 5, 4, 8, 12, 9, 6]);
168+
});
169+
});
170+
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ describe('Binary search tree traversals', () => {
1515

1616
describe('Check bst was created as expected', () => {
1717
it('Inorder traversal of the created bst should be [ 2, 4, 5, 6, 8, 9, 12 ]', () => {
18-
expect(bst.traverseInorder()).toEqual([2, 4, 5, 6, 8, 9, 12]);
18+
expect(bst.inorder()).toEqual([2, 4, 5, 6, 8, 9, 12]);
1919
});
2020

2121
it('Preorder traversal of the created bst should be [ 6, 4, 2, 5, 9, 8, 12 ]', () => {
22-
expect(bst.traversePreorder()).toEqual([6, 4, 2, 5, 9, 8, 12]);
22+
expect(bst.preorder()).toEqual([6, 4, 2, 5, 9, 8, 12]);
2323
});
2424

2525
it('Postorder traversal of the created bst should be [ 2, 5, 4, 8, 12, 9, 6 ]', () => {
26-
expect(bst.traversePostorder()).toEqual([2, 5, 4, 8, 12, 9, 6]);
26+
expect(bst.postorder()).toEqual([2, 5, 4, 8, 12, 9, 6]);
2727
});
2828
});
2929

‎src/_DataStructures_/Trees/BinarySearchTree/height-of-bst/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function findHeightOfBST(root) {
2727
// myBST.add(10);
2828

2929
// // console.log(myBST.root);
30-
// console.log(myBST.traversePreorder());
30+
// console.log(myBST.preorder());
3131
// console.log(findHeightOfBST(myBST.root));
3232

3333
module.exports = findHeightOfBST;

‎src/_DataStructures_/Trees/BinarySearchTree/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class BinarySearchTree {
2323
return this.BSTUtils.preorder(this.root);
2424
}
2525

26-
traversePostorder() {
26+
postorder() {
2727
return this.BSTUtils.postorder(this.root);
2828
}
2929

30-
traverseInorder() {
30+
inorder() {
3131
return this.BSTUtils.inorder(this.root);
3232
}
3333

@@ -61,13 +61,13 @@ class BinarySearchTree {
6161

6262
// console.log(bst.root);
6363

64-
// const preorder = bst.traversePreorder();
64+
// const preorder = bst.preorder();
6565
// console.log('Preorder Traversal - ', preorder);
6666

67-
// const inorder = bst.traverseInorder();
67+
// const inorder = bst.inorder();
6868
// console.log('Inorder Traversal - ', inorder);
6969

70-
// const postorder = bst.traversePostorder();
70+
// const postorder = bst.postorder();
7171
// console.log('Postorder Traversal - ', postorder);
7272

7373
// const search = 18;
@@ -80,7 +80,7 @@ class BinarySearchTree {
8080
// console.log('Maximum value =>', maxNode);
8181

8282
// bst.remove(4);
83-
// console.log(bst.traversePreorder());
83+
// console.log(bst.preorder());
8484

8585
// console.log(bst.root);
8686

‎src/_DataStructures_/Trees/BinaryTree/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ class BinaryTree {
2121
return root;
2222
}
2323

24-
traversePreorder(root) {
24+
preorder(root) {
2525
let arr = [];
2626

2727
if (root === null) return arr;
2828
// push node to arr
2929
arr.push(root.value);
3030

3131
// push left node
32-
const left = this.traversePreorder(root.leftChild);
32+
const left = this.preorder(root.leftChild);
3333
arr = [...arr, ...left];
3434

3535
// push right node
36-
const right = this.traversePreorder(root.rightChild);
36+
const right = this.preorder(root.rightChild);
3737
arr = [...arr, ...right];
3838

3939
return arr;
4040
}
4141

4242
preOrder() {
43-
return this.traversePreorder(this.root);
43+
return this.preorder(this.root);
4444
}
4545
}
4646

‎src/_Problems_/bfs-bst/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const myBST = new BST(51);
2323

2424
[10, 34, 32, 12, 90, 54, 61, 2, 71, 9].forEach(e => myBST.add(e));
2525

26-
const preOrderElements = myBST.traversePreorder();
26+
const preOrderElements = myBST.preorder();
2727
const levelOrderElements = traverseBFS(myBST.root);
2828

2929
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)
Please sign in to comment.