mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 15:53:02 +08:00
Fix BST removal method.
This commit is contained in:
parent
d57b725034
commit
c3a961840d
@ -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
|
||||
*/
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -29,7 +29,7 @@ export default class BinarySearchTree {
|
||||
|
||||
/**
|
||||
* @param {*} value
|
||||
* @return {BinarySearchTreeNode}
|
||||
* @return {boolean}
|
||||
*/
|
||||
remove(value) {
|
||||
return this.root.remove(value);
|
||||
|
@ -81,7 +81,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
|
||||
/**
|
||||
* @param {*} value
|
||||
* @return {BinarySearchTreeNode}
|
||||
* @return {boolean}
|
||||
*/
|
||||
remove(value) {
|
||||
const nodeToRemove = this.find(value);
|
||||
@ -120,7 +120,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
return nodeToRemove;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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', () => {
|
||||
|
@ -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', () => {
|
||||
|
@ -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`);
|
||||
|
Loading…
Reference in New Issue
Block a user