mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Added findMax() method in BinarySearchTreeNode.js and corressponding test cases in BinarySearchTreenode.test.js
This commit is contained in:
parent
2c67b48c21
commit
1d2d4206c3
11
src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js
Normal file → Executable file
11
src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js
Normal file → Executable file
@ -148,4 +148,15 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
|
||||
return this.left.findMin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns max node in Binary search tree
|
||||
* @returns {BinarySearchTreeNode}
|
||||
*/
|
||||
findMax() {
|
||||
if (!this || !this.right) {
|
||||
return this;
|
||||
}
|
||||
return this.right.findMax();
|
||||
}
|
||||
}
|
||||
|
10
src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js
Normal file → Executable file
10
src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js
Normal file → Executable file
@ -252,4 +252,14 @@ describe('BinarySearchTreeNode', () => {
|
||||
|
||||
expect(childNode.parent).toBeNull();
|
||||
});
|
||||
|
||||
it('should give max node', () => {
|
||||
const bst = new BinarySearchTreeNode(10);
|
||||
bst.insert(20);
|
||||
bst.insert(-2);
|
||||
bst.insert(4);
|
||||
bst.insert(-10);
|
||||
bst.insert(40);
|
||||
expect(bst.findMax().value).toBe(40);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user