Skip to content

Commit dc95e90

Browse files
committed
refactor: move all tests to a single file
1 parent 29f1b16 commit dc95e90

13 files changed

+185
-220
lines changed

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

-68
This file was deleted.

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

-18
This file was deleted.

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

-18
This file was deleted.

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

-18
This file was deleted.

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

-32
This file was deleted.

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

-17
This file was deleted.

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

-34
This file was deleted.
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+
});

0 commit comments

Comments
 (0)