diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js index 53ce46e9..385b471f 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js @@ -10,9 +10,10 @@ export default class BinarySearchTree { /** * @param {*} value + * @return {BinarySearchTreeNode} */ insert(value) { - this.root.insert(value); + return this.root.insert(value); } /** @@ -25,6 +26,7 @@ export default class BinarySearchTree { /** * @param {*} value + * @return {BinarySearchTreeNode} */ remove(value) { return this.root.remove(value); diff --git a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js index 9c51c1da..9262474c 100644 --- a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js +++ b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js @@ -14,11 +14,13 @@ describe('BinarySearchTree', () => { it('should insert values', () => { const bst = new BinarySearchTree(); - bst.insert(10); - bst.insert(20); + const insertedNode1 = bst.insert(10); + const insertedNode2 = bst.insert(20); bst.insert(5); expect(bst.toString()).toBe('5,10,20'); + expect(insertedNode1.value).toBe(10); + expect(insertedNode2.value).toBe(20); }); it('should check if value exists', () => { @@ -41,10 +43,13 @@ describe('BinarySearchTree', () => { expect(bst.toString()).toBe('5,10,20'); - bst.remove(5); + const removedNode1 = bst.remove(5); expect(bst.toString()).toBe('10,20'); - bst.remove(20); + expect(removedNode1.value).toBe(5); + + const removedNode2 = bst.remove(20); expect(bst.toString()).toBe('10'); + expect(removedNode2.value).toBe(20); }); it('should insert object values', () => { diff --git a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js index c8dab915..35a7fc53 100644 --- a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js +++ b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js @@ -125,10 +125,13 @@ describe('BinarySearchTreeNode', () => { expect(bstRootNode.toString()).toBe('5,10,20'); - bstRootNode.remove(5); + const removedNode1 = bstRootNode.remove(5); expect(bstRootNode.toString()).toBe('10,20'); - bstRootNode.remove(20); + expect(removedNode1.value).toBe(5); + + const removedNode2 = bstRootNode.remove(20); expect(bstRootNode.toString()).toBe('10'); + expect(removedNode2.value).toBe(20); }); it('should remove nodes with one child', () => {