red-black-tree-typed 2.2.2 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +92 -37
  2. package/dist/cjs/index.cjs +163 -0
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +164 -0
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +163 -0
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +164 -0
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  14. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  15. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  16. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  18. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  19. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  20. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  21. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  22. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  23. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  24. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  25. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  26. package/dist/umd/red-black-tree-typed.js +164 -0
  27. package/dist/umd/red-black-tree-typed.js.map +1 -1
  28. package/dist/umd/red-black-tree-typed.min.js +3 -3
  29. package/dist/umd/red-black-tree-typed.min.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  32. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  33. package/src/data-structures/binary-tree/bst.ts +322 -13
  34. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  35. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  36. package/src/data-structures/graph/directed-graph.ts +126 -1
  37. package/src/data-structures/graph/undirected-graph.ts +160 -1
  38. package/src/data-structures/hash/hash-map.ts +110 -27
  39. package/src/data-structures/heap/heap.ts +107 -58
  40. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  41. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  42. package/src/data-structures/queue/deque.ts +95 -67
  43. package/src/data-structures/queue/queue.ts +90 -34
  44. package/src/data-structures/stack/stack.ts +58 -40
  45. package/src/data-structures/trie/trie.ts +109 -47
  46. package/src/interfaces/binary-tree.ts +2 -0
@@ -60,431 +60,99 @@ export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
60
60
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
61
61
  * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
62
62
  * @example
63
- * // text editor operation history
64
- * const actions = [
65
- * { type: 'insert', content: 'first line of text' },
66
- * { type: 'insert', content: 'second line of text' },
67
- * { type: 'delete', content: 'delete the first line' }
68
- * ];
69
- * const editorHistory = new DoublyLinkedList<{ type: string; content: string }>(actions);
63
+ * // basic DoublyLinkedList creation and push operation
64
+ * // Create a simple DoublyLinkedList with initial values
65
+ * const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
70
66
  *
71
- * console.log(editorHistory.last?.type); // 'delete'
72
- * console.log(editorHistory.pop()?.content); // 'delete the first line'
73
- * console.log(editorHistory.last?.type); // 'insert'
74
- * @example
75
- * // Browser history
76
- * const browserHistory = new DoublyLinkedList<string>();
67
+ * // Verify the list maintains insertion order
68
+ * console.log([...list]); // [1, 2, 3, 4, 5];
77
69
  *
78
- * browserHistory.push('home page');
79
- * browserHistory.push('search page');
80
- * browserHistory.push('details page');
70
+ * // Check length
71
+ * console.log(list.length); // 5;
81
72
  *
82
- * console.log(browserHistory.last); // 'details page'
83
- * console.log(browserHistory.pop()); // 'details page'
84
- * console.log(browserHistory.last); // 'search page'
73
+ * // Push a new element to the end
74
+ * list.push(6);
75
+ * console.log(list.length); // 6;
76
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
85
77
  * @example
86
- * // Use DoublyLinkedList to implement music player
87
- * // Define the Song interface
88
- * interface Song {
89
- * title: string;
90
- * artist: string;
91
- * duration: number; // duration in seconds
92
- * }
93
- *
94
- * class Player {
95
- * private playlist: DoublyLinkedList<Song>;
96
- * private currentSong: ReturnType<typeof this.playlist.getNodeAt> | undefined;
97
- *
98
- * constructor(songs: Song[]) {
99
- * this.playlist = new DoublyLinkedList<Song>();
100
- * songs.forEach(song => this.playlist.push(song));
101
- * this.currentSong = this.playlist.head;
102
- * }
103
- *
104
- * // Play the next song in the playlist
105
- * playNext(): Song | undefined {
106
- * if (!this.currentSong?.next) {
107
- * this.currentSong = this.playlist.head; // Loop to the first song
108
- * } else {
109
- * this.currentSong = this.currentSong.next;
110
- * }
111
- * return this.currentSong?.value;
112
- * }
113
- *
114
- * // Play the previous song in the playlist
115
- * playPrevious(): Song | undefined {
116
- * if (!this.currentSong?.prev) {
117
- * this.currentSong = this.playlist.tail; // Loop to the last song
118
- * } else {
119
- * this.currentSong = this.currentSong.prev;
120
- * }
121
- * return this.currentSong?.value;
122
- * }
123
- *
124
- * // Get the current song
125
- * getCurrentSong(): Song | undefined {
126
- * return this.currentSong?.value;
127
- * }
128
- *
129
- * // Loop through the playlist twice
130
- * loopThroughPlaylist(): Song[] {
131
- * const playedSongs: Song[] = [];
132
- * const initialNode = this.currentSong;
133
- *
134
- * // Loop through the playlist twice
135
- * for (let i = 0; i < this.playlist.length * 2; i++) {
136
- * playedSongs.push(this.currentSong!.value);
137
- * this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
138
- * }
139
- *
140
- * // Reset the current song to the initial song
141
- * this.currentSong = initialNode;
142
- * return playedSongs;
143
- * }
144
- * }
145
- *
146
- * const songs = [
147
- * { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
148
- * { title: 'Hotel California', artist: 'Eagles', duration: 391 },
149
- * { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
150
- * { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
151
- * ];
152
- * let player = new Player(songs);
153
- * // should play the next song
154
- * player = new Player(songs);
155
- * const firstSong = player.getCurrentSong();
156
- * const nextSong = player.playNext();
157
- *
158
- * // Expect the next song to be "Hotel California by Eagles"
159
- * console.log(nextSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
160
- * console.log(firstSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
161
- *
162
- * // should play the previous song
163
- * player = new Player(songs);
164
- * player.playNext(); // Move to the second song
165
- * const currentSong = player.getCurrentSong();
166
- * const previousSong = player.playPrevious();
167
- *
168
- * // Expect the previous song to be "Bohemian Rhapsody by Queen"
169
- * console.log(previousSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
170
- * console.log(currentSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
78
+ * // DoublyLinkedList pop and shift operations
79
+ * const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
171
80
  *
172
- * // should loop to the first song when playing next from the last song
173
- * player = new Player(songs);
174
- * player.playNext(); // Move to the second song
175
- * player.playNext(); // Move to the third song
176
- * player.playNext(); // Move to the fourth song
81
+ * // Pop removes from the end
82
+ * const last = list.pop();
83
+ * console.log(last); // 50;
177
84
  *
178
- * const nextSongToFirst = player.playNext(); // Should loop to the first song
85
+ * // Shift removes from the beginning
86
+ * const first = list.shift();
87
+ * console.log(first); // 10;
179
88
  *
180
- * // Expect the next song to be "Bohemian Rhapsody by Queen"
181
- * console.log(nextSongToFirst); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
182
- *
183
- * // should loop to the last song when playing previous from the first song
184
- * player = new Player(songs);
185
- * player.playNext(); // Move to the first song
186
- * player.playNext(); // Move to the second song
187
- * player.playNext(); // Move to the third song
188
- * player.playNext(); // Move to the fourth song
189
- *
190
- * const previousToLast = player.playPrevious(); // Should loop to the last song
191
- *
192
- * // Expect the previous song to be "Billie Jean by Michael Jackson"
193
- * console.log(previousToLast); // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
194
- *
195
- * // should loop through the entire playlist
196
- * player = new Player(songs);
197
- * const playedSongs = player.loopThroughPlaylist();
198
- *
199
- * // The expected order of songs for two loops
200
- * console.log(playedSongs); // [
201
- * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
202
- * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
203
- * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
204
- * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 },
205
- * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
206
- * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
207
- * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
208
- * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
209
- * // ]
89
+ * // Verify remaining elements
90
+ * console.log([...list]); // [20, 30, 40];
91
+ * console.log(list.length); // 3;
210
92
  * @example
211
- * // Use DoublyLinkedList to implement LRU cache
212
- * interface CacheEntry<K, V> {
213
- * key: K;
214
- * value: V;
215
- * }
216
- *
217
- * class LRUCache<K = string, V = any> {
218
- * private readonly capacity: number;
219
- * private list: DoublyLinkedList<CacheEntry<K, V>>;
220
- * private map: Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>;
221
- *
222
- * constructor(capacity: number) {
223
- * if (capacity <= 0) {
224
- * throw new Error('lru cache capacity must be greater than 0');
225
- * }
226
- * this.capacity = capacity;
227
- * this.list = new DoublyLinkedList<CacheEntry<K, V>>();
228
- * this.map = new Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>();
229
- * }
230
- *
231
- * // Get the current cache length
232
- * get length(): number {
233
- * return this.list.length;
234
- * }
235
- *
236
- * // Check if it is empty
237
- * get isEmpty(): boolean {
238
- * return this.list.isEmpty();
239
- * }
240
- *
241
- * // Get cached value
242
- * get(key: K): V | undefined {
243
- * const node = this.map.get(key);
244
- *
245
- * if (!node) return undefined;
246
- *
247
- * // Move the visited node to the head of the linked list (most recently used)
248
- * this.moveToFront(node);
249
- *
250
- * return node.value.value;
251
- * }
252
- *
253
- * // Set cache value
254
- * set(key: K, value: V): void {
255
- * // Check if it already exists
256
- * const node = this.map.get(key);
257
- *
258
- * if (node) {
259
- * // Update value and move to head
260
- * node.value.value = value;
261
- * this.moveToFront(node);
262
- * return;
263
- * }
264
- *
265
- * // Check capacity
266
- * if (this.list.length >= this.capacity) {
267
- * // Delete the least recently used element (the tail of the linked list)
268
- * const removedNode = this.list.tail;
269
- * if (removedNode) {
270
- * this.map.delete(removedNode.value.key);
271
- * this.list.pop();
272
- * }
273
- * }
274
- *
275
- * // Create new node and add to head
276
- * const newEntry: CacheEntry<K, V> = { key, value };
277
- * this.list.unshift(newEntry);
93
+ * // DoublyLinkedList for...of iteration and map operation
94
+ * const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
278
95
  *
279
- * // Save node reference in map
280
- * const newNode = this.list.head;
281
- * if (newNode) {
282
- * this.map.set(key, newNode);
283
- * }
284
- * }
96
+ * // Iterate through list
97
+ * const doubled = list.map(value => value * 2);
98
+ * console.log(doubled.length); // 5;
285
99
  *
286
- * // Delete specific key
287
- * delete(key: K): boolean {
288
- * const node = this.map.get(key);
289
- * if (!node) return false;
290
- *
291
- * // Remove from linked list
292
- * this.list.delete(node);
293
- * // Remove from map
294
- * this.map.delete(key);
295
- *
296
- * return true;
297
- * }
298
- *
299
- * // Clear cache
300
- * clear(): void {
301
- * this.list.clear();
302
- * this.map.clear();
303
- * }
304
- *
305
- * // Move the node to the head of the linked list
306
- * private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
307
- * this.list.delete(node);
308
- * this.list.unshift(node.value);
309
- * }
100
+ * // Use for...of loop
101
+ * const result: number[] = [];
102
+ * for (const item of list) {
103
+ * result.push(item);
310
104
  * }
311
- *
312
- * // should set and get values correctly
313
- * const cache = new LRUCache<string, number>(3);
314
- * cache.set('a', 1);
315
- * cache.set('b', 2);
316
- * cache.set('c', 3);
317
- *
318
- * console.log(cache.get('a')); // 1
319
- * console.log(cache.get('b')); // 2
320
- * console.log(cache.get('c')); // 3
321
- *
322
- * // The least recently used element should be evicted when capacity is exceeded
323
- * cache.clear();
324
- * cache.set('a', 1);
325
- * cache.set('b', 2);
326
- * cache.set('c', 3);
327
- * cache.set('d', 4); // This will eliminate 'a'
328
- *
329
- * console.log(cache.get('a')); // undefined
330
- * console.log(cache.get('b')); // 2
331
- * console.log(cache.get('c')); // 3
332
- * console.log(cache.get('d')); // 4
333
- *
334
- * // The priority of an element should be updated when it is accessed
335
- * cache.clear();
336
- * cache.set('a', 1);
337
- * cache.set('b', 2);
338
- * cache.set('c', 3);
339
- *
340
- * cache.get('a'); // access 'a'
341
- * cache.set('d', 4); // This will eliminate 'b'
342
- *
343
- * console.log(cache.get('a')); // 1
344
- * console.log(cache.get('b')); // undefined
345
- * console.log(cache.get('c')); // 3
346
- * console.log(cache.get('d')); // 4
347
- *
348
- * // Should support updating existing keys
349
- * cache.clear();
350
- * cache.set('a', 1);
351
- * cache.set('a', 10);
352
- *
353
- * console.log(cache.get('a')); // 10
354
- *
355
- * // Should support deleting specified keys
356
- * cache.clear();
357
- * cache.set('a', 1);
358
- * cache.set('b', 2);
359
- *
360
- * console.log(cache.delete('a')); // true
361
- * console.log(cache.get('a')); // undefined
362
- * console.log(cache.length); // 1
363
- *
364
- * // Should support clearing cache
365
- * cache.clear();
366
- * cache.set('a', 1);
367
- * cache.set('b', 2);
368
- * cache.clear();
369
- *
370
- * console.log(cache.length); // 0
371
- * console.log(cache.isEmpty); // true
105
+ * console.log(result); // [1, 2, 3, 4, 5];
372
106
  * @example
373
- * // finding lyrics by timestamp in Coldplay's "Fix You"
374
- * // Create a DoublyLinkedList to store song lyrics with timestamps
375
- * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
376
- *
377
- * // Detailed lyrics with precise timestamps (in milliseconds)
378
- * const lyrics = [
379
- * { time: 0, text: "When you try your best, but you don't succeed" },
380
- * { time: 4000, text: 'When you get what you want, but not what you need' },
381
- * { time: 8000, text: "When you feel so tired, but you can't sleep" },
382
- * { time: 12000, text: 'Stuck in reverse' },
383
- * { time: 16000, text: 'And the tears come streaming down your face' },
384
- * { time: 20000, text: "When you lose something you can't replace" },
385
- * { time: 24000, text: 'When you love someone, but it goes to waste' },
386
- * { time: 28000, text: 'Could it be worse?' },
387
- * { time: 32000, text: 'Lights will guide you home' },
388
- * { time: 36000, text: 'And ignite your bones' },
389
- * { time: 40000, text: 'And I will try to fix you' }
390
- * ];
391
- *
392
- * // Populate the DoublyLinkedList with lyrics
393
- * lyrics.forEach(lyric => lyricsList.push(lyric));
394
- *
395
- * // Test different scenarios of lyric synchronization
396
- *
397
- * // 1. Find lyric at exact timestamp
398
- * const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
399
- * console.log(exactTimeLyric?.text); // 'And ignite your bones'
400
- *
401
- * // 2. Find lyric between timestamps
402
- * const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
403
- * console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
107
+ * // Browser history
108
+ * const browserHistory = new DoublyLinkedList<string>();
404
109
  *
405
- * // 3. Find first lyric when timestamp is less than first entry
406
- * const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
407
- * console.log(earlyTimeLyric); // undefined
110
+ * browserHistory.push('home page');
111
+ * browserHistory.push('search page');
112
+ * browserHistory.push('details page');
408
113
  *
409
- * // 4. Find last lyric when timestamp is after last entry
410
- * const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
411
- * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
114
+ * console.log(browserHistory.last); // 'details page';
115
+ * console.log(browserHistory.pop()); // 'details page';
116
+ * console.log(browserHistory.last); // 'search page';
412
117
  * @example
413
- * // cpu process schedules
414
- * class Process {
415
- * constructor(
416
- * public id: number,
417
- * public priority: number
418
- * ) {}
419
- *
420
- * execute(): string {
421
- * return `Process ${this.id} executed.`;
422
- * }
118
+ * // DoublyLinkedList for LRU cache implementation
119
+ * interface CacheEntry {
120
+ * key: string;
121
+ * value: string;
423
122
  * }
424
123
  *
425
- * class Scheduler {
426
- * private queue: DoublyLinkedList<Process>;
427
- *
428
- * constructor() {
429
- * this.queue = new DoublyLinkedList<Process>();
430
- * }
431
- *
432
- * addProcess(process: Process): void {
433
- * // Insert processes into a queue based on priority, keeping priority in descending order
434
- * let current = this.queue.head;
435
- * while (current && current.value.priority >= process.priority) {
436
- * current = current.next;
437
- * }
438
- *
439
- * if (!current) {
440
- * this.queue.push(process);
441
- * } else {
442
- * this.queue.addBefore(current, process);
443
- * }
444
- * }
445
- *
446
- * executeNext(): string | undefined {
447
- * // Execute tasks at the head of the queue in order
448
- * const process = this.queue.shift();
449
- * return process ? process.execute() : undefined;
450
- * }
451
- *
452
- * listProcesses(): string[] {
453
- * return this.queue.toArray().map(process => `Process ${process.id} (Priority: ${process.priority})`);
454
- * }
455
- *
456
- * clear(): void {
457
- * this.queue.clear();
458
- * }
124
+ * // Simulate LRU cache using DoublyLinkedList
125
+ * // DoublyLinkedList is perfect because:
126
+ * // - O(1) delete from any position
127
+ * // - O(1) push to end
128
+ * // - Bidirectional traversal for LRU policy
129
+ *
130
+ * const cacheList = new DoublyLinkedList<CacheEntry>();
131
+ * const maxSize = 3;
132
+ *
133
+ * // Add cache entries
134
+ * cacheList.push({ key: 'user:1', value: 'Alice' });
135
+ * cacheList.push({ key: 'user:2', value: 'Bob' });
136
+ * cacheList.push({ key: 'user:3', value: 'Charlie' });
137
+ *
138
+ * // Try to add a new entry when cache is full
139
+ * if (cacheList.length >= maxSize) {
140
+ * // Remove the oldest (first) entry
141
+ * const evicted = cacheList.shift();
142
+ * console.log(evicted?.key); // 'user:1';
459
143
  * }
460
144
  *
461
- * // should add processes based on priority
462
- * let scheduler = new Scheduler();
463
- * scheduler.addProcess(new Process(1, 10));
464
- * scheduler.addProcess(new Process(2, 20));
465
- * scheduler.addProcess(new Process(3, 15));
466
- *
467
- * console.log(scheduler.listProcesses()); // [
468
- * // 'Process 2 (Priority: 20)',
469
- * // 'Process 3 (Priority: 15)',
470
- * // 'Process 1 (Priority: 10)'
471
- * // ]
472
- *
473
- * // should execute the highest priority process
474
- * scheduler = new Scheduler();
475
- * scheduler.addProcess(new Process(1, 10));
476
- * scheduler.addProcess(new Process(2, 20));
477
- *
478
- * console.log(scheduler.executeNext()); // 'Process 2 executed.'
479
- * console.log(scheduler.listProcesses()); // ['Process 1 (Priority: 10)']
145
+ * // Add new entry
146
+ * cacheList.push({ key: 'user:4', value: 'Diana' });
480
147
  *
481
- * // should clear all processes
482
- * scheduler = new Scheduler();
483
- * scheduler.addProcess(new Process(1, 10));
484
- * scheduler.addProcess(new Process(2, 20));
148
+ * // Verify current cache state
149
+ * console.log(cacheList.length); // 3;
150
+ * const cachedKeys = [...cacheList].map(entry => entry.key);
151
+ * console.log(cachedKeys); // ['user:2', 'user:3', 'user:4'];
485
152
  *
486
- * scheduler.clear();
487
- * console.log(scheduler.listProcesses()); // []
153
+ * // Access entry (in real LRU, this would move it to end)
154
+ * const foundEntry = [...cacheList].find(entry => entry.key === 'user:2');
155
+ * console.log(foundEntry?.value); // 'Bob';
488
156
  */
489
157
  export declare class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, DoublyLinkedListNode<E>> {
490
158
  protected _equals: (a: E, b: E) => boolean;
@@ -46,8 +46,124 @@ export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
46
46
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
47
47
  * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
48
48
  * @example
49
+ * // basic SinglyLinkedList creation and push operation
50
+ * // Create a simple SinglyLinkedList with initial values
51
+ * const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
52
+ *
53
+ * // Verify the list maintains insertion order
54
+ * console.log([...list]); // [1, 2, 3, 4, 5];
55
+ *
56
+ * // Check length
57
+ * console.log(list.length); // 5;
58
+ *
59
+ * // Push a new element to the end
60
+ * list.push(6);
61
+ * console.log(list.length); // 6;
62
+ * console.log([...list]); // [1, 2, 3, 4, 5, 6];
63
+ * @example
64
+ * // SinglyLinkedList pop and shift operations
65
+ * const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
66
+ *
67
+ * // Pop removes from the end
68
+ * const last = list.pop();
69
+ * console.log(last); // 50;
70
+ *
71
+ * // Shift removes from the beginning
72
+ * const first = list.shift();
73
+ * console.log(first); // 10;
74
+ *
75
+ * // Verify remaining elements
76
+ * console.log([...list]); // [20, 30, 40];
77
+ * console.log(list.length); // 3;
78
+ * @example
79
+ * // SinglyLinkedList unshift and forward traversal
80
+ * const list = new SinglyLinkedList<number>([20, 30, 40]);
81
+ *
82
+ * // Unshift adds to the beginning
83
+ * list.unshift(10);
84
+ * console.log([...list]); // [10, 20, 30, 40];
85
+ *
86
+ * // Access elements (forward traversal only for singly linked)
87
+ * const second = list.at(1);
88
+ * console.log(second); // 20;
89
+ *
90
+ * // SinglyLinkedList allows forward iteration only
91
+ * const elements: number[] = [];
92
+ * for (const item of list) {
93
+ * elements.push(item);
94
+ * }
95
+ * console.log(elements); // [10, 20, 30, 40];
96
+ *
97
+ * console.log(list.length); // 4;
98
+ * @example
99
+ * // SinglyLinkedList filter and map operations
100
+ * const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
101
+ *
102
+ * // Filter even numbers
103
+ * const filtered = list.filter(value => value % 2 === 0);
104
+ * console.log(filtered.length); // 2;
105
+ *
106
+ * // Map to double values
107
+ * const doubled = list.map(value => value * 2);
108
+ * console.log(doubled.length); // 5;
109
+ *
110
+ * // Use reduce to sum
111
+ * const sum = list.reduce((acc, value) => acc + value, 0);
112
+ * console.log(sum); // 15;
113
+ * @example
114
+ * // SinglyLinkedList for sequentially processed data stream
115
+ * interface LogEntry {
116
+ * timestamp: number;
117
+ * level: 'INFO' | 'WARN' | 'ERROR';
118
+ * message: string;
119
+ * }
120
+ *
121
+ * // SinglyLinkedList is ideal for sequential processing where you only need forward iteration
122
+ * // O(1) insertion/deletion at head, O(n) for tail operations
123
+ * const logStream = new SinglyLinkedList<LogEntry>();
124
+ *
125
+ * // Simulate incoming log entries
126
+ * const entries: LogEntry[] = [
127
+ * { timestamp: 1000, level: 'INFO', message: 'Server started' },
128
+ * { timestamp: 1100, level: 'WARN', message: 'Memory usage high' },
129
+ * { timestamp: 1200, level: 'ERROR', message: 'Connection failed' },
130
+ * { timestamp: 1300, level: 'INFO', message: 'Connection restored' }
131
+ * ];
132
+ *
133
+ * // Add entries to the stream
134
+ * for (const entry of entries) {
135
+ * logStream.push(entry);
136
+ * }
137
+ *
138
+ * console.log(logStream.length); // 4;
139
+ *
140
+ * // Process logs sequentially (only forward iteration needed)
141
+ * const processedLogs: string[] = [];
142
+ * for (const log of logStream) {
143
+ * processedLogs.push(`[${log.level}] ${log.message}`);
144
+ * }
145
+ *
146
+ * console.log(processedLogs); // [
147
+ * // '[INFO] Server started',
148
+ * // '[WARN] Memory usage high',
149
+ * // '[ERROR] Connection failed',
150
+ * // '[INFO] Connection restored'
151
+ * // ];
152
+ *
153
+ * // Get first log (O(1) - direct head access)
154
+ * const firstLog = logStream.at(0);
155
+ * console.log(firstLog?.message); // 'Server started';
156
+ *
157
+ * // Remove oldest log (O(1) operation at head)
158
+ * const removed = logStream.shift();
159
+ * console.log(removed?.message); // 'Server started';
160
+ * console.log(logStream.length); // 3;
161
+ *
162
+ * // Remaining logs still maintain order for sequential processing
163
+ * console.log(logStream.length); // 3;
164
+ * @example
49
165
  * // implementation of a basic text editor
50
- * class TextEditor {
166
+ * class TextEditor {
51
167
  * private content: SinglyLinkedList<string>;
52
168
  * private cursorIndex: number;
53
169
  * private undoStack: Stack<{ operation: string; data?: any }>;
@@ -100,17 +216,17 @@ export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
100
216
  * editor.insert('l');
101
217
  * editor.insert('l');
102
218
  * editor.insert('o');
103
- * console.log(editor.getText()); // 'Hello' // Output: "Hello"
219
+ * console.log(editor.getText()); // 'Hello'; // Output: "Hello"
104
220
  *
105
221
  * editor.delete();
106
- * console.log(editor.getText()); // 'Hell' // Output: "Hell"
222
+ * console.log(editor.getText()); // 'Hell'; // Output: "Hell"
107
223
  *
108
224
  * editor.undo();
109
- * console.log(editor.getText()); // 'Hello' // Output: "Hello"
225
+ * console.log(editor.getText()); // 'Hello'; // Output: "Hello"
110
226
  *
111
227
  * editor.moveCursor(1);
112
228
  * editor.insert('a');
113
- * console.log(editor.getText()); // 'Haello'
229
+ * console.log(editor.getText()); // 'Haello';
114
230
  */
115
231
  export declare class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
116
232
  protected _equals: (a: E, b: E) => boolean;