Added findMax() method in BinarySearchTreeNode.js and corressponding test cases in BinarySearchTreenode.test.js

This commit is contained in:
RaviSadam 2024-07-03 12:58:16 +00:00
parent 2c67b48c21
commit 1d2d4206c3
2 changed files with 21 additions and 0 deletions

View 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();
}
}

View 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);
});
});