Skip to content

Commit 44060dd

Browse files
committed
fix: before & after blocks added, fix order of execution
1 parent d6adc76 commit 44060dd

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

src/_DataStructures_/Trees/BinarySearchTree/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class BinarySearchTree {
3131
return this.BSTUtils.inorder(this.root);
3232
}
3333

34-
searchFor(value) {
34+
search(value) {
3535
return this.BSTUtils.search(this.root, value);
3636
}
3737

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

+17-22
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Data Structure : Binary Search Tree', () => {
7070
});
7171
});
7272

73-
describe('Check if BST `Is Empty`', () => {
73+
describe('Check if BST `Is Empty`, find Min & Max in BST', () => {
7474
const keys = [4, 9, 2, 5, 8, 12];
7575

7676
beforeEach(() => {
@@ -87,16 +87,11 @@ describe('Data Structure : Binary Search Tree', () => {
8787
});
8888

8989
it('Should return `true` when BST is empty', () => {
90-
bst.remove(6);
90+
// remove all the nodes
91+
keys.push(6); // head node
92+
keys.forEach(e => bst.remove(e));
9193
expect(bst.isEmpty()).toEqual(true);
9294
});
93-
});
94-
95-
/*
96-
97-
describe('Find maximum value in BST', () => {
98-
bst = new BinarySearchTree(6);
99-
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
10095

10196
it('Should expect maximum key', () => {
10297
expect(bst.getMaximum()).toEqual(12);
@@ -106,11 +101,6 @@ describe('Data Structure : Binary Search Tree', () => {
106101
bst.add(20);
107102
expect(bst.getMaximum()).toEqual(20);
108103
});
109-
});
110-
111-
describe('Find the minimum value in BST', () => {
112-
bst = new BinarySearchTree(6);
113-
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
114104

115105
it('Should expect minimum key', () => {
116106
expect(bst.getMinimum()).toEqual(2);
@@ -123,8 +113,6 @@ describe('Data Structure : Binary Search Tree', () => {
123113
});
124114

125115
describe('Remove Node in BST', () => {
126-
bst = null;
127-
128116
beforeEach(() => {
129117
bst = new BinarySearchTree(5);
130118
});
@@ -133,9 +121,9 @@ describe('Data Structure : Binary Search Tree', () => {
133121
bst.add(4);
134122
bst.add(9);
135123
bst.add(2);
136-
bst.delete(bst.root, 4);
124+
bst.remove(4);
137125
expect(bst.inorder()).toEqual([2, 5, 9]);
138-
bst.delete(bst.root, 2);
126+
bst.remove(2);
139127
expect(bst.inorder()).toEqual([5, 9]);
140128
});
141129

@@ -149,18 +137,26 @@ describe('Data Structure : Binary Search Tree', () => {
149137

150138
describe('Search value in BST', () => {
151139
bst = new BinarySearchTree(6);
152-
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
153140

154141
it('Should return `true` for 8', () => {
155-
expect(bst.searchFor(8)).toEqual(true);
142+
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
143+
expect(bst.search(8)).toEqual(true);
156144
});
157145

158146
it('Should return `false` for 100', () => {
159-
expect(bst.searchFor(100)).toEqual(false);
147+
expect(bst.search(100)).toEqual(false);
160148
});
161149
});
162150

163151
describe('Traversals in BST', () => {
152+
beforeEach(() => {
153+
bst = new BinarySearchTree(6);
154+
[4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
155+
});
156+
afterEach(() => {
157+
if (bst.root) bst.root = null;
158+
});
159+
164160
it('Should return the `Preorder Traversal` for given BST', () => {
165161
const preOrderTraversal = bst.preorder();
166162
expect(preOrderTraversal).toEqual([6, 4, 2, 5, 9, 8, 12]);
@@ -176,5 +172,4 @@ describe('Data Structure : Binary Search Tree', () => {
176172
expect(postOrderTraversal).toEqual([2, 5, 4, 8, 12, 9, 6]);
177173
});
178174
});
179-
*/
180175
});

src/_DataStructures_/Trees/BinarySearchTree/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const utils = {
6666
return [...arr, root.value];
6767
},
6868

69+
// eslint-disable-next-line consistent-return
6970
search(root, value) {
7071
if (root === null) return false;
7172
if (value === root.value) return true;
@@ -76,7 +77,6 @@ const utils = {
7677
if (value > root.value) {
7778
return this.search(root.rightChild, value);
7879
}
79-
return false;
8080
},
8181

8282
delete(root, value) {

0 commit comments

Comments
 (0)