Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b079c0

Browse files
Merge pull request amejiarosario#85 from amejiarosario/feat/tree-map-improvements
feat(treeMap): get last entry (highest value)
2 parents b047c23 + 249de5d commit 3b079c0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

‎src/data-structures/maps/tree-maps/tree-map.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const Tree = require('../../trees/red-black-tree'); // fast insertion
1818
* allocate memory beforehand (e.g. HashMap’s initial capacity)
1919
* nor you have to rehash when is getting full.
2020
*
21+
* Implementations in other languages:
22+
* Java: https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/TreeMap.html
23+
* C++: https://en.cppreference.com/w/cpp/container/map
24+
* Python: none
25+
*
2126
*/
2227
class TreeMap {
2328
// tag::constructor[]
@@ -92,6 +97,22 @@ class TreeMap {
9297
}
9398
// end::delete[]
9499

100+
/**
101+
* Get the last key/value pair (node with largest key)
102+
*/
103+
lastEntry() {
104+
const node = this.tree.getRightmost();
105+
return node ? [node.value, node.data()] : [];
106+
}
107+
108+
/**
109+
* Get the first key/value pair (node with smallest key)
110+
*/
111+
firstEntry() {
112+
const node = this.tree.getLeftmost();
113+
return node ? [node.value, node.data()] : [];
114+
}
115+
95116
// tag::iterators[]
96117
/**
97118
* Default iterator for this map
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// some parts tested on src/data-structures/maps/map.spec.js
2+
3+
const TreeMap = require('./tree-map');
4+
5+
describe('TreeMap: keep values sorted', () => {
6+
let map;
7+
8+
beforeEach(() => {
9+
map = new TreeMap();
10+
});
11+
12+
describe('when map is empty', () => {
13+
describe('.lastEntry and .firstEntry', () => {
14+
it('should get last/first entry', () => {
15+
expect(map.lastEntry()).toEqual([]);
16+
expect(map.firstEntry()).toEqual([]);
17+
});
18+
});
19+
});
20+
21+
describe('when map has entries', () => {
22+
beforeEach(() => {
23+
map.set(20, { title: '3sum', passed: true });
24+
map.set(30, { title: '3sum', passed: false });
25+
map.set(10, { title: '2sum', passed: true });
26+
map.set(5, { title: '4sum', passed: false });
27+
});
28+
29+
describe('.lastEntry and .firstEntry', () => {
30+
it('should get last/first entry', () => {
31+
expect(map.lastEntry()).toEqual([
32+
30,
33+
{ title: '3sum', passed: false },
34+
]);
35+
36+
expect(map.firstEntry()).toEqual([
37+
5,
38+
{ title: '4sum', passed: false },
39+
]);
40+
});
41+
});
42+
});
43+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /