File tree Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Original file line number Diff line number Diff line change 11const HashMap = require ( '../maps/hash-maps/hashmap' ) ;
2+
23/**
3- * Set implemented with our HashMap to have sublinear times on all operations
4+ * Set implemented with our HashMap
5+ * Have an average of O(1) time on all operations
46 */
57class HashMapSet {
68 /**
Original file line number Diff line number Diff line change 1- // const Tree = require('../trees/avl-tree');
2- const Tree = require ( '../trees/red-black-tree' ) ;
1+ // const Tree = require('../trees/avl-tree'); // faster lookups
2+ const Tree = require ( '../trees/red-black-tree' ) ; // faster insertion
33
44/**
55 * TreeSet implements a Set (collection of unique values)
@@ -24,6 +24,7 @@ class TreeSet {
2424
2525 /**
2626 * Add a new value (duplicates will be added only once)
27+ * Runtime: O(log n)
2728 * @param {any } value
2829 */
2930 add ( value ) {
@@ -33,37 +34,40 @@ class TreeSet {
3334 }
3435
3536 /**
36- * check if value is already on the set
37+ * Check if value is already on the set
38+ * Runtime: O(log n)
3739 * @param {any } value
40+ * @returns {boolean } true if exists or false otherwise
3841 */
3942 has ( value ) {
4043 return ! ! this . tree . get ( value ) ;
4144 }
4245
4346 /**
4447 * Delete a value from the set
48+ * Runtime: O(log n)
4549 * @param {any } value
4650 */
4751 delete ( value ) {
4852 return this . tree . remove ( value ) ;
4953 }
5054
5155 /**
52- * Get all the values on the Set
56+ * Default iterator for this set
5357 * @returns {iterator } values in ascending order
5458 */
55- * keys ( ) {
59+ * [ Symbol . iterator ] ( ) {
5660 for ( const node of this . tree . inOrderTraversal ( ) ) {
5761 yield node . value ;
5862 }
5963 }
6064
6165 /**
62- * Default iterator for this set
66+ * Get all the values on the Set
6367 * @returns {iterator } values in ascending order
6468 */
65- * [ Symbol . iterator ] ( ) {
66- yield * this . keys ( ) ;
69+ * keys ( ) {
70+ yield * this ;
6771 }
6872
6973 /**
You can’t perform that action at this time.
0 commit comments