Fix BST removal method.

This commit is contained in:
Oleksii Trekhleb 2018-06-13 06:39:18 +03:00
parent d57b725034
commit c3a961840d
7 changed files with 30 additions and 12 deletions

View File

@ -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 * @param {BinarySearchTreeNode} node
*/ */

View File

@ -229,4 +229,14 @@ describe('AvlTree', () => {
expect(tree.toString()).toBe('10,15,18,30,35,40,42,43,45,47'); expect(tree.toString()).toBe('10,15,18,30,35,40,42,43,45,47');
expect(tree.root.height).toBe(3); 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();
});
}); });

View File

@ -29,7 +29,7 @@ export default class BinarySearchTree {
/** /**
* @param {*} value * @param {*} value
* @return {BinarySearchTreeNode} * @return {boolean}
*/ */
remove(value) { remove(value) {
return this.root.remove(value); return this.root.remove(value);

View File

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

View File

@ -43,13 +43,13 @@ describe('BinarySearchTree', () => {
expect(bst.toString()).toBe('5,10,20'); expect(bst.toString()).toBe('5,10,20');
const removedNode1 = bst.remove(5); const removed1 = bst.remove(5);
expect(bst.toString()).toBe('10,20'); 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(bst.toString()).toBe('10');
expect(removedNode2.value).toBe(20); expect(removed2).toBeTruthy();
}); });
it('should insert object values', () => { it('should insert object values', () => {

View File

@ -125,13 +125,13 @@ describe('BinarySearchTreeNode', () => {
expect(bstRootNode.toString()).toBe('5,10,20'); expect(bstRootNode.toString()).toBe('5,10,20');
const removedNode1 = bstRootNode.remove(5); const removed1 = bstRootNode.remove(5);
expect(bstRootNode.toString()).toBe('10,20'); 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(bstRootNode.toString()).toBe('10');
expect(removedNode2.value).toBe(20); expect(removed2).toBeTruthy();
}); });
it('should remove nodes with one child', () => { it('should remove nodes with one child', () => {

View File

@ -34,7 +34,7 @@ export default class RedBlackTree extends BinarySearchTree {
/** /**
* @param {*} value * @param {*} value
* @return {BinarySearchTreeNode} * @return {boolean}
*/ */
remove(value) { remove(value) {
throw new Error(`Can't remove ${value}. Remove method is not implemented yet`); throw new Error(`Can't remove ${value}. Remove method is not implemented yet`);