Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3a9618

Browse files
committed
Fix BST removal method.
1 parent d57b725 commit c3a9618

File tree

7 files changed

+30
-12
lines changed

7 files changed

+30
-12
lines changed

‎src/data-structures/tree/avl-tree/AvlTree.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export default class AvlTree extends BinarySearchTree {
1616
}
1717
}
1818

19+
/**
20+
* @param {*} value
21+
* @return {boolean}
22+
*/
23+
remove(value) {
24+
throw new Error(`Can't remove ${value}. Remove method is not implemented yet`);
25+
}
26+
1927
/**
2028
* @param {BinarySearchTreeNode} node
2129
*/

‎src/data-structures/tree/avl-tree/__test__/AvlTRee.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,14 @@ describe('AvlTree', () => {
229229
expect(tree.toString()).toBe('10,15,18,30,35,40,42,43,45,47');
230230
expect(tree.root.height).toBe(3);
231231
});
232+
233+
it('should throw an error when trying to remove the node', () => {
234+
const removeNodeAvlTree = () => {
235+
const tree = new AvlTree();
236+
237+
tree.remove(1);
238+
};
239+
240+
expect(removeNodeAvlTree).toThrowError();
241+
});
232242
});

‎src/data-structures/tree/binary-search-tree/BinarySearchTree.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class BinarySearchTree {
2929

3030
/**
3131
* @param {*} value
32-
* @return {BinarySearchTreeNode}
32+
* @return {boolean}
3333
*/
3434
remove(value) {
3535
return this.root.remove(value);

‎src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
8181

8282
/**
8383
* @param {*} value
84-
* @return {BinarySearchTreeNode}
84+
* @return {boolean}
8585
*/
8686
remove(value) {
8787
const nodeToRemove = this.find(value);
@@ -120,7 +120,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
120120
}
121121
}
122122

123-
return nodeToRemove;
123+
return true;
124124
}
125125

126126
/**

‎src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ describe('BinarySearchTree', () => {
4343

4444
expect(bst.toString()).toBe('5,10,20');
4545

46-
const removedNode1 = bst.remove(5);
46+
const removed1 = bst.remove(5);
4747
expect(bst.toString()).toBe('10,20');
48-
expect(removedNode1.value).toBe(5);
48+
expect(removed1).toBeTruthy();
4949

50-
const removedNode2 = bst.remove(20);
50+
const removed2 = bst.remove(20);
5151
expect(bst.toString()).toBe('10');
52-
expect(removedNode2.value).toBe(20);
52+
expect(removed2).toBeTruthy();
5353
});
5454

5555
it('should insert object values', () => {

‎src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ describe('BinarySearchTreeNode', () => {
125125

126126
expect(bstRootNode.toString()).toBe('5,10,20');
127127

128-
const removedNode1 = bstRootNode.remove(5);
128+
const removed1 = bstRootNode.remove(5);
129129
expect(bstRootNode.toString()).toBe('10,20');
130-
expect(removedNode1.value).toBe(5);
130+
expect(removed1).toBeTruthy();
131131

132-
const removedNode2 = bstRootNode.remove(20);
132+
const removed2 = bstRootNode.remove(20);
133133
expect(bstRootNode.toString()).toBe('10');
134-
expect(removedNode2.value).toBe(20);
134+
expect(removed2).toBeTruthy();
135135
});
136136

137137
it('should remove nodes with one child', () => {

‎src/data-structures/tree/red-black-tree/RedBlackTree.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class RedBlackTree extends BinarySearchTree {
3434

3535
/**
3636
* @param {*} value
37-
* @return {BinarySearchTreeNode}
37+
* @return {boolean}
3838
*/
3939
remove(value) {
4040
throw new Error(`Can't remove ${value}. Remove method is not implemented yet`);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /