File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
src/data-structures/maps/tree-maps Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff 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 */
2227class TreeMap {
2328 // tag::constructor[]
@@ -92,6 +97,13 @@ class TreeMap {
9297 }
9398 // end::delete[]
9499
100+ lastEntry ( ) {
101+ return {
102+ key : this . tree . getRightmost ( ) . value ,
103+ value : this . tree . getRightmost ( ) . data ( ) ,
104+ } ;
105+ }
106+
95107 // tag::iterators[]
96108 /**
97109 * Default iterator for this map
Original file line number Diff line number Diff line change 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 items has ' , ( ) => {
13+ beforeEach ( ( ) => {
14+ map . set ( 20 , { title : '3sum' , passed : true } ) ;
15+ map . set ( 30 , { title : '3sum' , passed : false } ) ;
16+ map . set ( 10 , { title : '2sum' , passed : true } ) ;
17+ map . set ( 5 , { title : '4sum' , passed : false } ) ;
18+ } ) ;
19+
20+ describe ( '#lastEntry' , ( ) => {
21+ it ( 'should get last entry' , ( ) => {
22+ expect ( map . lastEntry ( ) ) . toEqual ( {
23+ key : 30 ,
24+ value : { title : '3sum' , passed : false } ,
25+ } ) ;
26+ } ) ;
27+ } ) ;
28+ } ) ;
29+ } ) ;
You can’t perform that action at this time.
0 commit comments