Skip to content

Commit

Permalink
Fix BST removal method.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jun 13, 2018
1 parent d57b725 commit c3a9618
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/data-structures/tree/avl-tree/AvlTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export default class AvlTree extends BinarySearchTree {
}
}

/**
* @param {*} value
* @return {boolean}
*/
remove(value) {
throw new Error(`Can't remove ${value}. Remove method is not implemented yet`);
}

/**
* @param {BinarySearchTreeNode} node
*/
Expand Down
10 changes: 10 additions & 0 deletions src/data-structures/tree/avl-tree/__test__/AvlTRee.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,14 @@ describe('AvlTree', () => {
expect(tree.toString()).toBe('10,15,18,30,35,40,42,43,45,47');
expect(tree.root.height).toBe(3);
});

it('should throw an error when trying to remove the node', () => {
const removeNodeAvlTree = () => {
const tree = new AvlTree();

tree.remove(1);
};

expect(removeNodeAvlTree).toThrowError();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class BinarySearchTree {

/**
* @param {*} value
* @return {BinarySearchTreeNode}
* @return {boolean}
*/
remove(value) {
return this.root.remove(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {

/**
* @param {*} value
* @return {BinarySearchTreeNode}
* @return {boolean}
*/
remove(value) {
const nodeToRemove = this.find(value);
Expand Down Expand Up @@ -120,7 +120,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
}
}

return nodeToRemove;
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ describe('BinarySearchTree', () => {

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

const removedNode1 = bst.remove(5);
const removed1 = bst.remove(5);
expect(bst.toString()).toBe('10,20');
expect(removedNode1.value).toBe(5);
expect(removed1).toBeTruthy();

const removedNode2 = bst.remove(20);
const removed2 = bst.remove(20);
expect(bst.toString()).toBe('10');
expect(removedNode2.value).toBe(20);
expect(removed2).toBeTruthy();
});

it('should insert object values', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ describe('BinarySearchTreeNode', () => {

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

const removedNode1 = bstRootNode.remove(5);
const removed1 = bstRootNode.remove(5);
expect(bstRootNode.toString()).toBe('10,20');
expect(removedNode1.value).toBe(5);
expect(removed1).toBeTruthy();

const removedNode2 = bstRootNode.remove(20);
const removed2 = bstRootNode.remove(20);
expect(bstRootNode.toString()).toBe('10');
expect(removedNode2.value).toBe(20);
expect(removed2).toBeTruthy();
});

it('should remove nodes with one child', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/tree/red-black-tree/RedBlackTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class RedBlackTree extends BinarySearchTree {

/**
* @param {*} value
* @return {BinarySearchTreeNode}
* @return {boolean}
*/
remove(value) {
throw new Error(`Can't remove ${value}. Remove method is not implemented yet`);
Expand Down

0 comments on commit c3a9618

Please sign in to comment.