Return removed nodes in BST.

This commit is contained in:
Oleksii Trekhleb 2018-05-31 08:29:59 +03:00
parent 7a4265403c
commit f04626bc5c
3 changed files with 17 additions and 7 deletions

View File

@ -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);

View File

@ -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', () => {

View File

@ -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', () => {