Skip to content

Commit f04626b

Browse files
committed
Return removed nodes in BST.
1 parent 7a42654 commit f04626b

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export default class BinarySearchTree{
1010

1111
/**
1212
* @param{*} value
13+
* @return{BinarySearchTreeNode}
1314
*/
1415
insert(value){
15-
this.root.insert(value);
16+
returnthis.root.insert(value);
1617
}
1718

1819
/**
@@ -25,6 +26,7 @@ export default class BinarySearchTree{
2526

2627
/**
2728
* @param{*} value
29+
* @return{BinarySearchTreeNode}
2830
*/
2931
remove(value){
3032
returnthis.root.remove(value);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ describe('BinarySearchTree', () =>{
1414
it('should insert values',()=>{
1515
constbst=newBinarySearchTree();
1616

17-
bst.insert(10);
18-
bst.insert(20);
17+
constinsertedNode1=bst.insert(10);
18+
constinsertedNode2=bst.insert(20);
1919
bst.insert(5);
2020

2121
expect(bst.toString()).toBe('5,10,20');
22+
expect(insertedNode1.value).toBe(10);
23+
expect(insertedNode2.value).toBe(20);
2224
});
2325

2426
it('should check if value exists',()=>{
@@ -41,10 +43,13 @@ describe('BinarySearchTree', () =>{
4143

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

44-
bst.remove(5);
46+
constremovedNode1=bst.remove(5);
4547
expect(bst.toString()).toBe('10,20');
46-
bst.remove(20);
48+
expect(removedNode1.value).toBe(5);
49+
50+
constremovedNode2=bst.remove(20);
4751
expect(bst.toString()).toBe('10');
52+
expect(removedNode2.value).toBe(20);
4853
});
4954

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

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

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

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

128-
bstRootNode.remove(5);
128+
constremovedNode1=bstRootNode.remove(5);
129129
expect(bstRootNode.toString()).toBe('10,20');
130-
bstRootNode.remove(20);
130+
expect(removedNode1.value).toBe(5);
131+
132+
constremovedNode2=bstRootNode.remove(20);
131133
expect(bstRootNode.toString()).toBe('10');
134+
expect(removedNode2.value).toBe(20);
132135
});
133136

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

0 commit comments

Comments
(0)