From 249de5d4769549e9f05562bb6dad50d4e0384524 Mon Sep 17 00:00:00 2001 From: Adrian Mejia Date: Sat, 3 Oct 2020 18:15:10 -0400 Subject: [PATCH 1/2] feat(treeMap): get last entry (highest value) --- .../maps/tree-maps/tree-map.js | 21 +++++++++ .../maps/tree-maps/tree-map.spec.js | 43 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/data-structures/maps/tree-maps/tree-map.spec.js diff --git a/src/data-structures/maps/tree-maps/tree-map.js b/src/data-structures/maps/tree-maps/tree-map.js index eed8bc78..10957aed 100644 --- a/src/data-structures/maps/tree-maps/tree-map.js +++ b/src/data-structures/maps/tree-maps/tree-map.js @@ -18,6 +18,11 @@ const Tree = require('../../trees/red-black-tree'); // fast insertion * allocate memory beforehand (e.g. HashMap’s initial capacity) * nor you have to rehash when is getting full. * + * Implementations in other languages: + * Java: https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/TreeMap.html + * C++: https://en.cppreference.com/w/cpp/container/map + * Python: none + * */ class TreeMap { // tag::constructor[] @@ -92,6 +97,22 @@ class TreeMap { } // end::delete[] + /** + * Get the last key/value pair (node with largest key) + */ + lastEntry() { + const node = this.tree.getRightmost(); + return node ? [node.value, node.data()] : []; + } + + /** + * Get the first key/value pair (node with smallest key) + */ + firstEntry() { + const node = this.tree.getLeftmost(); + return node ? [node.value, node.data()] : []; + } + // tag::iterators[] /** * Default iterator for this map diff --git a/src/data-structures/maps/tree-maps/tree-map.spec.js b/src/data-structures/maps/tree-maps/tree-map.spec.js new file mode 100644 index 00000000..8661207a --- /dev/null +++ b/src/data-structures/maps/tree-maps/tree-map.spec.js @@ -0,0 +1,43 @@ +// some parts tested on src/data-structures/maps/map.spec.js + +const TreeMap = require('./tree-map'); + +describe('TreeMap: keep values sorted', () => { + let map; + + beforeEach(() => { + map = new TreeMap(); + }); + + describe('when map is empty', () => { + describe('.lastEntry and .firstEntry', () => { + it('should get last/first entry', () => { + expect(map.lastEntry()).toEqual([]); + expect(map.firstEntry()).toEqual([]); + }); + }); + }); + + describe('when map has entries', () => { + beforeEach(() => { + map.set(20, { title: '3sum', passed: true }); + map.set(30, { title: '3sum', passed: false }); + map.set(10, { title: '2sum', passed: true }); + map.set(5, { title: '4sum', passed: false }); + }); + + describe('.lastEntry and .firstEntry', () => { + it('should get last/first entry', () => { + expect(map.lastEntry()).toEqual([ + 30, + { title: '3sum', passed: false }, + ]); + + expect(map.firstEntry()).toEqual([ + 5, + { title: '4sum', passed: false }, + ]); + }); + }); + }); +}); From 1b2d61211fa31175ef973e6c45fa0163b627c36c Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Sat, 3 Oct 2020 23:55:46 +0000 Subject: [PATCH 2/2] :bookmark: chore(release): 2.3.0 # [2.3.0](https://github.com/amejiarosario/dsa.js/compare/2.2.1...2.3.0) (2020-10-03) ### Features * **treeMap:** get last entry (highest value) ([249de5d](https://github.com/amejiarosario/dsa.js/commit/249de5d4769549e9f05562bb6dad50d4e0384524)) --- CHANGELOG.md | 7 +++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7be071e..c98336a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [2.3.0](https://github.com/amejiarosario/dsa.js/compare/2.2.1...2.3.0) (2020-10-03) + + +### Features + +* **treeMap:** get last entry (highest value) ([249de5d](https://github.com/amejiarosario/dsa.js/commit/249de5d4769549e9f05562bb6dad50d4e0384524)) + ## [2.2.1](https://github.com/amejiarosario/dsa.js/compare/2.2.0...2.2.1) (2020-10-02) diff --git a/package-lock.json b/package-lock.json index eb408cf2..7ea7ed10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "2.2.1", + "version": "2.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index caf7c267..9cf19af3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "2.2.1", + "version": "2.3.0", "description": "Data Structures & Algorithms in JS", "author": "Adrian Mejia (https://adrianmejia.com)", "homepage": "https://github.com/amejiarosario/dsa.js",