diff --git a/README.md b/README.md index 2ff0a891..7c4c45e5 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,8 @@ npm test ``` npm test -- -t 'LinkedList' ``` + +## Useful Links + +- [Data Structures and Algorithms on YouTube](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) + diff --git a/src/data-structures/README.md b/src/data-structures/README.md index d2109f4b..ee682416 100644 --- a/src/data-structures/README.md +++ b/src/data-structures/README.md @@ -1,9 +1,5 @@ # Data Structures -**Useful Links** - -- [YouTube Playlist](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) - ## Common Data Structure Operations ![Common Data Structure Operations](https://github.com/trekhleb/javascript-algorithms/blob/master/assets/big-o-data-structures.png) diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index 6d068b8a..fed5b7c2 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -1,7 +1,7 @@ export default class BinaryTreeNode { - constructor(value = null, left = null, right = null) { - this.left = left; - this.right = right; + constructor(value = null) { + this.left = null; + this.right = null; this.value = value; } diff --git a/src/data-structures/tree/__test__/BinaryTreeNode.test.js b/src/data-structures/tree/__test__/BinaryTreeNode.test.js index 4bf869a1..8f500860 100644 --- a/src/data-structures/tree/__test__/BinaryTreeNode.test.js +++ b/src/data-structures/tree/__test__/BinaryTreeNode.test.js @@ -26,7 +26,11 @@ describe('BinaryTreeNode', () => { it('should traverse node', () => { const leftNode = new BinaryTreeNode(1); const rightNode = new BinaryTreeNode(3); - const rootNode = new BinaryTreeNode(2, leftNode, rightNode); + const rootNode = new BinaryTreeNode(2); + + rootNode + .addLeft(leftNode) + .addRight(rightNode); expect(rootNode.traverseInOrder()).toEqual([1, 2, 3]); diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js index d298c722..6fb7db69 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js @@ -21,20 +21,32 @@ export default class BinarySearchTreeNode extends BinaryTreeNode { return this; } - contains(value) { + find(value) { // Check the root. if (this.value === value) { - return true; + return this; } if (value < this.value && this.left) { // Check left nodes. - return this.left.contains(value); + return this.left.find(value); } else if (value > this.value && this.right) { // Check right nodes. - return this.right.contains(value); + return this.right.find(value); } - return false; + return null; + } + + contains(value) { + return !!this.find(value); + } + + findMin() { + if (!this.left) { + return this; + } + + return this.left.findMin(); } } diff --git a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js index 1191301e..0fb88539 100644 --- a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js +++ b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js @@ -56,4 +56,31 @@ describe('BinarySearchTreeNode', () => { expect(bstNode.contains(1)).toBeTruthy(); expect(bstNode.contains(3)).toBeFalsy(); }); + + it('should find min node', () => { + const node = new BinarySearchTreeNode(10); + + node.insert(20); + node.insert(30); + node.insert(5); + node.insert(40); + node.insert(1); + + expect(node.findMin()).not.toBeNull(); + expect(node.findMin().value).toBe(1); + }); + + it('should find node', () => { + const node = new BinarySearchTreeNode(10); + + node.insert(20); + node.insert(30); + node.insert(5); + node.insert(40); + node.insert(1); + + expect(node.find(6)).toBeNull(); + expect(node.find(5)).not.toBeNull(); + expect(node.find(5).value).toBe(5); + }); });