mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Return removed nodes in BST.
This commit is contained in:
parent
7a4265403c
commit
f04626bc5c
@ -10,9 +10,10 @@ export default class BinarySearchTree {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {*} value
|
* @param {*} value
|
||||||
|
* @return {BinarySearchTreeNode}
|
||||||
*/
|
*/
|
||||||
insert(value) {
|
insert(value) {
|
||||||
this.root.insert(value);
|
return this.root.insert(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,6 +26,7 @@ export default class BinarySearchTree {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {*} value
|
* @param {*} value
|
||||||
|
* @return {BinarySearchTreeNode}
|
||||||
*/
|
*/
|
||||||
remove(value) {
|
remove(value) {
|
||||||
return this.root.remove(value);
|
return this.root.remove(value);
|
||||||
|
@ -14,11 +14,13 @@ describe('BinarySearchTree', () => {
|
|||||||
it('should insert values', () => {
|
it('should insert values', () => {
|
||||||
const bst = new BinarySearchTree();
|
const bst = new BinarySearchTree();
|
||||||
|
|
||||||
bst.insert(10);
|
const insertedNode1 = bst.insert(10);
|
||||||
bst.insert(20);
|
const insertedNode2 = bst.insert(20);
|
||||||
bst.insert(5);
|
bst.insert(5);
|
||||||
|
|
||||||
expect(bst.toString()).toBe('5,10,20');
|
expect(bst.toString()).toBe('5,10,20');
|
||||||
|
expect(insertedNode1.value).toBe(10);
|
||||||
|
expect(insertedNode2.value).toBe(20);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should check if value exists', () => {
|
it('should check if value exists', () => {
|
||||||
@ -41,10 +43,13 @@ describe('BinarySearchTree', () => {
|
|||||||
|
|
||||||
expect(bst.toString()).toBe('5,10,20');
|
expect(bst.toString()).toBe('5,10,20');
|
||||||
|
|
||||||
bst.remove(5);
|
const removedNode1 = bst.remove(5);
|
||||||
expect(bst.toString()).toBe('10,20');
|
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(bst.toString()).toBe('10');
|
||||||
|
expect(removedNode2.value).toBe(20);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should insert object values', () => {
|
it('should insert object values', () => {
|
||||||
|
@ -125,10 +125,13 @@ describe('BinarySearchTreeNode', () => {
|
|||||||
|
|
||||||
expect(bstRootNode.toString()).toBe('5,10,20');
|
expect(bstRootNode.toString()).toBe('5,10,20');
|
||||||
|
|
||||||
bstRootNode.remove(5);
|
const removedNode1 = bstRootNode.remove(5);
|
||||||
expect(bstRootNode.toString()).toBe('10,20');
|
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(bstRootNode.toString()).toBe('10');
|
||||||
|
expect(removedNode2.value).toBe(20);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove nodes with one child', () => {
|
it('should remove nodes with one child', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user